flat assembler
Message board for the users of flat assembler.

Index > OS Construction > bootstrap32

Author
Thread Post new topic Reply to topic
Mike Gonta



Joined: 26 Dec 2010
Posts: 243
Mike Gonta 25 Apr 2012, 00:15
32 bit protected mode FAT32 USB flash drive boot loader.

PM32 program named "kernel.bin" loaded to 1MB.
Program loaded from the root directory of the FAT32 formatted flash drive.
Register edx is the boot drive identifier as supplied by the BIOS in register dl.
Paging is enabled and the entire 4Gb space is identity mapped.
Global Descriptor Table entries for code and data are 4 Gb flat address space.
Setting up a new stack pointer is recommended.
All interrupts are initialized and enabled.
Protected Mode Interrupt Descriptor Table is located at 1000h.
BIOS interrupts are accessible from PM32 in the same manner as RM16.
Use 32 bit registers for BIOS parameters.
Segment registers not used for BIOS parameters.
32 bit registers used for BIOS address parameters.
BIOS address parameters can be anywhere in the flat 4Gb space.


Last edited by Mike Gonta on 19 Nov 2012, 22:00; edited 3 times in total
Post 25 Apr 2012, 00:15
View user's profile Send private message Visit poster's website Reply with quote
Mike Gonta



Joined: 26 Dec 2010
Posts: 243
Mike Gonta 26 Apr 2012, 00:04
Unzip bootstrap32.zip inside your qemu folder.
Run the qemu.cmd.
Code:
@echo off
set qemu=qemu-system-x86_64.exe
if not exist %SystemRoot%\SysWOW64 set qemu=qemu.exe
cd ..
%qemu% -L . -localtime -hda bootstrap32\bootstrap32.img    


Last edited by Mike Gonta on 19 Nov 2012, 22:00; edited 3 times in total
Post 26 Apr 2012, 00:04
View user's profile Send private message Visit poster's website Reply with quote
Madis731



Joined: 25 Sep 2003
Posts: 2139
Location: Estonia
Madis731 26 Apr 2012, 08:04
Its a nice bootstrap. You can build pretty much anything on top of that. Smile All the hard work is done.
Post 26 Apr 2012, 08:04
View user's profile Send private message Visit poster's website Yahoo Messenger MSN Messenger Reply with quote
Mike Gonta



Joined: 26 Dec 2010
Posts: 243
Mike Gonta 26 Apr 2012, 23:23
Use this safe DD script to transfer bootstrap32 to a FAT32 formatted any size flash drive.
Code:
  @echo off
  echo Requires a non partitioned FAT32 formatted flash drive.

  set drive=
  set /p drive=bootstrap32 flash drive letter:

  set format=N
  set /p format=Do you want to format drive %drive%:? (Y/N)
  if not %format%==Y (
    if not %format%==y (
      goto :next
    )
  )
  dd\dd.exe --filter=removable if=/dev/zero od=%drive%: bs=512 count=1 2>NUL
  echo Remove and re-insert the flash drive.
  echo (Ignore Windows message to format the drive!)
  pause

  format %drive%: /FS:FAT32 /Q

:next
  dd\dd.exe id=%drive%: of=temp bs=512 count=1 2>NUL

  dd\dd.exe if=bootstrap32.img of=temp bs=1 skip=48 seek=48 count=4 2>NUL
  dd\dd.exe if=bootstrap32.img of=temp bs=1 skip=90 seek=90 count=422 2>NUL
  dd\dd.exe if=bootstrap32.img of=temp bs=512 skip=1 seek=1 count=31 2>NUL

  dd\dd.exe --filter=removable if=/dev/zero od=%drive%: bs=512 count=1 2>NUL
  echo Remove and re-insert the flash drive.
  echo (Ignore Windows message to format the drive!)
  pause

  dd\dd.exe --filter=removable if=temp od=%drive%: 2>NUL
  del temp >NUL
  copy /Y root\*.* %drive%:
  pause    


Last edited by Mike Gonta on 19 Nov 2012, 22:01; edited 3 times in total
Post 26 Apr 2012, 23:23
View user's profile Send private message Visit poster's website Reply with quote
Mike Gonta



Joined: 26 Dec 2010
Posts: 243
Mike Gonta 03 May 2012, 20:44
The bootstrap32 now uses the volume label (the directory entry in the root directory) to obtain the file name
of the loaded PM32 kernel file. The default file name can be easily changed to match your own without editing
the boot sector as is commonly done. The volume label must be the same as the directory entry of the file.
For example KERNEL.BIN would be:
Code:
KERNEL  BIN
12345678123    
The file name is a maximum of 8 character (padded with spaces on the right) followed by an optional file
extension with a maximum of 3 characters (no period).


Last edited by Mike Gonta on 19 Nov 2012, 22:01; edited 1 time in total
Post 03 May 2012, 20:44
View user's profile Send private message Visit poster's website Reply with quote
hyperzap



Joined: 22 Apr 2011
Posts: 4
hyperzap 17 May 2012, 07:12
Hey mike,

what would be the correct method of taking control of the PIT and its interrupt? Obviously, I need to use the timer interrupt yet I want the floppy BIOS calls to remain functional.

Secondly, what tool do you use to create an image of the USB disk? running bootstrap32.cmd prepares the physical medium, but I would like to test using QEMU rather than hardware.

Edit: Also this bootloader/bootstrap32.cmd does not work on my hardware. When booting with only the USB device connected, it errors, and if there is any other boot device connected (but on a lower boot order) it just boots that instead. Im not sure if that is due to my hardware, or a failure of bootstrap32.cmd
Post 17 May 2012, 07:12
View user's profile Send private message Reply with quote
Mike Gonta



Joined: 26 Dec 2010
Posts: 243
Mike Gonta 17 May 2012, 18:30
hyperzap wrote:
what would be the correct method of taking control of the PIT and its interrupt?
Obviously, I need to use the timer interrupt yet I want the floppy BIOS calls to remain functional.
The correct method is to hook the interrupt, do your thing and then jump to the original handler.
The protected mode interrupt descriptor table is located at address 1000h.
Quote:
Secondly, what tool do you use to create an image of the USB disk?
ImDisk
Quote:
running bootstrap32.cmd prepares the physical medium, but I would like to test using QEMU rather
than hardware.
One of the advantages of using Qemu as an emulator is that Qemu will emulate the boot and run of the actual
USB flash drive (even on a PC that will not natively boot from USB).
Quote:
Also this bootloader/bootstrap32.cmd does not work on my hardware. When booting with only the USB
device connected, it errors,
There are three error messages in bootstrap32:
Drive read error! - This means that there was a failure (not likely) in the boot sector to read the flash drive.
A20 line error - This means that the A20 line could not be set.
Can't find the boot file - This message could occur in bootstrap32. If the file name is not specified it means
that there is no volume name (which is used to identify the file to be loaded. This is easy to correct, merely
change the volume name in Windows Explorer to match the name of the kernel file. On the other hand if there
is a name specified it means that kernel file is not in the root directory, or doesn't have the same name, also
easy to correct.
Any other error message either originates in the original (formatted) boot sector or from the BIOS. This means
that the process of injecting bootstrap32 failed. It is important to remove and re-insert the flash drive at the
prompt (twice if the format option is selected).
Quote:
and if there is any other boot device connected (but on a lower boot order) it just boots that instead.
Im not sure if that is due to my hardware, or a failure of bootstrap32.cmd
Most PC's require entering the BIOS setup to enable flash drive booting, although in this case it sounds like
the flash drive has not been properly prepared. A simple test would be to use HxD to transfer the bootstrap32.img
to the flash drive. This will make the flash drive appear to be only 32.5 Mb but otherwise fully useable - just
reformat to get the full capacity after testing.


Last edited by Mike Gonta on 19 Nov 2012, 22:01; edited 1 time in total
Post 17 May 2012, 18:30
View user's profile Send private message Visit poster's website Reply with quote
flash



Joined: 11 Mar 2006
Posts: 55
Location: Cuba
flash 22 May 2012, 03:31
Hi Mike!
Can you post or make public the source code? I am interested in access usb drives from scratch. Thanks!
Post 22 May 2012, 03:31
View user's profile Send private message Reply with quote
Dex4u



Joined: 08 Feb 2005
Posts: 1601
Location: web
Dex4u 22 May 2012, 10:54
If your PC supports USB booting, you can use int 13h function to read/write to USB fob in realmode.
As soon as you move to PM emulation stops.
So to read/write to USB fob from PM, you need to implement a usb stack or use some method to access real mode int 13h, in my OS for example, i switch beteen real and pmode, as soon as you go back to real mode USB read\write work again.

Not sure about the inner workings of bootstrap32
Post 22 May 2012, 10:54
View user's profile Send private message Reply with quote
flash



Joined: 11 Mar 2006
Posts: 55
Location: Cuba
flash 22 May 2012, 18:38
Thank's Dex, I want to avoid BIOS usage, despite a bunch of technical documentation about USB interface i am not able to implement any working code to acces mass storage device... Sad I am completely lost. Have you any example?
On the other hand... what drive number can be used al int 13h to point to usb device?
Thank's again
Post 22 May 2012, 18:38
View user's profile Send private message Reply with quote
Mike Gonta



Joined: 26 Dec 2010
Posts: 243
Mike Gonta 22 May 2012, 21:18
flash wrote:
I want to avoid BIOS usage
As soon as you become an expert in the usage of the BIOS, you will then be able to avoid using it. Possibly it's
the avoidance of 16bit real mode code that you are after.
bootstrap32 allows full access to the BIOS in 32bit PM. The boot loader code is entirely PM32, in fact the only 16bit
real mode setup code is in the boot sector itself. The entire loaded second stage of bootstrap32 is entirely PM32.
This is the same as the "kernel" file loaded by bootstrap32. This is made possible by the BIOS extender in bootstrap32.
flash wrote:
what drive number can be used al int 13h to point to usb device?
This is in register dl which is provided to the boot sector by the BIOS, (it's also provide by bootstrap32 to the loaded file).


Last edited by Mike Gonta on 19 Nov 2012, 22:01; edited 1 time in total
Post 22 May 2012, 21:18
View user's profile Send private message Visit poster's website Reply with quote
Dex4u



Joined: 08 Feb 2005
Posts: 1601
Location: web
Dex4u 23 May 2012, 11:34
flash wrote:
Thank's Dex, I want to avoid BIOS usage, despite a bunch of technical documentation about USB interface i am not able to implement any working code to acces mass storage device... Sad I am completely lost. Have you any example?
On the other hand... what drive number can be used al int 13h to point to usb device?
Thank's again

Your better of using bootstrap32, because the work that is needed to code a usb stack is a lot, thats why a book has been written about it.
But to your ?, some BIOS emulate has floppy, others has hdd, some both.
So i have found it best to write for both, your boot code will be the same as for a floppy or hdd.
(i use fat16 for hdd).
Now i have done lots of tests and if you emulate has a floppy you must have a BPB at start (this does make a difference) .
For hdd you can use with or without it.

If you use floppy emulation, your usb fob will be 1.44MB in size (a quick format will fix that).
Also best to use a BPB for hdd, than you can add file from win/linux in the normal way.

You could do what menuetos does and load the whole floppy img into memory from unreal mode and than use the image as a vfile sys, than writing any changes when exiting OS by writing the vfile back to disk, back in unreal mode.
Post 23 May 2012, 11:34
View user's profile Send private message Reply with quote
Mike Gonta



Joined: 26 Dec 2010
Posts: 243
Mike Gonta 23 May 2012, 17:58
Dex4u wrote:
Your better of using bootstrap32, because the work that is needed to code a usb stack is a lot,
thats why a book has been written about it.
Actually, you're better off using DexOS.

Hi Craig,
Welcome to the mutual admiration society!
It's easier to develop new code in a working environment.
bootstrap32 will boot and run hobby OS kernels and standalone code.


Last edited by Mike Gonta on 19 Nov 2012, 22:02; edited 1 time in total
Post 23 May 2012, 17:58
View user's profile Send private message Visit poster's website Reply with quote
Mike Gonta



Joined: 26 Dec 2010
Posts: 243
Mike Gonta 09 Jun 2012, 18:55
bootstrap32 will now boot and run all three versions.
The bootstrap32 code is identical, the only difference is the Bios Parameter Block in the boot sector and
the file system. Even the boot sector code is identical.


Last edited by Mike Gonta on 19 Nov 2012, 22:02; edited 1 time in total
Post 09 Jun 2012, 18:55
View user's profile Send private message Visit poster's website Reply with quote
asmdev



Joined: 21 Dec 2006
Posts: 18
asmdev 10 Jun 2012, 18:54
Probably a good idea to get rid of A20 code from Fat32 VBR, and add support for LBA, and switch to reading disk one sector(512b) per one int13h call (for legacy reasons), and remove protected mode switch

Enough critics, I'll probably attach fat32 vbr of my own.


Description:
Download
Filename: vbr_fat32.asm
Filesize: 8.15 KB
Downloaded: 431 Time(s)

Post 10 Jun 2012, 18:54
View user's profile Send private message Reply with quote
Mike Gonta



Joined: 26 Dec 2010
Posts: 243
Mike Gonta 10 Jun 2012, 19:23
asmdev wrote:
Probably a good idea to get rid of A20 code from Fat32 VBR
Hi asmdev,
Modern PC's bootup with the A20 enabled, so it matters not whether it's FAT32, FAT16 or FAT12.
For legacy purposes bootstrap32 has A20 code. It should be noted that all real mode setup code in
bootstrap32 is in (and run from) the boot sector. The A20 code first checks if the A20 line is already
enabled and only then if not enabled tries the 3 standard methods (BIOS int 15h, ax=2401h; port 92h
and port 60h).
asmdev wrote:
and add support for LBA
bootstrap32 supports LBA both in the bootloader function and the BIOS interface.
asmdev wrote:
and switch to reading disk one sector(512b) per one int13h call (for legacy reasons)
That's a bit too legacy, even for me. bootstrap32 loads the entire second stage (which is bootstrap32) from the
boot sector in one call, which means that it will cross to the other side of the first track of a legacy floppy disk
(yes, bootstrap32 supports the legacy floppy drive), but only the most ancient of PC's will have trouble with this.
asmdev wrote:
and remove protected mode switch
In bootstrap32, all real mode setup code is contained (and run from the boot sector). This includes the A20 code
(which should be run in real mode), setting up and enabling identity mapped paging, and loading the GDT and
changing to protected mode. Unlike other bootloaders, bootstrap32 is entirely PM32. This is made possible by
the built in BIOS extender.
bootstrap32 (and the user's loaded PM32 kernel) has full access to the native motherboard BIOS.
It also means that I no longer have to write any real mode code.
Need a memory map? - simply make the BIOS call in PM32.
Read and write to the IPL USB flash drive? - no problem in PM32.


Last edited by Mike Gonta on 19 Nov 2012, 22:02; edited 1 time in total
Post 10 Jun 2012, 19:23
View user's profile Send private message Visit poster's website Reply with quote
asmdev



Joined: 21 Dec 2006
Posts: 18
asmdev 10 Jun 2012, 21:18
I am just saying that you made all the mistakes that a beginner makes inside MBR or VBR. Especially when you have second stage bootloader.
Post 10 Jun 2012, 21:18
View user's profile Send private message Reply with quote
Mike Gonta



Joined: 26 Dec 2010
Posts: 243
Mike Gonta 10 Jun 2012, 21:39
asmdev wrote:
I am just saying that you made all the mistakes that a beginner makes inside MBR or VBR. Especially
when you have second stage bootloader.
Exactly!
However, after 5 years of testing and development (and countless boot sectors) these same beginner mistakes
are entirely by design.
>>> No More Real Mode Code <<< is no mistake.
It's only a legacy issue that the PC starts up in real mode. There's no reason why the PC can't startup in PM32
with BIOS access. Of course, that's the path taken by EUFI.
There's no real mode code in the bootstrap32 boot loader.
There's no real mode code in the loaded PM32 kernel.
And this is only the beginning.


Last edited by Mike Gonta on 19 Nov 2012, 22:03; edited 1 time in total
Post 10 Jun 2012, 21:39
View user's profile Send private message Visit poster's website Reply with quote
asmdev



Joined: 21 Dec 2006
Posts: 18
asmdev 10 Jun 2012, 22:14
You were embarrassing yourself during Your USB booting Quest because you didn't have enough computers. You are embarrassing yourself now. It's not in your interest to embarrass yourself for the 3rd time.

Quote:
It's only a legacy issue that the PC starts up in real mode. There's no reason why the PC can't startup in PM32
with BIOS access. Of course, that's the path taken by EUFI.
I have not made any argument regarding this. Please don't start new topic.
Post 10 Jun 2012, 22:14
View user's profile Send private message Reply with quote
smiddy



Joined: 31 Oct 2004
Posts: 557
smiddy 13 Jun 2012, 16:35
It looks good Mike, thanks for sharing man!
Post 13 Jun 2012, 16:35
View user's profile Send private message Reply with quote
Display posts from previous:
Post new topic Reply to topic

Jump to:  


< Last Thread | Next Thread >
Forum Rules:
You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum
You cannot vote in polls in this forum
You cannot attach files in this forum
You can download files in this forum


Copyright © 1999-2025, Tomasz Grysztar. Also on GitHub, YouTube.

Website powered by rwasa.