12 #define _LARGEFILE64_SOURCE
18 #ifndef __USE_LARGEFILE64
19 #define __USE_LARGEFILE64
21 #ifndef __USE_FILE_OFFSET64
22 #define __USE_FILE_OFFSET64
30 #include <sys/types.h>
36 char Version[]=COMMIT_HASH;
64 off_t ReadSize,WriteSize;
65 unsigned char Buffer[655360], *Bp;
71 unsigned char GZHead[2][4]={ {0x1f,0x8b,0x08,0x00},{0x1f,0x8b,0x08,0x08} };
77 Hold = lseek(Fin,0,SEEK_CUR);
83 ReadSize = read(Fin,Buffer+GZindex,1);
91 if (!memcmp(Buffer,GZHead[0],GZindex+1) ||
92 !memcmp(Buffer,GZHead[1],GZindex+1))
103 if (ReadSize == 0)
return;
107 memset(Name,0,
sizeof(Name));
108 snprintf(Name,250,
"Kernel_%04d",Counter);
110 Fout = open(Name,O_CREAT | O_LARGEFILE | O_WRONLY | O_TRUNC, 0644);
113 Fout = open(Name,O_CREAT | O_WRONLY | O_TRUNC, 0644);
119 perror(
"ERROR: Unable to create output file for kernel");
130 WriteSize = write(Fout,Bp,ReadSize);
132 ReadSize = ReadSize - WriteSize;
133 if (WriteSize <= 0) {
break; }
135 ReadSize = read(Fin,Buffer,
sizeof(Buffer));
143 lseek(Fin,Hold,SEEK_SET);
158 off_t ReadSize,WriteSize;
159 unsigned char Buffer[655360], *Bp;
161 static int Counter=0;
172 if (
Verbose) fprintf(stderr,
"ERROR: Partition size is <= 0.\n");
177 Hold = lseek64(Fin,0,SEEK_CUR);
181 if (
Verbose) fprintf(stderr,
"ERROR: Start is before the starting area.\n");
182 lseek64(Fin,Hold,SEEK_SET);
188 if (Start > Stat.st_size)
191 if (
Verbose) fprintf(stderr,
"ERROR: Partition start is after then end of file.\n");
192 lseek64(Fin,Hold,SEEK_SET);
195 if (Start + Size > Stat.st_size)
198 if (
Verbose) fprintf(stderr,
"WARNING: Partition end is after then end of file; partition is truncated.\n");
199 Size = Stat.st_size - Start;
203 memset(Name,0,
sizeof(Name));
204 snprintf(Name,250,
"Partition_%04d",Counter);
206 Fout = open(Name,O_CREAT | O_LARGEFILE | O_WRONLY | O_TRUNC, 0644);
209 Fout = open(Name,O_CREAT | O_WRONLY | O_TRUNC, 0644);
213 perror(
"ERROR: Unable to create output file for partition");
219 lseek64(Fin,(off64_t)Start,SEEK_SET);
223 if (Size >
sizeof(Buffer))
225 ReadSize = read(Fin,Buffer,
sizeof(Buffer));
229 ReadSize = read(Fin,Buffer,Size);
231 if (ReadSize <= 0) Size=0;
235 WriteSize = write(Fout,Bp,ReadSize);
236 Size = Size - WriteSize;
238 ReadSize = ReadSize - WriteSize;
239 if (WriteSize <= 0) {ReadSize=0; Size=0;}
248 lseek64(Fin,Hold,SEEK_SET);
262 unsigned char MBR[0x200];
268 int Head[2],Sec[2],Cyl[2];
275 lseek(Fin,MBRStart,SEEK_SET);
276 for(i=0; i<0x200; i++)
278 if(read(Fin,MBR+i,1) < 0)
280 fprintf(stderr,
"ERROR %s.%d: unable to perform read", __FILE__, __LINE__);
281 fprintf(stderr,
"ERROR errno is: %s\n", strerror(errno));
286 if ((MBR[0x1fe] != 0x55) || (MBR[0x1ff] != 0xaa))
288 fprintf(stderr,
"ERROR: No master boot record\n");
303 for(i=446; i<510; i+=16)
308 Sec[0]=(MBR[i+2] >> 2) & 0xcf;
309 Cyl[0]=MBR[i+3] + (MBR[i+2] & 0x3)*16;
312 Sec[1]=(MBR[i+6] >> 2) & 0xcf;
313 Cyl[1]=MBR[i+7] + (MBR[i+6] & 0x3)*16;
315 Start=MBR[i+ 8] + MBR[i+ 9]*256 + MBR[i+10]*256*256 + MBR[i+11]*256*256*256;
316 Size= MBR[i+12] + MBR[i+13]*256 + MBR[i+14]*256*256 + MBR[i+15]*256*256*256;
319 printf(
"Partition: (Active=%d,Type=%x)\n",ActiveFlag & 0x80,Type);
320 printf(
" HSC Start=%d,%d,%d\n",Head[0],Sec[0],Cyl[0]);
321 printf(
" HSC End =%d,%d,%d\n",Head[1],Sec[1],Cyl[1]);
322 printf(
" Sector: Start=%llu (%08llx) End=%llu (%08llx)\n",
323 (
unsigned long long)Start,(
unsigned long long)Start,
324 (
unsigned long long)Start+Size,(
unsigned long long)Start+Size);
325 printf(
" Byte: Logical start= %llu (%08llx)\n",
326 (
unsigned long long)MBRStart+(Start)*SectorSize,
327 (
unsigned long long)MBRStart+(Start)*SectorSize);
328 printf(
" Byte: Logical end = %llu (%08llx)\n",
329 (
unsigned long long)MBRStart+(Size+Start)*SectorSize,
330 (
unsigned long long)MBRStart+(Size+Start)*SectorSize);
347 Offset = lseek(Fin,0,SEEK_CUR);
348 ReadMBR(Fin,MBRStart+(Start)*SectorSize);
349 Offset = lseek(Fin,Offset,SEEK_CUR);
361 S=MBRStart+(Start)*SectorSize;
362 E=MBRStart+(Size)*SectorSize;
364 fprintf(stderr,
"Extracting type %02x: start=%04llx size=%llu\n",
365 Type,(
unsigned long long)S,(
unsigned long long)E);
379 fprintf(stderr,
"Usage: %s [-t] diskimage\n",
Filename);
380 fprintf(stderr,
" -t = test -- do not actually extract.\n");
381 fprintf(stderr,
" -v = Verbose.\n");
387 int main (
int argc,
char *argv[])
392 if ((argc < 2) || (argc > 3))
398 while((c = getopt(argc,argv,
"tv")) != -1)
402 case 't':
Test=1;
break;
410 if (optind != argc-1)
417 Fin = open(argv[optind],O_RDONLY | O_LARGEFILE);
420 Fin = open(argv[optind],O_RDONLY);
424 perror(
"ERROR: Unable to open diskimage");
int main(int argc, char *argv[])
Main for departition.
void Usage(char *Filename)
Usage.
int ReadMBR(int Fin, uint64_t MBRStart)
Read a master boot record (first 0x200 bytes).
int Verbose
Verbose level for log.
void ExtractPartition(int Fin, uint64_t Start, uint64_t Size)
Dump a partition to a file.
int Test
Set to 0 to extract, 1 to just be verbose.
void ExtractKernel(int Fin)
Extract a kernel file system and copy it to a new file called "Kernel_%04d",Counter.