int main (int argc, char *argv[]) { int Fin; int c; if ((argc < 2) || (argc > 3)) { Usage(argv[0]); exit(-1); } while((c = getopt(argc,argv,"tv")) != -1) { switch(c) { case 't': Test=1; break; case 'v': Verbose++; break; default: Usage(argv[0]); exit(-1); break; } } if (optind != argc-1) { Usage(argv[0]); exit(-1); } #ifdef O_LARGEFILE Fin = open(argv[optind],O_RDONLY | O_LARGEFILE); #else /** BSD does not use nor need O_LARGEFILE **/ Fin = open(argv[optind],O_RDONLY); #endif if (Fin == -1) { perror("ERROR: Unable to open diskimage"); exit(-1); } ReadMBR(Fin,0); close(Fin); return(0); } /* main() */
void FindExt2FSDrives() { WORD disk_no ; for(disk_no =0; disk_no < 4;disk_no++) { if(IDEDrive[disk_no].found) { disk = disk_no; ReadMBR(); TraversePrimaryPartitions(); TraverseExtendedPartitions(); } } if(numlin == 0) { PrintError("No Linux partition found on disk\n"); } }
/**************************************************** ReadMBR(): Read a master boot record (first 0x200 bytes). The MBR contains 446 bytes of assembly, and 4 partition tables. Start = offset for record Returns: 0=not MBR, 1=MBR ****************************************************/ int ReadMBR (int Fin, u_long MBRStart) { unsigned char MBR[0x200]; /* master boot record sector */ int i; /* extended partitions */ off_t Offset; /* partition descriptions */ int ActiveFlag,Type; int Head[2],Sec[2],Cyl[2]; u_long Start,Size; /* disk descriptions */ u_long SectorSize; u_long SectorPerCluster; u_long SectorPerCylinder; lseek(Fin,MBRStart,SEEK_SET); /* rewind file */ for(i=0; i<0x200; i++) { read(Fin,MBR+i,1); } /* check if it really is a MBR */ if ((MBR[0x1fe] != 0x55) || (MBR[0x1ff] != 0xaa)) { fprintf(stderr,"ERROR: No master boot record\n"); return(0); } /* 512 bytes per sector is pretty much standard. Apparently IBM's AS/400 systems use disks with 520 bytes/sector. MFM/RLL disks didn't have a native sector size. Some SCSI disks use 2048 bytes. But IDE uses 512. */ SectorSize = 512; SectorPerCluster = 0; /* does not matter for extraction */ SectorPerCylinder = 0; /* does not matter for extraction */ /* process each partition table */ for(i=446; i<510; i+=16) { /* 16 bytes describe each partition */ ActiveFlag=MBR[i]; /* 0x1BE */ Head[0]=MBR[i+1]; Sec[0]=(MBR[i+2] >> 2) & 0xcf; Cyl[0]=MBR[i+3] + (MBR[i+2] & 0x3)*16; Type=MBR[i+4]; Head[1]=MBR[i+5]; Sec[1]=(MBR[i+6] >> 2) & 0xcf; Cyl[1]=MBR[i+7] + (MBR[i+6] & 0x3)*16; /* Starting sector number, size of the sector */ Start=MBR[i+ 8] + MBR[i+ 9]*256 + MBR[i+10]*256*256 + MBR[i+11]*256*256*256; Size= MBR[i+12] + MBR[i+13]*256 + MBR[i+14]*256*256 + MBR[i+15]*256*256*256; if (Type != 0) /* Type 0 is unused */ { printf("Partition: (Active=%d,Type=%x)\n",ActiveFlag & 0x80,Type); printf(" HSC Start=%d,%d,%d\n",Head[0],Sec[0],Cyl[0]); printf(" HSC End =%d,%d,%d\n",Head[1],Sec[1],Cyl[1]); printf(" Sector: Start=%lu (%08lx) End=%lu (%08lx)\n", Start,Start,Start+Size,Start+Size); printf(" Byte: Logical start= %lu (%08lx)\n", MBRStart+(Start)*SectorSize, MBRStart+(Start)*SectorSize); printf(" Byte: Logical end = %lu (%08lx)\n", MBRStart+(Size+Start)*SectorSize, MBRStart+(Size+Start)*SectorSize); if (Start == 0) /* if it is a Linux kernel */ { ExtractKernel(Fin); break; } } /* check for extended partitions */ /** Types: http://www.win.tue.nl/~aeb/partitions/partition_types-1.html **/ switch(Type) { case 0x00: /* unused */ break; case 0x05: /* extended partition */ case 0x0f: /* Win95 extended partition */ Offset = lseek(Fin,0,SEEK_CUR); ReadMBR(Fin,MBRStart+(Start)*SectorSize); Offset = lseek(Fin,Offset,SEEK_CUR); break; case 0x06: /* FAT (DOS 3.3+) */ case 0x07: /* OS/2 HPFS, Windows NTFS, Advanced Unix */ case 0x0b: /* Win95 OSR2 FAT32 */ case 0x0c: /* Win95 OSR2 FAT32, LBA-mapped */ case 0x82: /* Linux swap */ case 0x83: /* Linux partition */ default: /* extract partition */ { long S,E; S=MBRStart+(Start)*SectorSize; E=MBRStart+(Size)*SectorSize; if (Verbose) fprintf(stderr,"Extracting type %02x: start=%04lx size=%lu\n",Type,S,E); ExtractPartition(Fin,S,E); } } } /* for MBR */ return(1); } /* ReadMBR() */
/******************************************************************************* * Function Name : FAT_Init * Description : Init the FAT16 File System. * Input : Mone * Output : None * Return : None *******************************************************************************/ void FAT_Init(void) { ReadMBR(); ReadBPB(); }