A simple bootstrap

                                                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:gr1x@sina.com
;****************************************************
[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----------------------------------------------
//*********************************************
//              write_fd.cpp: 2004-11-1 (C) Gr1x
//               Email:gr1x@sian.com
//***********************************************

#include <windows.h>
#include <winioctl.h>
#include <stdio.h>
#pragma comment(linker,"/subsystem:console" )
void Banner()
{
 printf("---------------------------------------------/n");
 printf("  write_fd:write a file to floppy in raw mode/n");
 printf("  author:gr1x              email:gr1x@sian.com/n");
 printf("  usage:write_fd filename/n");
 printf("---------------------------------------------/n);
}

int main(int argc, char **argv)
{
 HANDLE                  hDevice=NULL,hFile=NULL;
 TCHAR                   szDevicename[64];
 LPTSTR                  szBuff=NULL;
 DISK_GEOMETRY           Geometry;
 BOOL                    bRet;
 DWORD                   bytes,bread,count;
 Banner();
 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:");
  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*2);
  if ( szBuff == NULL)
  {
   printf("Error in HeapAlloc!");
   __leave;
  }
  bytes = 512*2;
  bRet = ReadFile(hFile, szBuff, bytes, &bread, NULL);
  if (bRet==FALSE || bread<512)
  {
   printf("Error in read file!");
   __leave;
  }
  printf("%d bytes read from %s/n",bread,argv[1]);
  bRet=WriteFile(hDevice,szBuff,bytes,&bread,NULL);
  if (bRet==FALSE || bread<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!

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值