title: Working FAT16 Bootloader Generates Read Error on Actual Hardware?
body: <p>For about the past week, I've been developing a simple OS for learning purposes and... "fun". VirtualBox and NASM in tow, I actually got off to a pretty good start. Eventually, I decided that I wanted to also develop a bootloader (after hitting the 512-byte wall pretty hard) by powering through the infamous <a href="http://www.brokenthorn.com/Resources/OSDevIndex.html" rel="noreferrer">Brokenthorn tutorial</a>, up until the point of loading from filesystems.</p>
  
  <p>With some HexFiend shenanigans and some blank FAT16 images, I eventually got the BPB worked out. With some additional Assembly hackery (the basis being Brokenthorn's tutorial, <a href="http://www.brokenthorn.com/Resources/OSDev6.html" rel="noreferrer">part 6</a>), I also got file loading working with my bootloader, which loads the aptly-named 'boot' file from my virtual disk (made using dd if=/dev/zero of=boot.img bs=512 count=2880)</p>
  
  <p>So, what is the issue then? It's what I see when I load on to actual hardware via a USB stick (in this case, /dev/disk3, where the compiled file is boot.bin):</p>
  
  <pre><code>dd bs=512 count=1 if=compiled/boot.bin of=/dev/disk3
  </code></pre>
  
  <p>Here is the expected output (in VirtualBox):</p>
  
  <p><img src="https://i.stack.imgur.com/zURX5.png" alt="Current output"></p>
  
  <p>Compared to the <a href="http://d.pr/RfDT+" rel="noreferrer">actual output</a> (on an old laptop)</p>
  
  <p><img src="https://i.stack.imgur.com/CD6xd.jpg" alt="Old output"></p>
  
  <pre><code>'-' indicates a sector is being loaded
  '_' indicates a sector was loaded
  '!' indicates all of the desired sectors were loaded properly
  'R' indicates a read error
  'T' indicates the FAT table is being loaded
  'D' indicates the FAT table was loaded properly
  'F' means the file is being located (or Found, hence the F)
  'L' means the file is being loaded
  </code></pre>
  
  <p>(I would have used <em>actual</em> debug messages, but the 512-byte limit is pretty gruesome.)</p>
  
  <p>So, the difference is that one is a USB stick, and one is a (virtual) floppy disk. They both have the <em>exact</em> same information loaded on each, including the BPB. However, one works, and one doesn't. Here is the main part of my code for loading a sector (using ah 02h/int 13h, which I heard worked properly for a USB):</p>
  
  <pre><code>ReadSectors:
      mov di, 0x0005                  ; How many times should we retry the read?
  
  ReadSectors.loop:
      ; DEBUG
      push ax
      mov ah, 0eh
      mov al, '-'
      int 10h
      pop ax
      push ax
      push bx
      push cx
      call LBAToCHS
      mov ah, 02h                     ; Set the interrupt to the 
                                      ; 'read sector' function
      mov al, 1                       ; Only read one sector
      mov ch, byte[chs.track]         ; The track to read from
      mov cl, byte[chs.sector]        ; The sector to read from
      mov dh, byte[chs.head]          ; The head to read from
      mov dl, byte[_bpb.driveNumber]  ; The drive to read from
      int 13h                         ; Call our 'disk IO' interrupt
      jnc ReadSectors.success         ; If we successfully read the data, 
                                      ; we don't have to try again
      mov ah, 00h                     ; Set the interrupt to the 
                                      ; 'reset disk' function
      int 13h                         ; Call our 'disk IO' interrupt
      dec di                          ; Decrement our error counter
      pop cx
      pop bx
      pop ax
      jnz ReadSectors.loop            ; Try again if we've failed
      jmp ReadSectors.fail            ; RED ALERT
  </code></pre>
  
  <p>(The full source, including the BPB, can be found on Pastebin (<a href="http://pastebin.com/SeUm7xu6" rel="noreferrer">http://pastebin.com/SeUm7xu6</a>)</p>
  
  <p>I've overcome a number of issues with Assembly so far, but this one has me stumped. Hopefully, I can get past the bootloader and abstract the file IO as soon as possible.</p>
  
  <p>Any suggestions would be greatly appreciated. Thanks in advance!</p>