tinito
Posts: 4
Joined: Thu Apr 22, 2021 5:52 pm

CM4 bootloader and GPIO filters

Thu Apr 22, 2021 6:01 pm

I'm working on a bootloader configuration for the CM4 which allows us to update the OS from a USB stick.
So far I succeeded by adding BCM-USB-MSD to BOOT_ORDER, but as the timeout is as long as 20s, the standard boot is now much slower and not compatible with our product.

Is it possible to use GPIO filters in the recovery/boot.conf configuration file, as we do with config.txt, to specify the BOOT_ORDER?
Then I would try to boot from BCM-USB-MSD only if a button is pressed, otherwise directly boot from eMMC for much faster boot time.

cleverca22
Posts: 3792
Joined: Sat Aug 18, 2012 2:33 pm

Re: CM4 bootloader and GPIO filters

Thu Apr 22, 2021 8:23 pm

Code: Select all

[nix-shell:~/apps/rpi/lk-overlay]$ cat ../rpi-eeprom/firmware/beta/bootconf.txt
[all]
BOOT_UART=1
WAKE_ON_GPIO=1
POWER_OFF_ON_HALT=0
DHCP_TIMEOUT=45000
DHCP_REQ_TIMEOUT=4000
TFTP_FILE_TIMEOUT=30000
TFTP_IP=192.168.2.15
TFTP_PREFIX=0
SD_BOOT_MAX_RETRIES=3
NET_BOOT_MAX_RETRIES=5

gpio=21=ip,pu

[gpio21=0]
BOOT_ORDER=0x3

[gpio21=1]
BOOT_ORDER=0x5241

[none]
FREEZE_VERSION=1
ive been using gpio conditionals like this, because 3 (rpiboot) entirely lacks a timeout, so its essentially fatal when it fails

tinito
Posts: 4
Joined: Thu Apr 22, 2021 5:52 pm

Re: CM4 bootloader and GPIO filters

Fri Apr 23, 2021 7:46 am

Thank you, working for me as well (I just realized I wrote the flags in reverse order).

tinito
Posts: 4
Joined: Thu Apr 22, 2021 5:52 pm

Re: CM4 bootloader and GPIO filters

Thu May 13, 2021 4:05 pm

I'm now facing a weird issue: the bootloader EEPROM has been written as expected

Code: Select all

pi@raspberrypi:~ $ rpi-eeprom-config 
[all]
BOOT_UART=0
WAKE_ON_GPIO=1
POWER_OFF_ON_HALT=1

gpio=5=ip,pu

[gpio5=1]
BOOT_ORDER=0xf1

[gpio5=0]
BOOT_ORDER=0xf2345

ENABLE_SELF_UPDATE=1
But if I keep gpio5 to GND the CM4 actually boots from the USB drive *only* if the eMMC is erased.
If the eMMC has a valid boot partition, the CM4 always boots from the eMMC even if gpio5 is tied to ground.
Note that with gpio5=0 the bootloader should not even try to boot from the eMMC (BOOT_ORDER=0xf2345).

Any suggestions?

cleverca22
Posts: 3792
Joined: Sat Aug 18, 2012 2:33 pm

Re: CM4 bootloader and GPIO filters

Thu May 13, 2021 4:22 pm

thats weird, can you double-check that the gpio is working? try reading it from linux, and then pull it high and low, and see if it changes states?

tinito
Posts: 4
Joined: Thu Apr 22, 2021 5:52 pm

Re: CM4 bootloader and GPIO filters

Fri May 14, 2021 9:39 am

Solved! (see the end of this message)

GPIO5 is working fine if I read it with sysfs, and if the eMMC has an empty partition map GPIO5 makes a difference (not tied to ground -> fails to boot from eMMC, tied to ground -> it boots from USB).

I now set BOOT_UART=1 and this is the output:

Code: Select all

PM_RSTS: 0x00001000
RPi: BOOTLOADER release VERSION:d6d82cf9 DATE: Feb 16 2021 TIME: 13:23:40 BOOTMODE: 0x00000006 part: 0 BUILD_TIMESTAMP=1613481816 0xd085de1a 0x00a03140 0x000b333c
PM_RSTS: 0x00001000
part 00000000 reset_info 00000000
uSD voltage 3.3V
Initialising SDRAM 'Samsung' 8Gb x1 total-size: 8 Gbit 3200
PCI reset
PCI reset
PCIe timeout: 0x0000008f
USB xHC init failed
Boot mode: BCM-USB-MSD (05) order f4
XHCI-STOP
xHC ver: 272 HCS: 01000140 0c0000f1 07ff000a HCC: 0220fe65
xHC ports 1 slots 64 intrs 1
USB2[1] 000206e1 connected
USB2[1] 00200e03 connected enabled
USB2 root HUB port 1 init
DEV [01:00] 2.00 000000:01 class 9 VID 0424 PID 2514
HUB init [01:00] 2.00 000000:01
HUB [01:00] 2.00 000000:01 init port 1 speed 3
DEV [02:01] 2.16 000001:01 class 0 VID 0781 PID 5591
MSD device [02:01] 2.16 000001:01 conf 0 iface 0 ep 81#512 02#512
MSD [02:01] 2.16 000001:01 register MSD
MSD [02:01] 2.16 000001:01 LUN 0
MSD INQUIRY [02:01] 2.16 000001:01
MSD [02:01] 2.16 000001:01 lun 0 block-count 30031250 block-size 512
MBR: 0x00002000,  524288 type: 0x0c
MBR: 0x00082000, 3112960 type: 0x83
MBR: 0x0037a000,26384384 type: 0x0b
MBR: 0x00000000,       0 type: 0x00
Trying partition: 0
lba: 8192 oem: 'mkfs.fat' volume: ' boot       '
rsc 32 fat-sectors 4033 c-count 516190 c-size 1 r-dir 2 r-sec 0
PM_RSTS: 0x00001000
Trying partition: 0
lba: 8192 oem: 'mkfs.fat' volume: ' boot       '
rsc 32 fat-sectors 4033 c-count 516190 c-size 1 r-dir 2 r-sec 0
Read config.txt bytes     1784 hnd 0x000003f9 
SIG pieeprom.sig 501906e935115e7b2752bc07c09ac3bf283a31252f7d2eed374cd4b71a81386e 1620920679
Read pieeprom.upd bytes   524288 hnd 0x0001c220 
SELF-UPDATE timestamp current 1620982741 new 1620920679 skip
Read start4.elf bytes  2228800 hnd 0x0000e71e 
Read fixup4.dat bytes     5448 hnd 0x0000040c 
Firmware: 564e5f9b852b23a330b1764bcf0b2d022a20afd0 Feb 25 2021 12:10:40
0x00a03140 0x00000000 0x000000ff
MEM GPU: 76 ARM: 947 TOTAL: 1023
Starting start4.elf @ 0xfeb00200 partition 0
PCI reset
+
My understanding:
- Boot mode: BCM-USB-MSD (05) order f4 -> gpio5 is correctly read low, otherwise Boot mode would have been SD/eMMC (01)
- the MSD has been found (0781:5591 SanDisk Corporation Ultra USB 3.0)
- pieeprom.upd is found (it is on only the USB pen drive /boot partition) and correctly ignored as it is an old image
- start4.elf is executed from the MSD

Then I noticed that also /boot some times is mounted from /dev/sda2 (MSD), some times from /dev/mmcblk0p2, which lead me to check how rootfs is mounted: it uses the PARTUUID (root=PARTUUID=83c4223d-02 in /boot/cmdline.txt), which is the very same on both devices as they are clones of the official Raspberry Pi OS image.

[SOLUTION]
Updating /boot/cmdline.txt with root=/dev/sda2 on the /boot partition of the MSD solves the problem.
Maybe PARTUUID should be updated to a random value at the first boot (when rootfs is expanded?) to prevent such weird behaviours.

cleverca22
Posts: 3792
Joined: Sat Aug 18, 2012 2:33 pm

Re: CM4 bootloader and GPIO filters

Fri May 14, 2021 11:02 pm

aha, so its the same basic problem as noobs/pinn, and other users that want to boot usb first without conditions

it booted from the right /boot partition, but then mounted the wrong / once linux was in control

User avatar
procount
Posts: 2513
Joined: Thu Jun 27, 2013 12:32 pm
Location: UK

Re: CM4 bootloader and GPIO filters

Sat May 15, 2021 4:13 pm

tinito wrote:
Fri May 14, 2021 9:39 am
it uses the PARTUUID (root=PARTUUID=83c4223d-02 in /boot/cmdline.txt), which is the very same on both devices as they are clones of the official Raspberry Pi OS image.
PARTUUIDs are supposed to be unique within a system, so having the same PARTUUID on different devices will cause problems.
If you clone a drive, you should update the disk identifier to make the PARTUUIDs unique. You can do this with Fdisk extra options.
If you use the SD Card Copier in Rasp PIOS to clone the drive, then I believe it has the option to update the disk identifier to make it unique.
PINN - NOOBS with the extras... https://www.raspberrypi.org/forums/viewtopic.php?f=63&t=142574

Return to “Compute Module”