title: How to write new MLO and u-boot.img to an SD card without erasing the OS
body: <p>I have an SD card with a standard Beaglebone Debian image. I also have the <code>MLO</code> and <code>u-boot.img</code> files from building u-boot. I'd like to install the new u-boot on the SD card and then flash the modified image to MMC on a Beaglebone Black.</p>
  
  <p>I can flash onboard MMC from the SD card just fine. I can also connect to the board over serial and stop in u-boot.</p>
  
  <p><strong>What I don't know how to do is write the new u-boot to the SD card correctly. I've tried a few things</strong>:</p>
  
  <ol>
  <li><p>These <a href="https://elinux.org/Building_for_BeagleBone#Test_your_new_U-Boot" rel="noreferrer">elinux instructions</a> are for erasing the SD card and creating a new bootable partition. I didn't actually try this because I want to install the new u-boot to an existing image.</p>
  
  <pre><code>$ echo -e "o\nn\np\n1\n\n+64M\na\n1\nt\nc\nw\n" | sudo fdisk /dev/MYDISK ; sudo fdisk /dev/MYDISK -l
  $ sudo mount /dev/MYDISK /mnt
  $ sudo cp MLO /mnt
  $ sudo cp u-boot.img  /mnt
  $ sudo sync
  $ sudo umount  /mnt
  </code></pre></li>
  <li><p>These <a href="https://www.digikey.com/eewiki/display/linuxonarm/BeagleBone+Black#BeagleBoneBlack-SetupmicroSDcard" rel="noreferrer">digikey instructions</a> are a different version of the same thing. I tried writing MLO and u-boot.img in this way, without erasing the disk, but the board still boots using the original u-boot instead of the new one.</p>
  
  <pre><code>$ sudo dd if=/dev/zero of=${DISK} bs=1M count=10
  $ sudo dd if=./u-boot/MLO of=${DISK} count=1 seek=1 bs=128k
  $ sudo dd if=./u-boot/u-boot.img of=${DISK} count=2 seek=1 bs=384k
  </code></pre></li>
  <li><p>These <a href="https://wiki.beyondlogic.org/index.php?title=BeagleBoneBlack_Upgrading_uBoot#Saving_the_Bootloader_Images_to_Flash.2FCard" rel="noreferrer">beyondlogic instructions</a> have a slightly different pair of dd commands. I tried these as well, but again the board still boot susing the original u-boot.</p>
  
  <pre><code>$ sudo dd if=MLO of=/dev/sdb bs=512 seek=256 count=256 conv=notrunc
  $ sudo dd if=u-boot.img of=/dev/sdb bs=512 seek=768 count=1024 conv=notrunc
  $ sudo blockdev --flushbufs /dev/sdb
  </code></pre></li>
  <li><p>Someone suggested that I write the new u-boot to MMC using tftp. Can I use the same procedure to write to the SD card? If so, how do I find the start addresses? And how to I load and write the files? I tried searching, but I didn't find what I needed.</p>
  
  <pre><code>- boot into u-boot
  - discover the existing MLO and uboot.img start addresses
  - load the new MLO and uboot.img into RAM using tftp
  - write the new MLO and uboot.img to the correct location in MMC
  </code></pre></li>
  </ol>
  
  <p><strong>Additional Notes</strong></p>
  
  <ul>
  <li>Ideally, I would like to write the new u-boot to the SD card and test it without flashing. Supposedly the beaglebone will boot the SD card u-boot (instead of the MMC u-boot) if you press S2 when you supply power, but I'm not sure if that is working. <em>How can I tell which u-boot is loaded?</em></li>
  <li>However, I <em>can</em> write the MMC image to an SD card, so it would also work to write the new u-boot to MMC and test that.</li>
  <li>Lastly, I'm willing to erase the SD card, write u-boot with one of the above methods, and manually setup/write the partitions if I have to. Is that the only way to do this?</li>
  </ul>
  
  <p><strong>SD Card / Image</strong></p>
  
  <p>The SD card contains the Debian 7.9 2015-11-12 firmware image from <a href="https://beagleboard.org/latest-images" rel="noreferrer">beagleboard latest images</a>.</p>
  
  <p>This is the partition table.</p>
  
  <pre><code>Model: Generic- USB3.0 CRW -SD (scsi)
  Disk /dev/sdb: 31.9GB
  Sector size (logical/physical): 512B/512B
  Partition Table: msdos
  Disk Flags: 
  
  Number  Start   End     Size    Type     File system  Flags
   1      1049kB  102MB   101MB   primary  fat16        boot, lba
   2      102MB   3565MB  3463MB  primary  ext4
  </code></pre>
  
  <p>The boot partition does not contain MLO or u-boot.img files to replace:</p>
  
  <pre><code>total 96
  drwxr-xr-x 2 root root  2048 Nov 12  2015 App
  -rwxr-xr-x 1 root root   288 Nov 12  2015 autorun.inf
  drwxr-xr-x 4 root root  2048 Nov 12  2015 Docs
  drwxr-xr-x 5 root root  2048 Nov 12  2015 Drivers
  -rwxr-xr-x 1 root root    40 Nov 12  2015 ID.txt
  -rwxr-xr-x 1 root root 41174 Nov 12  2015 LICENSE.txt
  -rwxr-xr-x 1 root root  1008 Nov 12  2015 nfs-uEnv.txt
  -rwxr-xr-x 1 root root 16838 Nov 12  2015 README.htm
  -rwxr-xr-x 1 root root   428 Nov 12  2015 README.md
  drwxr-xr-x 2 root root  2048 Nov 12  2015 scripts
  -rwxr-xr-x 1 root root 16838 Nov 12  2015 START.htm
  -rwxr-xr-x 1 root root  1179 Nov 12  2015 uEnv.txt
  </code></pre>