Page 2 of 2

Re: How to make bootloader for USB flash memory ?

Posted: Mon Nov 25, 2013 9:14 am
by Octocontrabass
Pem3 wrote:
Octocontrabass wrote:The first bootloader (MBR) reads the partition table to find the partition where you've put the second bootloader (VBR; your code).
Ok, clear now. And how about bytes -> code. Is there available asembler code of these bytes (1st bootloader) ? Dunno how to find it by keywords in google, which keywords to use.
If you use Microsoft's MBR, look for "Windows MBR disassembly".
Pem3 wrote:Still don't understand what do you mean by fat32 code ;)

90-509; boot code
That part.
Pem3 wrote:Because it's not working. I pasted my code from 90 byte (0x5a) and same result - message "reebot and select proper boot device"
Hmm, does your computer run Windows 8? You must disable Secure Boot.

Can you show us sector 0 from your flash drive?

Re: How to make bootloader for USB flash memory ?

Posted: Mon Nov 25, 2013 9:41 am
by Combuster
Virtualbox (and whatever other emulator) also runs on a PC, which makes matters confusing. If you want the contrast, use something like "actual hardware" or "real machine".

My virtualbox (typing from it!) also has the integrated option to use any USB device directly without any need of additional software, interestingly providing a contradiction to your earlier notion (which probably means you're doing something wrong).

Re: How to make bootloader for USB flash memory ?

Posted: Mon Nov 25, 2013 1:07 pm
by Pem3
Octocontrabass wrote:
Pem3 wrote:
Octocontrabass wrote:The first bootloader (MBR) reads the partition table to find the partition where you've put the second bootloader (VBR; your code).
Ok, clear now. And how about bytes -> code. Is there available asembler code of these bytes (1st bootloader) ? Dunno how to find it by keywords in google, which keywords to use.
If you use Microsoft's MBR, look for "Windows MBR disassembly".
Pem3 wrote:Still don't understand what do you mean by fat32 code ;)

90-509; boot code
That part.
Pem3 wrote:Because it's not working. I pasted my code from 90 byte (0x5a) and same result - message "reebot and select proper boot device"
Hmm, does your computer run Windows 8? You must disable Secure Boot.

Can you show us sector 0 from your flash drive?

Nope, I use Windows 7. What is secure boot ? Why to disable it for ?

I will give you all i have atm and on what i am testing.

I have prepared usb flash with such commands:
Microsoft Windows [Version 6.1.7601]
Copyright (c) 2009 Microsoft Corporation. All rights reserved.

C:\Users\Admin>diskpart

Microsoft DiskPart version 6.1.7601
Copyright (C) 1999-2008 Microsoft Corporation.
On computer: ADMIN-PC

DISKPART> list disk

Disk ### Status Size Free Dyn Gpt
-------- ------------- ------- ------- --- ---
Disk 0 Online 79 GB 0 B
Disk 1 Online 951 MB 0 B

DISKPART> select disk 1

Disk 1 is now the selected disk.

DISKPART> clean

DiskPart succeeded in cleaning the disk.

DISKPART> create partition primary

DiskPart succeeded in creating the specified partition.

DISKPART> select partition 1

Partition 1 is now the selected partition.

DISKPART> active

DiskPart marked the current partition as active.

DISKPART> format FS=FAT32

100 percent completed

DiskPart successfully formatted the volume.

DISKPART> assign

DiskPart successfully assigned the drive letter or mount point.

DISKPART>
After that i have compiled my simple bootloader code:

Code: Select all

org 0
jmp btcode
nop

times 87 db 0ffh

btcode:

mov si, message+7C00h


writestr:
mov byte al, [si]

cmp al, 0h
je jobDone

mov ah, 0eh
mov bx, 07h
int 10h

inc si

jmp writestr
jobDone:


message 			db 'Bootloader is working', 00h

times 510-($-$$) db 0
dw 0xAA55
After compilation process i get such binary code:

Code: Select all

EB 58 90 FF FF FF FF FF FF FF FF FF FF FF FF FF FF FF FF FF FF FF FF FF FF FF FF FF FF FF FF FF FF FF FF FF FF FF FF FF FF FF FF FF FF FF FF FF FF FF FF FF FF FF FF FF FF FF FF FF FF FF FF FF FF FF FF FF FF FF FF FF FF FF FF FF FF FF FF FF FF FF FF FF FF FF FF FF FF FF BE 6D 7C 8A 04 3C 00 74 0A B4 0E BB 07 00 CD 10 46 EB F0 42 6F 6F 74 6C 6F 61 64 65 72 20 69 73 20 77 6F 72 6B 69 6E 67 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 55 AA
After that (now) i am pasting you current state of 2 bootloaders:

1st:

Code: Select all

33 C0 8E D0 BC 00 7C 8E C0 8E D8 BE 00 7C BF 00 06 B9 00 02 FC F3 A4 50 68 1C 06 CB FB B9 04 00 BD BE 07 80 7E 00 00 7C 0B 0F 85 0E 01 83 C5 10 E2 F1 CD 18 88 56 00 55 C6 46 11 05 C6 46 10 00 B4 41 BB AA 55 CD 13 5D 72 0F 81 FB 55 AA 75 09 F7 C1 01 00 74 03 FE 46 10 66 60 80 7E 10 00 74 26 66 68 00 00 00 00 66 FF 76 08 68 00 00 68 00 7C 68 01 00 68 10 00 B4 42 8A 56 00 8B F4 CD 13 9F 83 C4 10 9E EB 14 B8 01 02 BB 00 7C 8A 56 00 8A 76 01 8A 4E 02 8A 6E 03 CD 13 66 61 73 1C FE 4E 11 75 0C 80 7E 00 80 0F 84 8A 00 B2 80 EB 84 55 32 E4 8A 56 00 CD 13 5D EB 9E 81 3E FE 7D 55 AA 75 6E FF 76 00 E8 8D 00 75 17 FA B0 D1 E6 64 E8 83 00 B0 DF E6 60 E8 7C 00 B0 FF E6 64 E8 75 00 FB B8 00 BB CD 1A 66 23 C0 75 3B 66 81 FB 54 43 50 41 75 32 81 F9 02 01 72 2C 66 68 07 BB 00 00 66 68 00 02 00 00 66 68 08 00 00 00 66 53 66 53 66 55 66 68 00 00 00 00 66 68 00 7C 00 00 66 61 68 00 00 07 CD 1A 5A 32 F6 EA 00 7C 00 00 CD 18 A0 B7 07 EB 08 A0 B6 07 EB 03 A0 B5 07 32 E4 05 00 07 8B F0 AC 3C 00 74 09 BB 07 00 B4 0E CD 10 EB F2 F4 EB FD 2B C9 E4 64 EB 00 24 02 E0 F8 24 02 C3 49 6E 76 61 6C 69 64 20 70 61 72 74 69 74 69 6F 6E 20 74 61 62 6C 65 00 45 72 72 6F 72 20 6C 6F 61 64 69 6E 67 20 6F 70 65 72 61 74 69 6E 67 20 73 79 73 74 65 6D 00 4D 69 73 73 69 6E 67 20 6F 70 65 72 61 74 69 6E 67 20 73 79 73 74 65 6D 00 00 00 63 7B 9A 00 00 00 00 00 00 80 01 02 00 0B FE 3F 1D 40 00 00 00 40 6E 07 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 55 AA
2nd:

Code: Select all

EB 58 90 4D 53 44 4F 53 35 2E 30 00 08 04 26 06 02 00 00 00 00 F8 00 00 3F 00 FF 00 40 00 00 00 40 6E 07 00 ED 00 00 00 00 00 00 00 02 00 00 00 01 00 06 00 00 00 00 00 00 00 00 00 00 00 00 00 80 00 29 D7 BD 75 7C 4E 4F 20 4E 41 4D 45 20 20 20 20 46 41 54 33 32 20 20 20 33 C9 8E D1 BC F4 7B 8E C1 8E D9 BD 00 7C 88 4E 02 8A 56 40 B4 41 BB AA 55 CD 13 72 10 81 FB 55 AA 75 0A F6 C1 01 74 05 FE 46 02 EB 2D 8A 56 40 B4 08 CD 13 73 05 B9 FF FF 8A F1 66 0F B6 C6 40 66 0F B6 D1 80 E2 3F F7 E2 86 CD C0 ED 06 41 66 0F B7 C9 66 F7 E1 66 89 46 F8 83 7E 16 00 75 38 83 7E 2A 00 77 32 66 8B 46 1C 66 83 C0 0C BB 00 80 B9 01 00 E8 2B 00 E9 2C 03 A0 FA 7D B4 7D 8B F0 AC 84 C0 74 17 3C FF 74 09 B4 0E BB 07 00 CD 10 EB EE A0 FB 7D EB E5 A0 F9 7D EB E0 98 CD 16 CD 19 66 60 80 7E 02 00 0F 84 20 00 66 6A 00 66 50 06 53 66 68 10 00 01 00 B4 42 8A 56 40 8B F4 CD 13 66 58 66 58 66 58 66 58 EB 33 66 3B 46 F8 72 03 F9 EB 2A 66 33 D2 66 0F B7 4E 18 66 F7 F1 FE C2 8A CA 66 8B D0 66 C1 EA 10 F7 76 1A 86 D6 8A 56 40 8A E8 C0 E4 06 0A CC B8 01 02 CD 13 66 61 0F 82 75 FF 81 C3 00 02 66 40 49 75 94 C3 42 4F 4F 54 4D 47 52 20 20 20 20 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 0D 0A 52 65 6D 6F 76 65 20 64 69 73 6B 73 20 6F 72 20 6F 74 68 65 72 20 6D 65 64 69 61 2E FF 0D 0A 44 69 73 6B 20 65 72 72 6F 72 FF 0D 0A 50 72 65 73 73 20 61 6E 79 20 6B 65 79 20 74 6F 20 72 65 73 74 61 72 74 0D 0A 00 00 00 00 00 AC CB D8 00 00 55 AA
Now i modify 2nd bootloader by copying bytes from my bootloader (starting from 5ah / byte number 90) and that's how it look like:

Code: Select all

EB 58 90 4D 53 44 4F 53 35 2E 30 00 08 04 26 06 02 00 00 00 00 F8 00 00 3F 00 FF 00 40 00 00 00 40 6E 07 00 ED 00 00 00 00 00 00 00 02 00 00 00 01 00 06 00 00 00 00 00 00 00 00 00 00 00 00 00 80 00 29 D7 BD 75 7C 4E 4F 20 4E 41 4D 45 20 20 20 20 46 41 54 33 32 20 20 20 BE 6D 7C 8A 04 3C 00 74 0A B4 0E BB 07 00 CD 10 46 EB F0 42 6F 6F 74 6C 6F 61 64 65 72 20 69 73 20 77 6F 72 6B 69 6E 67 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 55 AA

Where is mistake ?

Re: How to make bootloader for USB flash memory ?

Posted: Mon Nov 25, 2013 1:24 pm
by DavidCooper
Are you sure you're writing your code to the right place on the disk? A common mistake people make when they're new to HxD is to open the disk as a logical disk (i.e. a partition) rather than a physical disk (the whole disk), then they write their code to the first sector of the partition instead of to the first sector of the disk. It seems unlikely that you have made this mistake as you identified something that appeared to be a partition table entry in the right place, but it's worth double checking this just in case it only looked like a partition table by chance.

If this turns out to be the problem, don't lose the partition table data - you'll probably want to keep that partition exactly as it is. Copy the entire sector so that you can restore it easily.

Re: How to make bootloader for USB flash memory ?

Posted: Mon Nov 25, 2013 3:26 pm
by Pem3
DavidCooper wrote:Are you sure you're writing your code to the right place on the disk? A common mistake people make when they're new to HxD is to open the disk as a logical disk (i.e. a partition) rather than a physical disk (the whole disk), then they write their code to the first sector of the partition instead of to the first sector of the disk. It seems unlikely that you have made this mistake as you identified something that appeared to be a partition table entry in the right place, but it's worth double checking this just in case it only looked like a partition table by chance.
wrong
If this turns out to be the problem, don't lose the partition table data - you'll probably want to keep that partition exactly as it is. Copy the entire sector so that you can restore it easily.
Well I have written my code to 1st sector of partition (opened as logical in HxD, 2nd bootloader - right ?). Is it wrong ? As I have understood by what you have written above, then it's totally wrong, hmm ? Dunno what to think about it now. Am I wrong or you ?

Re: How to make bootloader for USB flash memory ?

Posted: Mon Nov 25, 2013 3:50 pm
by DavidCooper
Pem3 wrote:Well I have written my code to 1st sector of partition (opened as logical in HxD, 2nd bootloader - right ?). Is it wrong ? As I have understood by what you have written above, then it's totally wrong, hmm ? Dunno what to think about it now. Am I wrong or you ?
If you read what I said more carefully, you'll notice that I told you that the logical drive is the partition. If you want your code to run, you can indeed put it there, but you'll then need to make sure you have a proper MBR in the first sector (sector zero) of the physical drive containing code capable of finding the partition and booting your VBR. The partition table entry also needs to indicate that the partition contains a bootable VBR, so the first byte of that table entry needs to be set to 80h. The MBR code will then load in the VBR and hand control over to it, assuming the MBR contains valid code - it may well not do this though, because typically the first sector on a flash drive is empty apart from the partition table and contains no code capable of booting a VBR.

If there is no MBR code, you can put your bootloader in the MBR for now instead and see if it boots up. It should do, because if the BIOS is set up to boot from USB, all it will care about is that the boot sig is there and if so it will run your code. If you have more than one USB port though, it may be that only one of them will work for this.

[Edit: corrected bit in bold - I wrote "logical sector" instead of "logical drive" at the first attempt.]

Edit 2: I notice now that you know you've put it at the start of a partition, but it won't boot from there without a proper MBR at sector 0 of the physical drive with MBR code in it and a partition table entry set to label the partition as bootable. That's what you need to fix, but if you want to test quickly whether you can get your machine to boot from USB at all, you can try using your bootloader code in place of MBR code (sector 0 of the physical drive) and it ought to boot up and display your message.

Re: How to make bootloader for USB flash memory ?

Posted: Mon Nov 25, 2013 8:01 pm
by Octocontrabass
You did not set CS or DS, so addresses in your code are wrong. Do something like this:

Code: Select all

btcode:
    jmp 7c0h:btcode2 ; set cs to 07C0h

btcode2:
    mov ax, 7c0h
    mov ds, ax         ; set ds to 07C0h

    mov si, message    ; don't need to add 7C00h anymore
(If you use ".org 0", you should set cs and ds to 7c0h. If you use ".org 7c00h", you should set cs and ds to 0.)


You also do not halt at the end of your code, so CPU continues executing until it crashes. Halt the CPU in an infinite loop:

Code: Select all

jobDone:
    hlt
    jmp jobDone
Try again with these changes.

Re: How to make bootloader for USB flash memory ?

Posted: Tue Nov 26, 2013 12:36 am
by mrstobbe
Pem3 wrote:I don't understand you. Shall I understand your answer as laughing of my person or what ? Have I written something funny or wrong ? I know that my question in this topic is funny as for person who was able to write in assembler a little advanced software like boot-loader and kernel, but so far I haven't worked directly with system files and so far I didn't know how is it working, yea have fun of that. I really don't understand your attitude.
Well I didn't understand you. I mistakenly believed you where using the term PC as a synonym for windows (PC and VirtualBox, or Windows and VirtualBox, meaning VirtualBox on Windows), so I apologize :)

Re: How to make bootloader for USB flash memory ?

Posted: Tue Nov 26, 2013 6:14 am
by Pem3
Problem seems to be solved. I changed USB device. My previous usb device was old mp3 device with USB 1.0 or 1.1, it was probably reason (but why ?) of not working (default file system for this usb was fat12 or fat16, dunno, it was discovered such by windows format manager).

Really dunno what was wrong before. I did same steps with new device and all is clear now (working fine). Working fine both in real machine and virtual box one.

Thank you guys for supporting me, it was very precious, thank you.


@ mrstobbe: Peace my friend :wink:

@Octocontrabass: Yea, you are right. I have noticed it just after bootloader became discoverable. It's fine now. Thank you.