A simple bootstrap

类别:编程语言 点击:0 评论:0 推荐:

                                                A simple bootstrap
                                                                    -----2004.11.1 (C) Gr1x
To make a long story short, and enough material can be found about the PC's POST procedure, I'll just show my code and explain sth important steps, I think my source code is self-explaining enough 'cause it's short and easy to understand . I have tested it with a floppy disk,  it's risky to test it with ur harddrive. If  u did do it, I  am not responsible for any damage u made : )
Environment :(Window Platform)
     a.VMWare(Optional).If u don't want to reboot ur system many times, u should get this.
     b: Nasm.(Essential). As u need it to compile ur code . u may use Ndisasm as well to disassemble the plain binary code compiled.
    c:Visual C++ 6 or Visual Stdio .Net. As we have to write a EXE to write the bootstrap to floppy.
       U may use WinHex to write bootstrap to floppy too instead of this writing a programme.
1>Write the bootstrap procedure
----------------------------bootstrap.asm-------------------------------------------------
;***************************************************
;                     bootstrap.asm: 2004-11-1 (C) Gr1x
;                     Email:[email protected]
;****************************************************
[ORG 0]
     jmp 07C0h:start                ; remember that BIOS loads this procedure in  0:7C00h, but don't count on registers like DS
                                             ;was initialized to 0. So we jump to it ourself.
    msg db 'Hello Gr1x!',0x0
start:
    ; Update the segment registers
     mov ax, cs
     mov ds, ax
     mov es, ax
     mov si, msg ; Print msg
print:
     lodsb ; loads charater at 0x7C0h:msg
     mov ah, 0eh ; 
     int 10h
     cmp al, 0 ;
     jne print ; 
     jmp $
     times 510-($-$$) db 0
     dw 0000h        ;or dw 0AA55h, u can write anything here , it seems that BIOS doesn't check this 2 bytes when 
                            ; it try to boot from a floppy disk
-----------------------------------------------------------------------------------------------------
OK , now compile it:
        >nasm bootstrap.asm -o bootstrap.bin
2.Now , copy this bootstrap to floppy's boot sector. If u do it with WinHex , u can omit following.
   I wrote a program which do this action.
------------------------------write_fd.cpp----------------------------------------------
//*********************************************
//               BootStrap.cpp: 2004-11-1 (C) Gr1x
//               Email:[email protected]
//***********************************************
#include <windows.h>
#include <winioctl.h>
#include <stdio.h>
#pragma comment(linker,"/subsystem:console" )
int main(int argc, char **argv)
{
 HANDLE                  hDevice=NULL,hFile=NULL;
 TCHAR                     szDevicename[64];
 LPTSTR                    szBuff=NULL;
 DISK_GEOMETRY           Geometry;
 BOOL                      bRet;
 DWORD                   bytes,breadwrite,count;
 if(argc!=2)
 {
  printf("Usage:%s filename\n",*argv);
  exit(0);
 }
 __try
 {
  hFile=CreateFile(argv[1],GENERIC_READ,FILE_SHARE_READ,NULL,OPEN_EXISTING,0,NULL);
  if (hDevice == INVALID_HANDLE_VALUE)
  {
   printf("Error in open file %s!\n",argv[1]);
   __leave;
  }
  wsprintf(szDevicename,"\\\\.\\a:");     //change \\\\.\\a for \\\\.\\PHYSICALDRIVE0 if u wanna write harddisk.Risky!
  hDevice = CreateFile( szDevicename,
   GENERIC_READ|GENERIC_WRITE,
   FILE_SHARE_READ|FILE_SHARE_WRITE,
   NULL,
   OPEN_EXISTING,
   0,
   NULL
   );

  if (hDevice == INVALID_HANDLE_VALUE)
  {
   printf("Error in open floppy device!");
   __leave;
  }

  DeviceIoControl(hDevice,FSCTL_LOCK_VOLUME, NULL,0,NULL,0,&count,NULL);
  DeviceIoControl(hDevice,IOCTL_DISK_GET_DRIVE_GEOMETRY,NULL,0,&Geometry,sizeof(DISK_GEOMETRY),&count,NULL);
  szBuff = (LPSTR)HeapAlloc(GetProcessHeap(),HEAP_ZERO_MEMORY,Geometry.BytesPerSector);
  if ( szBuff == NULL)
  {
   printf("Error in HeapAlloc!");
   __leave;
  }
  bytes = 512;
  bRet = ReadFile(hFile, szBuff, bytes, &breadwrite, NULL);
  if (bRet==FALSE || breadwrite<512)
  {
   printf("Error in read file!");
   __leave;
  }
  printf("%d bytes read from %s\n",breadwrite,argv[1]);
  bRet=WriteFile(hDevice,szBuff,bytes,&breadwrite,NULL);
  if (bRet==FALSE || breadwrite<512)
  {
   printf("Error in write file to floppy!");
   __leave;
  }
  if(!DeviceIoControl(hDevice,FSCTL_UNLOCK_VOLUME, NULL,0,NULL,0,&count,NULL))
   printf("DeviceIoControl(hDevice,FSCTL_UNLOCK_VOLUME, NULL,0,NULL,0,&count,NULL) error!\n");
  DeviceIoControl(hDevice,FSCTL_UNLOCK_VOLUME, NULL,0,NULL,0,&count,NULL);
  printf("Write finished!\nBoot sector in HEX:\n");
  for(int j=0,int i=0; i<512; i++)
  {
   unsigned char ch=(unsigned char) szBuff[i];
   if (j==8) printf(" -");
   if (j==16)
   {
    printf("\n");
    j=0;
   }
   j++;
   if (ch<0x10) printf(" %2x",ch);
   else printf(" %2x",ch);
  }
  printf("\n");
 }
 __finally
 {
  if(szBuff!=NULL)
   HeapFree(GetProcessHeap(),HEAP_NO_SERIALIZE,szBuff);
  if(hDevice!=NULL)
   CloseHandle(hDevice);
  if(hFile!=NULL)
   CloseHandle(hFile);
 }
 return 0;
}
--------------------------------------------------------------------------------------------
Don't ask me how to compile this cpp file!
3>Type following commad in CMD
    >write_fd bootstrap.bin
4>OK, boot ur system with the floppy.God bless u!

本文地址:http://com.8s8s.com/it/it25940.htm