1

I am on Ubuntu and I made a simple bootloader that just simply print a character.

It works well on qemu-system-i386

It looks like this:

start:
    mov ah, 0x0e
    mov al, '~'
    int 0x10
    jmp $
times 510-($-$$) db 0
db 0x55
db 0xaa

And I build with

fasm loader.asm img.bin

then check if my usb is available:

lsblk

after check that my usb is available at /dev/sdc, I ran

sudo dd if=img.bin of=/dev/sdc 
sync

And finally check if dd wrote well:

dd if=/dev/sdc bs=512 count=1 > f
ndisasm -b 16 f

above ndisasm print:

00000000  B40E              mov ah,0xe
00000002  B07E              mov al,0x7e
00000004  CD10              int 0x10
00000006  EBFE              jmp short 0x6
00000008  0000              add [bx+si],al
0000000A  0000              add [bx+si],al
repeats 0000...
000001FE  55                push bp
000001FF  AA                stosb

Now, I put this usb to real laptop with 32bit processor(Pentium M).

but it reboots again and again.

So, my question is: can I guess my usb broken?

I don't know if this related with my problem but.. here is one more thing: When I ran follow commands without usb inserted,

dd if=/dev/sdc bs=512 count=1 > f
ndisasm -b 16 f

above ndisasm print:

00000000  B40E              mov ah,0xe
00000002  B07E              mov al,0x7e
00000004  CD10              int 0x10
00000006  EBFE              jmp short 0x6
00000008  0000              add [bx+si],al
0000000A  0000              add [bx+si],al
repeats 0000...
000001FE  55                push bp
000001FF  AA                stosb

as if I inserted usb memory stick.

Will you give me some advice?

Thanks. (I tried hard to formulate a clear, and useful question. If it's not, please let me know.)

EDIT: I tested with BPB. The source code looks like this(nop added):

jmp start
nop
bpbOEM          db "TEST    "
bpbBytesPerSector:      DW 512
bpbSectorsPerCluster:   DB 1
bpbReservedSectors:     DW 1
bpbNumberOfFATs:    DB 2
bpbRootEntries:     DW 224
bpbTotalSectors:    DW 2880
bpbMedia:           DB 0xF0
bpbSectorsPerFAT:   DW 9
bpbSectorsPerTrack:     DW 18
bpbHeadsPerCylinder:    DW 2
bpbHiddenSectors:   DD 0
bpbTotalSectorsBig:     DD 0
bsDriveNumber:          DB 0
bsUnused:           DB 0
bsExtBootSignature:     DB 0x00
bsSerialNumber:         DD 0x00000000
bsVolumeLabel:          DB "           "
bsFileSystem:           DB "        "
start:
    mov ah, 0x0e
    mov al, '~'
    int 0x10
    jmp $
times 510-($-$$) db 0
db 0x55
db 0xaa

I ran:

fasm loader.asm img.bin
lsblk
sudo dd if=img.bin of=/dev/sdc 
sync
dd if=/dev/sdc bs=512 count=1 > f
ndisasm -b 16 f

and it prints:

00000000  EB3B              jmp short 0x3d
00000002  54                push sp
00000003  45                inc bp
00000004  53                push bx
00000005  54                push sp
00000006  2020              and [bx+si],ah
00000008  2020              and [bx+si],ah
0000000A  0002              add [bp+si],al
0000000C  0101              add [bx+di],ax
0000000E  0002              add [bp+si],al
00000010  E000              loopne 0x12
00000012  40                inc ax
00000013  0BF0              or si,ax
00000015  0900              or [bx+si],ax
00000017  1200              adc al,[bx+si]
00000019  0200              add al,[bx+si]
0000001B  0000              add [bx+si],al
0000001D  0000              add [bx+si],al
0000001F  0000              add [bx+si],al
00000021  0000              add [bx+si],al
00000023  0000              add [bx+si],al
00000025  0000              add [bx+si],al
00000027  0000              add [bx+si],al
00000029  0020              add [bx+si],ah
0000002B  2020              and [bx+si],ah
0000002D  2020              and [bx+si],ah
0000002F  2020              and [bx+si],ah
00000031  2020              and [bx+si],ah
00000033  2020              and [bx+si],ah
00000035  2020              and [bx+si],ah
00000037  2020              and [bx+si],ah
00000039  2020              and [bx+si],ah
0000003B  2020              and [bx+si],ah
0000003D  B40E              mov ah,0xe
0000003F  B07E              mov al,0x7e
00000041  CD10              int 0x10
00000043  EBFE              jmp short 0x43
repeats 0000...
000001FD  0055AA            add [di-0x56],dl

And the result was same. The real machine reboots again and again.

EDIT2: This is a bootloader that beeps. It worked on different usb, same computer.

bits 16
org 0x7c00
jmp main
main:
    mov ax, 3
    int 0x10
    mov ah, 0x0e
    mov al, 'A'
    int 0x10
    mov al, 0xb6
    out 0x43, al
    mov al, 0xa9
    out 0x42, al
    mov al, 0x04
    out 0x42, al
    in al, 0x61
    mov ah, al
    or al, 011b
    test al, ah
    je done
    out 0x61, al
done:
    jmp $

    times 510-($-$$) db 0
    db 0x55
    db 0xaa
times 1474560 - ($-$$) db 0
Taeyun
  • 211
  • 2
  • 16
  • If the BIOS is set to boot the USB drive as a Floppy type defice (USB FDD) then you may need to use a BIOS Parameter Block at the beginning of your bootloader for it to work as expected. – Michael Petch May 16 '17 at 14:21
  • @MichaelPetch Thanks for your reply. In the past, this machine works without BIOS Parameter Block, do I still need it? – Taeyun May 16 '17 at 14:24
  • 1
    Are you saying **you** have written your own bootloaders in the past and they all worked on the real hardware and this one doesn't? USB on real hardware is different than booting off a mere floppy disk image in QEMU/Virtual Box/Bochs. These days id the USB is on real hardware is using Floppy Disk Emulation (FDD) then it is more than likely you will need a BPB – Michael Petch May 16 '17 at 14:25
  • @MichaelPetch Yes. I wrote a bootloader that beeps in the past. and they worked on this real hardware(using usb). And this doesn't. Will you be give me some more advice? – Taeyun May 16 '17 at 14:28
  • Show us your code (that beeps) that when placed on the same USB drive. – Michael Petch May 16 '17 at 14:30
  • I've written about these USB / Real Hardware issues in the past. Recently I wrote [this SO Answer](http://stackoverflow.com/a/43787939/3857942) with a section _Real Hardware / USB / Laptop Issues_ . That has _NASM_ code with a fake BPB that should be usable with FASM with minor tweaks. – Michael Petch May 16 '17 at 14:31
  • @MichaelPetch bootloader that beeps was tested with other usb stick. Do you still want me to upload it?(I edited to post BPB version) – Taeyun May 16 '17 at 14:36
  • That BPB is wrong. There should be a `NOP` right after `jmp start` – Michael Petch May 16 '17 at 14:39
  • So just out of curiosity. You claim the one that beeped was on a different USB device. What happens if you put your new code on the USB device that apparently worked? – Michael Petch May 16 '17 at 14:44
  • If you place the code that beeps onto your current USB device does it work? – Michael Petch May 16 '17 at 14:45
  • @MichaelPetch I updated the question. I placed nop instruction right after jmp start. I also tested beeping bootloader. both reboots. – Taeyun May 16 '17 at 14:47
  • So your beep code doesn't work. – Michael Petch May 16 '17 at 14:47
  • You should boot into your machines BIOS and see what type of emulation is being done on the USB devices. Usually it'll have options that may include things like USB FDD (floppy), USB HDD, USB Zip, etc. You want one that emulates a floppy (FDD) – Michael Petch May 16 '17 at 14:49
  • Before removing the USB device I'd install the `udisks` package into Ubuntu and then use the command `sudo udisks --detach /dev/sdX` where `X`is your device letter (in your case it appears to be `c` to safely unmount a USB device to make sure everything gets written as expected. – Michael Petch May 16 '17 at 14:53
  • @MichaelPetch I just found that usb stick that worked before has same issue now. this is strange.. the usb stick I used at the time of this posting was 4GB. and usb stick worked before was 64GB. I installed Ubuntu live iso to 64GB in the past. I tried the same code and build routine to 64GB and it boots into Ubuntu live. As if I didn't dd to /dev/sdc. But I confirmed it with **lsblk** – Taeyun May 16 '17 at 14:55
  • Did you go into your BIOS (at bootup there is a keystroke that will take you into the BIOS config) to see what USB emulation is being used? – Michael Petch May 16 '17 at 14:57
  • As well I also highly recommend `udisks` to properly flush and prepare to eject (you removing it) a USB device on Linux. – Michael Petch May 16 '17 at 14:57
  • @MichaelPetch Yes, it says, 1. USB HDD 2. ALL USB CDROM 3. USB FDD 4. IDE HDD0 – Taeyun May 16 '17 at 14:59
  • Don't know about that BIOS but it seems to suggest it is doing USB HDD emulation first. I want you to try getting it to do USB FDD emulation when it boots. – Michael Petch May 16 '17 at 15:01
  • @MichaelPetch I am sorry but I can't find udisks and I can't find package named udisks. sorry. – Taeyun May 16 '17 at 15:04
  • @MichaelPetch I tried udisksctl unmount -b /dev/sdc && udisksctl power-off -b /dev/sdc. and it does change nothing. I am sorry that you spend too much time on this problem.. – Taeyun May 16 '17 at 15:11
  • Did you try to do the boot using the USB FDD emulation. I commented earlier that it appears you may be booting using USB HDD (hard disk emulation) which may be a problem. And whatever you test make sure you have a working BPB. Don't use the code without one. – Michael Petch May 16 '17 at 15:11
  • @MichaelPetch I set Boot priority order to 1. USB FDD 2. IDE HDD 0. And the computer cannot boot into usb at all. It doesn't even reboots. I just found that gparted says Libparted Error: end of file while reading /dev/sdc – Taeyun May 16 '17 at 15:18
  • @MichaelPetch Thank you very much for your helps. I guess usb is not broken because I can write something on windows machine.(diskpart, new volume, and format) just my guess.. Thanks anyway! – Taeyun May 16 '17 at 15:26
  • At this point I can't do much without the physical PC in front of me. Someone else may have other ideas. It is possible in your case it doesn't want a BPB but it is looking for partition table at the end of your boot sector and then looking for an active boot partition and then reading the Volume Boot Record of the bootable partition and booting code from there. – Michael Petch May 16 '17 at 15:27
  • A test you might consider is under Windows format the USB device. That should create an MBR with a partition table. Then put the USB stick onto the Linux box. Then rather than overwriting the MBR (sector 0) using DD with an output device of /dev/sdc - you specify the partition (likely /dev/sdc1). This would have the effect of altering the Volume Boot Record of the partition. In this case the code you write to the VBR should not contain a BPB (BPB usually only applies to the MBR) – Michael Petch May 16 '17 at 15:32
  • One observation is that the USB device being on /dev/sdc . Usually a hard drive is at /dev/sdc unless you have booted Linux from a CD-ROM? I guess I am asking if you are sure /dev/sdc is actually your USB device? – Michael Petch May 16 '17 at 15:43
  • @MichaelPetch I just found that on another linux box, it says 4GB usb contains first 4 bytes of 0x33, 0xc0, 0x8e, 0xd0. which is different from linux box I am at now: 0xeb, 0x3c, 0x90, 0x54. linux box I am at now somehow falsely report what usb contains. I wonder if lsblk, sync, dd, and ndisasm are enough to confirm that I wrote to usb. – Taeyun May 16 '17 at 15:51
  • @MichaelPetch I think /dev/sdc is usb. because when I remove the usb and lsblk, sdc is gone. – Taeyun May 16 '17 at 15:52
  • Okay regarding /dev/sdc. Was just making sure. After writing did you use the `udisksctl` before removing the USB stick and moving it to another system? And are you sure on the other Linux Box you are reading the boot sector from the correct device? – Michael Petch May 16 '17 at 15:55
  • @MichaelPetch On the other linux box, I used lsblk to check if my usb is available. it says /dev/sdd so I dd if=/dev/sdd bs=10 count=1 | hexdump -C. It seems my current linux system is the source of the problem. – Taeyun May 16 '17 at 16:01
  • I have only one other hunch. At the bottom of the bootloader after `db 0xaa` place this line at the end `times 1474560 - ($-$$) db 0` . Assemble your bootloader write it the way you have take it to the other PC. I'm curious if the bootloader looks correct. – Michael Petch May 16 '17 at 16:04
  • @MichaelPetch I reboot current linux machine, and ran the same commands. this time, my system reports that it cannot do dd if=/dev/sdc bs=512 count=1 > f because of permission. so I added sudo. and it suddenly print correct character on the laptop. I am confused that I cannot guarantee that I wrote to usb with lsblk, dd, sync, ndisasm. Thanks anyway. Please post answer so that I can choose. – Taeyun May 16 '17 at 16:18
  • HAHA, It had crossed my mind that it might be a permission issue (not using root privs) but you would have been receiving an error (very odd you didn't). You can answer your own question here if you wish. Glad you got it going. – Michael Petch May 16 '17 at 16:21
  • @MichaelPetch Just for curiosity, is it not sufficient to use lsblk, dd, sync, ndisasm to make sure? And I don't want to answer my own question. – Taeyun May 16 '17 at 16:23
  • Usually it would be but there is something else going on. The fact after a reboot things worked differently has me puzzled as to how anything was really working previously. I can not say what yor system was doing prior to the reboot that then required you to use `sudo` (which you should have had to do all along) – Michael Petch May 16 '17 at 16:27
  • @MichaelPetch Please write an answer. I am in endangered in question ban so I don't want to answer my own question. T_T – Taeyun May 16 '17 at 16:33
  • I don't know what the cause of your problem is, so I can't give an adequate answer. The fact a reboot made it work as expected has me clueless. – Michael Petch May 16 '17 at 16:34
  • @MichaelPetch OK. then I will leave it as it is now. Thank you very much anyway. – Taeyun May 16 '17 at 16:37

0 Answers0