Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add alternative flash method for Nice!Nano's #80

Open
wants to merge 3 commits into
base: main
Choose a base branch
from

Conversation

nakato
Copy link

@nakato nakato commented Jun 26, 2024

The Nice!Nano's with the adafruit bootloader, the default one, have three ways they can be flashed that I know of.

  1. Mount as filesystem and copy the file over
  2. writing uf2 file directly into device, such as with dd
  3. Using adafruit-nrfdfu

1 is what is currently used, it requires a mounting tool in user-space or root privileges to mount the device, and the user needs to know to mount it with options to give user permissions to access the filesystem, eg mount -o rw,uid=$(id -u user),gid=$(id -g user) /dev/sdX /mnt/nn.

The most annoying part of this method however is that the device just disappears when the file copy is done. Sometimes it leaves the device in the kernel uncleanly, so the next disk that appears, such as the second n!n will be at /dev/sdY.
If the user is copying the files manually and attempts to chain the umount command, this can regularly cause umount to stick in uninterruptible sleep, which is painful to correct as it requires rebooting and the reboot cannot be performed cleanly.

The dd method is pretty good, but it requires root privileges again, or possibly some custom udev rules to change the permissions on block device so a regular user can perform the action, but DDing the start of a disk is a bit scary and likely to raise eyebrows, so that method is probably best not used here.

Finally, the 3rd method. The bootloader exposes a serial port, this can be used via the adafruit-nrfdfu utility to write the application to the device as well, and that's what is being added here. The only requirement is the user is part of the dialout group so they have access to serial ports. The user puts the device into bootloader mode, and the flash proceeds without additional input.

To use this, I update flash to:

flash = zmk-nix.packages.''${system}.flash-nicenano-dfu.override { inherit firmware; };

and run

NIXPKGS_ALLOW_UNFREE=1 nix run --impure -L .#flash

In order to build a nRF52 DFU package for flashing via DFU a HEX copy is
required.
@nakato
Copy link
Author

nakato commented Jun 26, 2024

The CI failure has been caused by the license of adafruit-nrfutil being unfree, and that has caused nix flake check to fail.

Short of changing all the CI scripts to include NIXPKGS_ALLOW_UNFREE=1 and running them with --impure I'm unsure of how to resolve this, do you have any ideas?

I tried to use other implementations of nrfdfu, however they won`t work with the adafruit bootloader, so switching to a different implementation isn't a possible solution sadly.

@lilyinstarlight
Copy link
Owner

Yeahh I noticed that. I'll figure something out for that when I get a chance to review this, hopefully within the next few days :)

Thank you so much for working on this! <3

@nakato
Copy link
Author

nakato commented Jun 27, 2024

I'm working on an alternative method I'll push up in a separate PR along this one for consideration. It uses the block-device method, but I think I'll be able to address all my apprehensions to that method to the point it will not be scary to anyone. The downside to it is that it will require a udev rule to work, but it'll side-step the unfree issue for now.

Thank you for this! It's amazing to be able to build locally without a massive Zephyr fight!

Comment on lines 26 to 27
# Nordic Semicondictor proprietary license
license = licenses.unfree;
Copy link
Owner

@lilyinstarlight lilyinstarlight Jun 27, 2024

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Can you source this for me, actually?

All of the files I see in https://github.com/adafruit/Adafruit_nRF52_nrfutil at a glance all look to have either BSD 3-clause (with "Nordic Semiconductor"/"Nordic Semiconductor ASA" listed as copyright holder) or MIT (some files sourced from other projects)

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Sure, it's in the python package metadata in setup.py

And the header on nordicsemi/dfu/signing.py

There's an open issue on the Adafruit_nRF52_nrfutil project about this license issue: adafruit/Adafruit_nRF52_nrfutil#41

Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Ah, that's unfortunate. I only spot checked a handful of files and that was not one of them

License here should probably be [ licenses.bsd3 licenses.mit locenses.unfreeRedistributable ] with a comment describing unfreeRedistributable and linking to that issue, I suppose?

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I've updated this line to be this list.

I originally copy-and pasted it, so the list included the typo locenses on unfreeRedistributable, which actually didn't throw an error, so it looks like license does not get evaluated properly when it's a list, as unfreeRedistributable should still throw the unfree issue.

nakato added 2 commits July 1, 2024 12:48
Something's been done to the adafruit version of the nrf52 bootloader
that makes it incompatiable with the normal nrf dfu utilities, which
means we can't use an open reimplementation.  In theory if we throw out
signing.py all the code in this is GPL as that is the only code that
marked with Nordic Simi's custom license, all the others are marked with
BSD.

Nordic Simi's license pretty much says you can only use it to build for
nordic simi chips, so no using this tool to flash a raspberry pi pico.

This tool provides two useful uses.
1. It converts the hex into a bin, creates a header, and a json file and
   then packages all that into a zip.
2. Connects to the bootloader via a serial port and pushes that binary
   data onto the microcontroller, thus flashing with no mounting and no
   borking of the kernel when the device just decides to disappear
   without warning.
This is an alternative flashing method for the Nice Nano V2's.  It will
work with other nrf52 devices with some extending.

The user only needs to put the n!n into bootloader mode and they will
able to be flashed without requiring additional steps.  As well, because
the system no-longer has a mounted filesystem just up and disappearing
there wont be left behind dead device references and the user doesn't
have to worry about umount getting locked up into uninterruptible sleep,
which means the user needs to reboot with a system that cannot cleanly
unmount the root filesystem anymore.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants