Skip to content

Comments

initrd-flash.sh: check if serial number has letters or leading zeros#1913

Open
paroque28 wants to merge 1 commit intoOE4T:scarthgapfrom
paroque28:bug/initrd
Open

initrd-flash.sh: check if serial number has letters or leading zeros#1913
paroque28 wants to merge 1 commit intoOE4T:scarthgapfrom
paroque28:bug/initrd

Conversation

@paroque28
Copy link
Contributor

If serial number contains letters or leading zeros like: ABC0066978 this code prevents the bash script from failing and assigns a valid session_id

If serial number contains letters or leading zeros like: ABC0066978
this code prevents the bash script from failing and assigns a
valid session_id

Signed-off-by: Pablo Rodriguez Quesada <pablo.aarch64@gmail.com>
@madisongh
Copy link
Member

So you have a Jetson module that has a serial number starting with non-numeric characters? Is this something you did yourself, or did it come that way from the factory?

Whatever is in this script needs to match the serial number used in the init-flash script.

Maybe it would be simpler to just treat the serial number as a string and truncate it at 8 characters... that might depend on whether the USB ID_MODEL attribute is a numeric (hex) value or string, though.

@paroque28
Copy link
Contributor Author

paroque28 commented Jun 3, 2025

Let's wait a bit until we continue this PR.

This was originally created because my ./initrd_flash was failing:

./initrd-flash: line 509: printf: LKE0066978: invalid number`

But when I run: sudo bash -x /initrd_flash it works.
So I would like to know what is happening before we commit to any solution. I'll come back after investigation

@paroque28
Copy link
Contributor Author

Ignore my previous comment.
In both my successful and failed flash I get the invalid number error.
So anyways, yes, my serial number is unique, probably because I have a custom hardware.

Unfortunately my serial_number has nothing to do with my ID_MODEL=0

Then I guess, I'll need to implement my own initrd_flash script

@paroque28
Copy link
Contributor Author

@madisongh It seems that my problem was the udisksctl mount -b "$dev" command in the mount_partition()`function

Setting the bash -x makes it so that udisksctl runs after /dev/sdb is available but so does any other delay added, like prints of sleeps.

Without it I get: Error looking up object for device /dev/sdb

What would be the preferred fix?

As for this PR: yes , my board has a peculiar serial number that is different from the standard NVIDIA format. we can either close the PR or fix it in case other people experience it.

@dwalkes
Copy link
Member

dwalkes commented Jun 9, 2025

Thanks @paroque28

Setting the bash -x makes it so that udisksctl runs after /dev/sdb is available but so does any other delay added, like prints of sleeps.

Does this mean wait_for_usb_storage returns successfully at

local dev=$(wait_for_usb_storage "$session_id" "flashpkg" "$usb_instance")
local exports
if [ -z "$dev" ]; then
echo "ERR: could not locate USB storage device for sending flashing commands" >&2
return 1
fi
local devsize=$(cat /sys/block/$(basename $dev)/size 2>/dev/null)

but when the udisksctl command happens within mount_partition at https://github.com/OE4T/meta-tegra/blob/e4379e5131b0f0df3949c023a13f648506a3084d/recipes-bsp/tegra-binaries/tegra-helper-scripts/initrd-flash.sh#L228C8-L228C18 presumably without the delays associated with bash -x, the device is not actually accessible?

I'm wondering if the logic at afce451, basically an additional check for udevadm info could help, or if a similar udisksctl info perhaps turned on optionally could help in this scenario.

Is this only reproducible on a specific host? I'm wondering why I don't see it with my scarthgap builds.

@paroque28
Copy link
Contributor Author

Hey @dwalkes ,
Are you pointing at the wait_for_usb _storage function?
I know that's in place, but it's not working as expected. Very weird

@dwalkes
Copy link
Member

dwalkes commented Jun 9, 2025

@paroque28 sorry accidentally hit the comment button before I finished typing, I've updated the comment above.

@paroque28
Copy link
Contributor Author

@dwalkes should we move this discussion to an issue or PR?

I found this online:
storaged-project/udisks#711 (comment)

And I think this is exactly what is happening.
But to be fair I am the only one that has seen it, although it's very consistent. Other people building the same project don't see it.

I looked into gdbus and got this:

$ gdbus monitor --system --dest org.freedesktop.UDisks2 --object-path /org/freedesktop/UDisks2

Monitoring signals on object /org/freedesktop/UDisks2 owned by org.freedesktop.UDisks2
The name org.freedesktop.UDisks2 is owned by :1.8

/org/freedesktop/UDisks2: org.freedesktop.DBus.ObjectManager.InterfacesAdded (objectpath '/org/freedesktop/UDisks2/drives/flashpkg_0_0', {'org.freedesktop.UDisks2.Drive': {'Vendor': <'flashpkg'>, 'Model': <'0'>, 'Revision': <'0001'>, 'Serial': <'0'>, 'WWN': <''>, 'Id': <'flashpkg-0-0'>, 'Configuration': <@a{sv} {}>, 'Media': <''>, 'MediaCompatibility': <@as []>, 'MediaRemovable': <true>, 'MediaAvailable': <true>, 'MediaChangeDetected': <true>, 'Size': <uint64 134217728>, 'TimeDetected': <uint64 1749546974735956>, 'TimeMediaDetected': <uint64 1749546974735956>, 'Optical': <false>, 'OpticalBlank': <false>, 'OpticalNumTracks': <uint32 0>, 'OpticalNumAudioTracks': <uint32 0>, 'OpticalNumDataTracks': <uint32 0>, 'OpticalNumSessions': <uint32 0>, 'RotationRate': <-1>, 'ConnectionBus': <'usb'>, 'Seat': <'seat0'>, 'Removable': <true>, 'Ejectable': <true>, 'SortKey': <'01hotplug/1749546974735956'>, 'CanPowerOff': <true>, 'SiblingId': <'/sys/devices/pci0000:00/0000:00:14.0/usb1/1-1/1-1.2/1-1.2:1.0'>}})

/org/freedesktop/UDisks2: org.freedesktop.DBus.ObjectManager.InterfacesAdded (objectpath '/org/freedesktop/UDisks2/block_devices/sdb', {'org.freedesktop.UDisks2.Block': {'Device': <b'/dev/sdb'>, 'PreferredDevice': <b'/dev/sdb'>, 'Symlinks': <[b'/dev/disk/by-id/usb-flashpkg_0_0-0:0', b'/dev/disk/by-path/pci-0000:00:14.0-usb-0:1.2:1.0-scsi-0:0:0:0', b'/dev/disk/by-uuid/8c9af423-a4cc-4fbe-b3b3-961eacd24c1d']>, 'DeviceNumber': <uint64 2064>, 'Id': <'by-uuid-8c9af423-a4cc-4fbe-b3b3-961eacd24c1d'>, 'Size': <uint64 134217728>, 'ReadOnly': <false>, 'Drive': <objectpath '/org/freedesktop/UDisks2/drives/flashpkg_0_0'>, 'MDRaid': <objectpath '/'>, 'MDRaidMember': <objectpath '/'>, 'IdUsage': <'filesystem'>, 'IdType': <'ext4'>, 'IdVersion': <'1.0'>, 'IdLabel': <''>, 'IdUUID': <'8c9af423-a4cc-4fbe-b3b3-961eacd24c1d'>, 'Configuration': <@a(sa{sv}) []>, 'CryptoBackingDevice': <objectpath '/'>, 'HintPartitionable': <true>, 'HintSystem': <false>, 'HintIgnore': <false>, 'HintAuto': <true>, 'HintName': <''>, 'HintIconName': <''>, 'HintSymbolicIconName': <''>, 'UserspaceMountOptions': <@as []>}, 'org.freedesktop.UDisks2.Filesystem': {'MountPoints': <@aay []>, 'Size': <uint64 134217728>}})

It can give it a try

@dwalkes
Copy link
Member

dwalkes commented Jun 10, 2025

Thanks @paroque28 that does look relevant:

should we move this discussion to an issue or PR?

I think it's already in a PR? You can probably just force push/repurpose it with any improvements you find here.

It can give it a try

Do you mean incorporating gdbus as a part of wait_for_usb_storage? Or adding udevadm settle?

@paroque28
Copy link
Contributor Author

Update on this PR:

I am currently testing the following patch:

diff --git a/recipes-bsp/tegra-binaries/tegra-helper-scripts/initrd-flash.sh b/recipes-bsp/tegra-binaries/tegra-helper-scripts/initrd-flash.sh
index 49040033..10c2bf5f 100644
--- a/recipes-bsp/tegra-binaries/tegra-helper-scripts/initrd-flash.sh
+++ b/recipes-bsp/tegra-binaries/tegra-helper-scripts/initrd-flash.sh
@@ -225,6 +225,7 @@ mount_partition() {
         fi
         i=$(expr $i - 1)
     done
+	udevadm settle
     if udisksctl mount -b "$dev" > /dev/null; then
 	cat /proc/mounts | grep "^$dev" | cut -d' ' -f2
 	return 0
@@ -506,7 +507,7 @@ if [ -z "$serial_number" ]; then
     echo "WARN: did not get device serial number at $(date -Is)" | tee -a "$logfile"
     session_id=
 else
-    session_id=$(printf "%x" "$serial_number" | tail -c8)
+    session_id=$(echo "$serial_number" | sed 's/^[^0-9]*//' | tr -d '\n\0' | tail -c8)
 fi
 
 # Boot device flashing
diff --git a/recipes-core/initrdscripts/tegra-flash-init/init-flash.sh b/recipes-core/initrdscripts/tegra-flash-init/init-flash.sh
index 569e8591..339671ea 100644
--- a/recipes-core/initrdscripts/tegra-flash-init/init-flash.sh
+++ b/recipes-core/initrdscripts/tegra-flash-init/init-flash.sh
@@ -26,11 +26,7 @@ mount -t configfs configfs -o nosuid,nodev,noexec /sys/kernel/config
 
 [ ! -e /usr/sbin/wd_keepalive ] || /usr/sbin/wd_keepalive &
 
-sernum=$(cat /proc/device-tree/serial-number)
-if [ -n "$sernum" ]; then
-    # Restricted to 8 characters for the ID_MODEL tag
-    sernum=$(printf "%x" "$sernum" | tail -c8)
-fi
+sernum=$(cat /proc/device-tree/serial-number | sed 's/^[^0-9]*//' | tr -d '\n\0' | tail -c8)
 [ -n "$sernum" ] || sernum="UNKNOWN"
 echo "Serial number: $sernum"
 UDC=$(ls -1 /sys/class/udc | head -n 1)

I'll do a few more flashes (at least 10 more) , and then confirm that this behavior disappeared .

Note, even if my serial number has letters, my cand_model is numeric, therefore the sed to remove characters

$ udevadm info --query=property /dev/sdc | grep '^ID_MODEL='
ID_MODEL=06697805

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.

3 participants