-2

I was writing boot loader and stuck with a 545 byte file, which I have no idea how to load. If it is a 512 byte file stored in the 2nd sector, it can be easily loaded onto the memory. But this seems to be a little weird to me.

How do I do this?

Michael Petch
  • 46,082
  • 8
  • 107
  • 198
  • Simply load the second sector in your first 512 byte MBR boot loader. – David Hoelzer Apr 12 '16 at 19:35
  • @Praveen - If you have another question, please ask that as a new question. – Bo Persson Apr 14 '16 at 09:03
  • Are you saying that your bootloader is 545 bytes?? It would help if you posted the code that ends up being 545 bytes. At first I thought you were talking about a second stage, but now I wonder if your bootsector itself is 545. Post your boot sector code in your question and we might have a better understanding of your problem – Michael Petch Apr 14 '16 at 17:47
  • 2
    Since you are on Windows, and if you followed my previous information it should be as simple as doing `dd if=stage2.bin od=e: seek=1` . Default block size for DD is 512, so `seek=1` skips past the first 512 bytes and starts writing the entire contents of stage2.bin. If it is greater than 512 it will write into the next sector (sector 3). **`e:` would be replaced with the drive letter of your USB!** (i use e: as an example). `stage2.bin` is whatever name is for your file that happens to be 545 bytes in size. – Michael Petch Apr 14 '16 at 18:29
  • I am going to load first 512 bytes into memory location 0x1000, and do I need to load the 3rd ie: Remaining of stage 2 into location 0x1200? – Praveen Kumar Apr 14 '16 at 18:47
  • Just use [int 13h/ah=2](https://en.wikipedia.org/wiki/INT_13H#INT_13h_AH.3D02h:_Read_Sectors_From_Drive) to read into memory at 0x1000 but tell it you want to read two sectors by placing 2 into the _AL_ register. Since you want to start reading from sector 2 of the device you put 2 into register _CL_. `int 13h/ah=2` with _AL_ =2 will read 2 full sectors (1024 bytes)into memory (it can't read exactly 545 bytes). It read extra bytes but that doesn't harm anything. – Michael Petch Apr 14 '16 at 18:58
  • Ok, I got some problem in my sys. So could you tell me the dd command to write the file to disk.img in bochs and my second stage is now 740k in size – Praveen Kumar Apr 14 '16 at 19:45
  • So that I can test it in Bochs – Praveen Kumar Apr 14 '16 at 19:52
  • Anyone here can help me out of this? Mr.Michael Petch? – Praveen Kumar Apr 14 '16 at 20:04
  • 1
    For a disk image of 1440k (1.44mb floppy). You first create a the full disk image with `dd if=/dev/zero of=disk.img bs=1024 count=1440` . (if you prefer a 720k floppy change count=1440 to count=720). Next write your bootloader to sector 1 with something like `dd if=boot.bin of=disk.img conv=notrunc`, then place the second stage starting at sector 2 with `dd if=stage2.bin of=disk.img conv=notrunc seek=1` . That image should be bootable with Bochs. – Michael Petch Apr 15 '16 at 01:44
  • Ok it works in Bochs, but when I tried to write to USB using DD it shows "Error writing file: 5 access is denied". I was using the command "DD if=stage2.bin od=d: seek=1" – Praveen Kumar Apr 16 '16 at 13:17
  • Anyone plss help me to solve this problem...sir.. – Praveen Kumar Apr 16 '16 at 13:57
  • If you want to update certain parts of a disk (like the MBR) you have to run _DD_ with administrator privileges. So you'll have to open up a Windows command prompt with Administrator privileges and then run the _DD_ command. – Michael Petch Apr 16 '16 at 15:39
  • I opened it with Administrator privileges as you said always..but the problem persists – Praveen Kumar Apr 16 '16 at 16:15
  • Maybe temporarily turn off your real time virus scanner if you have one. Load a command prompt with administrator privileges. Run this command `diskpart` then do `list disk` . Find the disk # of your USB drive. The use the command `select disk #` where # is the USB drive number. Then do the command `clean` . Then exit diskpart with `exit`. Then try your `dd` commands. – Michael Petch Apr 16 '16 at 17:39
  • When I issue clean command it says "access is denied" in privileged mode – Praveen Kumar Apr 16 '16 at 18:41
  • I tried in 2 USB's and found the same.. – Praveen Kumar Apr 16 '16 at 18:42
  • Please solve this problem.. – Praveen Kumar Apr 16 '16 at 19:06
  • Now a new problem, I just posted the screenshot above. – Praveen Kumar Apr 16 '16 at 19:26
  • You should be asking a new question because your question wasn't originally about how to write this to USB. – Michael Petch Apr 16 '16 at 19:27
  • Now its ok , right? I just edited my question – Praveen Kumar Apr 16 '16 at 19:30
  • 1
    No you should be asking a NEW question, not adding onto this one. – Michael Petch Apr 16 '16 at 19:30
  • Sorry, but I cant ask a new one for atleast 7 days, due to my poor question standards.. – Praveen Kumar Apr 16 '16 at 19:33
  • Please solve this problem, I will clear everything.. – Praveen Kumar Apr 16 '16 at 19:39

1 Answers1

3

Regarding your deleted question, How do I embedded C and Assembly in 16 bit?:

  1. Creating a "bootloader" that invokes a simple "kernel" isn't necessarily difficult ... but it's not something you can answer in a one-line question, either.

  2. The first question is - Q: What is your target platform?

    It sounds like - A: An Intel x86 CPU loaded from a FAT (or FAT32) boot media.

  3. The second question = Q: What is your "development workstation"? Are you compiling/assembling your code and creating your boot image on Windows? On Linux? "Something else"? Let's assume "linux". For many reasons, that's a "good choice".

  4. As others pointed out, this C program is not a good choice for your "kernel":

main.c =>

#include<stdio.h>

int main(int argc, char *argv[])  {
  printf("Hello world!!!");
  return 0;
}
  1. You'd need to link in a C runtime library ("CRTL") in order to get "printf()".

  2. You also need to invoke crt0.obj beforehand in order to get "main()".

  3. Even if you did so, the CRTL and crt0.obj you chose might not be compatible with 16-bit real mode.

  4. And even if you covered 1), 2) and 3), you'd still need an OS (e.g. DOS, Linux or Windows) in order to actually perform the "printf()".

SUGGESTION:

Try working through this tutorial (there are many similar examples on the web):

Community
  • 1
  • 1
paulsm4
  • 114,292
  • 17
  • 138
  • 190
  • He's developing on Windows.More information can be derived from his other question here: http://stackoverflow.com/questions/36044706/how-to-make-bootloader-to-load-the-second-sector-of-a-usb – Michael Petch Apr 16 '16 at 20:04