Page 1 of 1

rpi2 usb boot

Posted: Thu Oct 31, 2019 8:37 am
by mokos
I know, I know... I'm not trolling (in a sense...) let's get to work!!!
The Idea here is to boot from SD card, load uboot and link/tag usb boot partition to continue boot the Pi. This is the only way in order to boot from external usb hard drive the pi.

first of all I gathered info from these sources:
[*]setting up U-Boot into SD:
https://blog.christophersmart.com/2016/ ... rm-boards/

1. I used VirtualBox in my Arch system and installed Fedora for my guest Os
2. I connected my usb/card reader and passed-through my Fedora guest

Partition the SD card
you don't need ext4 partition because you'll use that from USB!!
Assuming your card is at /dev/sdx (replace as necessary, check dmesg after plugging it in if you’re not sure).

Code: Select all

sudo umount /dev/sdx* # makes sure it's not mounted
sudo parted -s /dev/sdx \
mklabel msdos \
mkpart primary fat32 1M 30M 
Now we can format the partitions (upstream U-Boot supports ext3 on the boot partition).

Code: Select all

sudo mkfs.vfat /dev/sdx1
Next, mount the boot partition to /mnt, this is where we will copy everything to.

Code: Select all

sudo mount /dev/sdx1 /mnt
Upstream U-Boot Bootloader

Code: Select all

sudo dnf install \
gcc-arm-linux-gnu \
binutils-arm-linux-gnu \
uboot-tools
We need to clone upstream U-Boot Git tree. Note that I’m checking out the release directly (-b v2019.10) but you could leave this off to get master, or change it to a different tag if you want.

Code: Select all

cd "${HOME}"
git clone --depth 1 -b v2019.10 git://git.denx.de/u-boot.git
cd u-boot
There are default configs for both Raspberry Pi 2 and 3, so select the one you want.
# Run this for the Pi 2

Code: Select all

CROSS_COMPILE=arm-linux-gnu- make rpi_2_defconfig
# Run this for the Pi 3

Code: Select all

CROSS_COMPILE=arm-linux-gnu- make rpi_3_32b_defconfig
Now, compile it.

Code: Select all

CROSS_COMPILE=arm-linux-gnu- make -j$(nproc)
Now, copy the u-boot.bin file onto the SD card, and call it kernel.img (this is what the bootloader looks for).

Code: Select all

sudo cp -iv u-boot.bin /mnt/kernel.img
Proprietary bootloader files
Sadly, the Raspberry Pi cannot boot entirely on open source software, we need to get the proprietary files from Broadcom and place them on the SD card also.
Clone the Raspberry Pi Foundation’s GitHub repository.

Code: Select all

cd "${HOME}"
git clone --depth 1 https://github.com/raspberrypi/firmware
Copy the minimum set of required files to the SD card.

Code: Select all

sudo cp -iv firmware/boot/{bootcode.bin,fixup.dat,start.elf} /mnt/
Finally, unmount the SD card.

Code: Select all

sync && sudo umount /mnt
OK, now our bootloader should be ready to go.

Bootloader config
Next we need to make a bootloader file, boot.cmd, which tells U-Boot what to load and boot (the kernel, device tree and initramfs).
The bootargs line says to output the console to serial and to boot from the ramdisk. Variables are used for the memory locations of the kernel, dtb and initramfs.
Note that the root device is /dev/root which is required for the initramfs.

Code: Select all

cat > boot.cmd << EOF                                                
# After modifying, run ./mkscr
#Set root partition to the second partition of boot device
setenv usb_pgood_delay 10000;	#usb_pgood_delay initial value was 2000 and the usb search inside uboot was failing
setenv fdtfile bcm2709-rpi-2-b.dtb	#load from sd card the dev tree 
usb stop;
usb start;
usb dev 0;

setenv bootargs console=ttyS1,115200 console=tty0 root=/dev/sda2 rw rootwait rootdelay=30 smsc95xx.macaddr="${usbethadd>
if load usb 0:1 ${kernel_addr_r} /kernel7.img; then
  if load usb 0:1 ${fdt_addr_r} /${fdtfile}; then
    if load usb 0:1 ${ramdisk_addr_r} /initramfs-linux.img; then
      setenv usb_pgood_delay 200;		#reset the value here because the 1st usb search always fails
      bootz ${kernel_addr_r} ${ramdisk_addr_r}:${filesize} ${fdt_addr_r};
    else
      setenv usb_pgood_delay 200;		#reset the value here because the 1st usb search always fails
      bootz ${kernel_addr_r} - ${fdt_addr_r};
    fi;
  fi;
fi
EOF
Compile the bootloader file and output it directly to the SD card at /mnt.

Code: Select all

sudo mkimage -C none -A arm -T script -d boot.cmd /mnt/boot.scr
Now, unmount your SD card.

Code: Select all

sudo umount /dev/sdx*
Prepare Usb Disk
I used ArchLinuxArm and followed those instructions. make sure you put boot partition in the 1st place and root 2nd.

After Booting Arch Linux
finalizing Arch with this tutorial:
https://github.com/phortx/Raspberry-Pi-Setup-Guide

Final Words
this guide is for those who want pure usb bootable system from RPI that supports U-Boot and doesn't support usb from start... I was playing with my RPI2 and all I've ever wanted was to update my system and to minimize sd card corruption/degradation from the read/write jobs.

inside my boot.cmd file I linked some files from my USB boot partition that comes with Arch-Arm Image preinstalled
1. /kernel7.img
2. /${fdtfile}
3. /initramfs-linux.img
all 3 of them are located inside my usb boot partition
an image after booting rpi2:
Image

Re: rpi2 usb boot

Posted: Thu Oct 31, 2019 10:00 am
by HawaiianPi
mokos wrote:
Thu Oct 31, 2019 8:37 am
The Idea here is to boot from SD card, load uboot and link/tag usb boot partition to continue boot the Pi. This is the only way in order to boot from external usb hard drive the pi.
Actually, all you need to do is write a Raspbian image to your USB drive with Etcher, then put a copy of the bootcode.bin file on a FAT32 formatted SD card. The bootcode.bin file can be copied from the USB drive's "boot" partition or downloaded at the link I provided. Works for Pi Zero to Pi3B (and 3B+), although 3B can USB boot with no card as well.

Might work with other OS (never tried it with Arch).

Or, if it's a Pi 2Bv1.2 (or 3B) you can enable direct USB boot by setting a special OTP bit and skip the SD card altogether.
Raspberry Pi boot modes

Re: rpi2 usb boot

Posted: Thu Oct 31, 2019 9:08 pm
by andrum99
HawaiianPi wrote:
Thu Oct 31, 2019 10:00 am
mokos wrote:
Thu Oct 31, 2019 8:37 am
The Idea here is to boot from SD card, load uboot and link/tag usb boot partition to continue boot the Pi. This is the only way in order to boot from external usb hard drive the pi.
Actually, all you need to do is write a Raspbian image to your USB drive with Etcher, then put a copy of the bootcode.bin file on a FAT32 formatted SD card. The bootcode.bin file can be copied from the USB drive's "boot" partition or downloaded at the link I provided. Works for Pi Zero to Pi3B (and 3B+), although 3B can USB boot with no card as well.

Might work with other OS (never tried it with Arch).

Or, if it's a Pi 2Bv1.2 (or 3B) you can enable direct USB boot by setting a special OTP bit and skip the SD card altogether.
Raspberry Pi boot modes
Yup - that's the easy way to do it - everything on the USB drive, just bootcode.bin on the SD card (and nothing else). If you need to, you can also add an empty file named 'timeout' to the SD card if your hard disk/SSD takes longer than expected to become ready after power-on - see https://www.raspberrypi.org/documentati ... /README.md.

(Things are slightly more complicated if your SD card is 64GB or larger, since it will be formatted as exFAT by default, and the boot ROM in the Pi SoC cannot read exFAT partitions. In that case you need a workaround to get a FAT32 partition onto the card - see https://www.raspberrypi.org/documentati ... matting.md).

Re: rpi2 usb boot

Posted: Fri Nov 01, 2019 7:01 am
by HawaiianPi
andrum99 wrote:
Thu Oct 31, 2019 9:08 pm
Things are slightly more complicated if your SD card is 64GB or larger, since it will be formatted as exFAT by default
Not sure why anyone would use a 64GB+ card just for bootcode.bin, but I suppose I've seen crazier things. :?

Re: rpi2 usb boot

Posted: Fri Nov 01, 2019 7:23 am
by rpdom
HawaiianPi wrote:
Fri Nov 01, 2019 7:01 am
andrum99 wrote:
Thu Oct 31, 2019 9:08 pm
Things are slightly more complicated if your SD card is 64GB or larger, since it will be formatted as exFAT by default
Not sure why anyone would use a 64GB+ card just for bootcode.bin, but I suppose I've seen crazier things. :?
It depends on what is available. Many years ago I had some PCs that needed to netboot. They didn't have the bootcode in the network card ROMs, so needed to load it from external media. For some of them I used floppy disks 1.44MB disks with a 16KB boot file on. Then I ran out of floppy drives for the rest so I used CDROM drives with a 650MB CD containing that 1.44MB floppy image with the 16KB file on. :?

Re: rpi2 usb boot

Posted: Sun Nov 03, 2019 12:54 pm
by andrum99
I've got a slightly different setup for the Pi 4B currently under test - boot on SD card, root on hard disk. Procedure was as follows:

1. Image the SD card with the latest Raspbian Lite image.
2. Image the hard disk, also with the latest Raspbian Lite image.
3. On the SD card, edit the cmdline.txt file to remove the init=.... part which does the partition resize on first boot.
4. Attach the hard disk to the Pi 4B.
5. Insert the SD card on the Pi 4B and boot from it.
6. Interestingly, the Pi 4B booted up with both boot and root mounted from the hard disk, although it did use /boot from the SD card to actually boot.
7. Unmount the hard disk boot partition, and mount the one on the SD card instead.
8. Edit cmdline.txt on the SD card so root=PARTUUID=PARTUUID_of_root_partition_on_hard_disk.
9. Edit /etc/fstab (remember, this one is on the hard disk) and change boot to PARTUUID=PARTUUID_of_boot_on_SD_card.
(You can find the PARTUUIDs using 'sudo blkid').
10. Reboot the Pi 4B to confirm it works.
11. To avoid potential confusion, delete the root partition from the SD card using fdisk: fdisk /dev/mmcblk0, d 2 w.
12. Similarly, we want to make sure we're not using the boot partition on the hard disk by mistake, so delete all of the files from it:: sudo mkdir /mnt/temp, sudo mount /dev/sda /mnt/temp, cd /mnt/temp, sudo rm -rf *, sudo umount /dev/sda.
13. We now have only the boot partition on the SD card, a blank FAT32 partition on the hard disk, and an ext4 root partition on the hard disk. But at this stage the root partition has not yet been resized. At this point I take the relatively safe option to power down the Pi 4B and attach it to my laptop again, then use gparted to expand the root partition to fill the rest of the drive.
14. Reconnecting the hard disk to the Pi 4B, then powering on I can see with a quick df -h that the root partition is now full size.

This differs from other setups in that we only have one boot partition, and one root partition, across the two drives. When direct USB MSD boot becomes available on the Pi 4B, we just need to copy the contents of the boot partition from the SD card to the empty FAT32 partition on the hard disk, unmount /boot, remove the SD card, then reboot and it should come straight up from the hard disk. (Once we've configured the bootloader EEPROM of course).

Re: rpi2 usb boot

Posted: Sun Nov 03, 2019 6:46 pm
by jamesh
andrum99 wrote:
Sun Nov 03, 2019 12:54 pm
I've got a slightly different setup for the Pi 4B currently under test - boot on SD card, root on hard disk. Procedure was as follows:

1. Image the SD card with the latest Raspbian Lite image.
2. Image the hard disk, also with the latest Raspbian Lite image.
3. On the SD card, edit the cmdline.txt file to remove the init=.... part which does the partition resize on first boot.
4. Attach the hard disk to the Pi 4B.
5. Insert the SD card on the Pi 4B and boot from it.
6. Interestingly, the Pi 4B booted up with both boot and root mounted from the hard disk, although it did use /boot from the SD card to actually boot.
7. Unmount the hard disk boot partition, and mount the one on the SD card instead.
8. Edit cmdline.txt on the SD card so root=PARTUUID=PARTUUID_of_root_partition_on_hard_disk.
9. Edit /etc/fstab (remember, this one is on the hard disk) and change boot to PARTUUID=PARTUUID_of_boot_on_SD_card.
(You can find the PARTUUIDs using 'sudo blkid').
10. Reboot the Pi 4B to confirm it works.
11. To avoid potential confusion, delete the root partition from the SD card using fdisk: fdisk /dev/mmcblk0, d 2 w.
12. Similarly, we want to make sure we're not using the boot partition on the hard disk by mistake, so delete all of the files from it:: sudo mkdir /mnt/temp, sudo mount /dev/sda /mnt/temp, cd /mnt/temp, sudo rm -rf *, sudo umount /dev/sda.
13. We now have only the boot partition on the SD card, a blank FAT32 partition on the hard disk, and an ext4 root partition on the hard disk. But at this stage the root partition has not yet been resized. At this point I take the relatively safe option to power down the Pi 4B and attach it to my laptop again, then use gparted to expand the root partition to fill the rest of the drive.
14. Reconnecting the hard disk to the Pi 4B, then powering on I can see with a quick df -h that the root partition is now full size.

This differs from other setups in that we only have one boot partition, and one root partition, across the two drives. When direct USB MSD boot becomes available on the Pi 4B, we just need to copy the contents of the boot partition from the SD card to the empty FAT32 partition on the hard disk, unmount /boot, remove the SD card, then reboot and it should come straight up from the hard disk. (Once we've configured the bootloader EEPROM of course).
Don't forget the title of the thread....