23 #define _LARGEFILE64_SOURCE 29 #ifndef __USE_LARGEFILE64 30 #define __USE_LARGEFILE64 32 #ifndef __USE_FILE_OFFSET64 33 #define __USE_FILE_OFFSET64 41 #include <sys/types.h> 47 char Version[]=COMMIT_HASH;
75 off_t ReadSize,WriteSize;
76 unsigned char Buffer[655360], *Bp;
82 unsigned char GZHead[2][4]={ {0x1f,0x8b,0x08,0x00},{0x1f,0x8b,0x08,0x08} };
88 Hold = lseek(Fin,0,SEEK_CUR);
94 ReadSize = read(Fin,Buffer+GZindex,1);
102 if (!memcmp(Buffer,GZHead[0],GZindex+1) ||
103 !memcmp(Buffer,GZHead[1],GZindex+1))
114 if (ReadSize == 0)
return;
118 memset(Name,0,
sizeof(Name));
119 snprintf(Name,250,
"Kernel_%04d",Counter);
121 Fout = open(Name,O_CREAT | O_LARGEFILE | O_WRONLY | O_TRUNC, 0644);
124 Fout = open(Name,O_CREAT | O_WRONLY | O_TRUNC, 0644);
130 perror(
"ERROR: Unable to create output file for kernel");
141 WriteSize = write(Fout,Bp,ReadSize);
143 ReadSize = ReadSize - WriteSize;
144 if (WriteSize <= 0) {
break; }
146 ReadSize = read(Fin,Buffer,
sizeof(Buffer));
154 lseek(Fin,Hold,SEEK_SET);
169 off_t ReadSize,WriteSize;
170 unsigned char Buffer[655360], *Bp;
172 static int Counter=0;
183 if (
Verbose) fprintf(stderr,
"ERROR: Partition size is <= 0.\n");
188 Hold = lseek64(Fin,0,SEEK_CUR);
192 if (
Verbose) fprintf(stderr,
"ERROR: Start is before the starting area.\n");
193 lseek64(Fin,Hold,SEEK_SET);
199 if (Start > Stat.st_size)
202 if (
Verbose) fprintf(stderr,
"ERROR: Partition start is after then end of file.\n");
203 lseek64(Fin,Hold,SEEK_SET);
206 if (Start + Size > Stat.st_size)
209 if (
Verbose) fprintf(stderr,
"WARNING: Partition end is after then end of file; partition is truncated.\n");
210 Size = Stat.st_size - Start;
214 memset(Name,0,
sizeof(Name));
215 snprintf(Name,250,
"Partition_%04d",Counter);
217 Fout = open(Name,O_CREAT | O_LARGEFILE | O_WRONLY | O_TRUNC, 0644);
220 Fout = open(Name,O_CREAT | O_WRONLY | O_TRUNC, 0644);
224 perror(
"ERROR: Unable to create output file for partition");
230 lseek64(Fin,(off64_t)Start,SEEK_SET);
234 if (Size >
sizeof(Buffer))
236 ReadSize = read(Fin,Buffer,
sizeof(Buffer));
240 ReadSize = read(Fin,Buffer,Size);
242 if (ReadSize <= 0) Size=0;
246 WriteSize = write(Fout,Bp,ReadSize);
247 Size = Size - WriteSize;
249 ReadSize = ReadSize - WriteSize;
250 if (WriteSize <= 0) {ReadSize=0; Size=0;}
259 lseek64(Fin,Hold,SEEK_SET);
273 unsigned char MBR[0x200];
279 int Head[2],Sec[2],Cyl[2];
286 lseek(Fin,MBRStart,SEEK_SET);
287 for(i=0; i<0x200; i++)
289 if(read(Fin,MBR+i,1) < 0)
291 fprintf(stderr,
"ERROR %s.%d: unable to perform read", __FILE__, __LINE__);
292 fprintf(stderr,
"ERROR errno is: %s\n", strerror(errno));
297 if ((MBR[0x1fe] != 0x55) || (MBR[0x1ff] != 0xaa))
299 fprintf(stderr,
"ERROR: No master boot record\n");
314 for(i=446; i<510; i+=16)
319 Sec[0]=(MBR[i+2] >> 2) & 0xcf;
320 Cyl[0]=MBR[i+3] + (MBR[i+2] & 0x3)*16;
323 Sec[1]=(MBR[i+6] >> 2) & 0xcf;
324 Cyl[1]=MBR[i+7] + (MBR[i+6] & 0x3)*16;
326 Start=MBR[i+ 8] + MBR[i+ 9]*256 + MBR[i+10]*256*256 + MBR[i+11]*256*256*256;
327 Size= MBR[i+12] + MBR[i+13]*256 + MBR[i+14]*256*256 + MBR[i+15]*256*256*256;
330 printf(
"Partition: (Active=%d,Type=%x)\n",ActiveFlag & 0x80,Type);
331 printf(
" HSC Start=%d,%d,%d\n",Head[0],Sec[0],Cyl[0]);
332 printf(
" HSC End =%d,%d,%d\n",Head[1],Sec[1],Cyl[1]);
333 printf(
" Sector: Start=%llu (%08llx) End=%llu (%08llx)\n",
334 (
unsigned long long)Start,(
unsigned long long)Start,
335 (
unsigned long long)Start+Size,(
unsigned long long)Start+Size);
336 printf(
" Byte: Logical start= %llu (%08llx)\n",
337 (
unsigned long long)MBRStart+(Start)*SectorSize,
338 (
unsigned long long)MBRStart+(Start)*SectorSize);
339 printf(
" Byte: Logical end = %llu (%08llx)\n",
340 (
unsigned long long)MBRStart+(Size+Start)*SectorSize,
341 (
unsigned long long)MBRStart+(Size+Start)*SectorSize);
358 Offset = lseek(Fin,0,SEEK_CUR);
359 ReadMBR(Fin,MBRStart+(Start)*SectorSize);
360 Offset = lseek(Fin,Offset,SEEK_CUR);
372 S=MBRStart+(Start)*SectorSize;
373 E=MBRStart+(Size)*SectorSize;
375 fprintf(stderr,
"Extracting type %02x: start=%04llx size=%llu\n",
376 Type,(
unsigned long long)S,(
unsigned long long)E);
390 fprintf(stderr,
"Usage: %s [-t] diskimage\n",Filename);
391 fprintf(stderr,
" -t = test -- do not actually extract.\n");
392 fprintf(stderr,
" -v = Verbose.\n");
398 int main (
int argc,
char *argv[])
403 if ((argc < 2) || (argc > 3))
409 while((c = getopt(argc,argv,
"tv")) != -1)
413 case 't':
Test=1;
break;
421 if (optind != argc-1)
428 Fin = open(argv[optind],O_RDONLY | O_LARGEFILE);
431 Fin = open(argv[optind],O_RDONLY);
435 perror(
"ERROR: Unable to open diskimage");
int ReadMBR(int Fin, uint64_t MBRStart)
Read a master boot record (first 0x200 bytes).
int Test
Set to 0 to extract, 1 to just be verbose.
int Verbose
Verbose level for log.
void Usage(char *Filename)
Usage.
void ExtractPartition(int Fin, uint64_t Start, uint64_t Size)
Dump a partition to a file.
int main(int argc, char *argv[])
Main for departition.
void ExtractKernel(int Fin)
Extract a kernel file system and copy it to a new file called "Kernel_%04d",Counter.