1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506
| #define _LARGEFILE64_SOURCE /* pour off64_t */
#include <sys/types.h>
#include <unistd.h>
#include <linux/unistd.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#define IS_EXTENDED_PARTITION(partition) ((partition)->type == 0x05 || (partition)->type == 0x0f)
#define IS_INVALID_EBR(ebr) ((*(ebr.signature) != 0x55) || (*((ebr.signature)+1) != 0xaa))
#define SECTOR_SIZE 512
typedef unsigned char BYTE;
typedef enum { FALSE, TRUE } BOOL;
typedef struct {
BYTE bootable;
BYTE firstHeadSector;
short firstSectAndCyl;
BYTE type;
BYTE lastHeadSector;
short lastSectAndCyl;
unsigned int start;
unsigned int length;
} PARTDESCR; /* structure d'une entrée de partition */
typedef struct {
BYTE unused[446];
BYTE firstEntry[16];
BYTE secondEntry[16];
BYTE unused2[32];
BYTE signature[2];
} EBRDESCR; /* structure d'une entrée de partition étendue */
void myRead(off64_t pos, unsigned int size, void * buffer); /* permet de lire */
void showPartDescr(PARTDESCR * infos, BOOL decal); /* permet d'afficher les caracteristiques d'une partition */
BOOL isEmpty(void * pZone, unsigned int size); /* permet de savoir si la zone memoire est nulle */
char * getStringType(int val); /* permet d'obtenir le type de partition sous forme de chaine */
int main(int argc, char * argv[])
{
PARTDESCR primaryParts[4]; /* partitions primaires */
PARTDESCR * logicalParts = NULL, * pLogicalPart = NULL; /* partitions logiques */
EBRDESCR ebr;
int extendedPartitionId = -1; /* identification de la partition primaire qui est la partition etendue */
unsigned int i, j, logicalPartCount = 0;
off64_t offset, offsetNextEBR; /* offset 64 bits */
for(i=0; i < 4; i++)
{
myRead(446+(i*16), 16, &primaryParts[i]); /* lit le disque juste après le mbr (446eme octets) */
if(IS_EXTENDED_PARTITION(&primaryParts[i]))
{
if(extendedPartitionId != -1)
{
perror("More than one extended partition !");
exit(1);
}
extendedPartitionId = i;
}
}
if(extendedPartitionId != -1)
{
offset = ((&primaryParts[extendedPartitionId])->start); /* on regarde a quel secteur se trouve la partition etendue */
offset *= SECTOR_SIZE; /* conversion en octet */
myRead(offset, 512, &ebr);
while(TRUE)
{
pLogicalPart = realloc(logicalParts, 16 * (logicalPartCount+1)); /* pour stocker la nouvelle description */
if(logicalParts == NULL) /* pour localiser le debut de la zone memoire */
logicalParts = pLogicalPart;
if(IS_INVALID_EBR(ebr))
{
perror("Bad extended signature !");
exit(1);
}
memcpy(&logicalParts[logicalPartCount], (int *)ebr.firstEntry, 16); /* on copie la premiere entree (description de partition) dans le tableau */
logicalPartCount++;
/*
Secteur de début du prochain EBR : *(int *)((ebr.secondEntry)+8)
x SECTOR_SIZE pour avoir en octets */
offsetNextEBR = ((off64_t)(*(int *)((ebr.secondEntry)+8))) * SECTOR_SIZE;
if(offsetNextEBR == 0) //si la zone est nulle, il n'y a plus de lecteurs logiques
break;
offsetNextEBR += offset; //le point de référence est le début de la partition étendue
myRead(offsetNextEBR, 512, &ebr);
}
}
printf(" Primary partitions :\n=--------------------=\n");
for(i=0; i < 4; i++)
{
printf("\t#%i\n", i+1);
showPartDescr(&primaryParts[i], FALSE);
if(extendedPartitionId == i)
{
printf("\t\t\t Logical drives :\n\t\t\t=----------------=\n");
for(j=0; j < logicalPartCount; j++)
{
printf("\t\t\t\t#%i-%i\n", i+1, j+1);
showPartDescr(&logicalParts[j], TRUE);
}
}
}
return 0;
}
/**
* Permet de lire le disque physique
* @param pos la position a laquelle on doit lire
* @param size la longueur, en octets, des donnees a lire
* @param buffer la zone memoire de destination
*/
void myRead(off64_t pos, unsigned int size, void * buffer)
{
int fp = -1;
int res = -1;
fp = open("/dev/sda", 'r');
if(fp == -1)
{
perror("Cannot open /dev/sda !");
exit(1);
}
lseek64(fp, pos, SEEK_SET);
res = read(fp, buffer, size);
if(res == -1)
{
perror("Cannot read !");
exit(1);
}
close(fp);
}
/**
* Permet d'afficher les informations d'une partition. Si la partition est vide, il affiche juste une information pour l'indiquer.
*
* @param infos les informations de la partition
* @param decal si l'on doit decaler l'affichage
*/
void showPartDescr(PARTDESCR * infos, BOOL decal)
{
if(!isEmpty(infos, sizeof(PARTDESCR)))
{
char * decalStr;
if(decal)
decalStr = "\t\t\t";
else
decalStr = "";
printf("%sBootable : ", decalStr);
if(infos->bootable == 0)
printf("False (0x00)\n");
else
printf("True (%#x)\n", infos->bootable);
printf("%sType : %s (%#x)\n", decalStr, getStringType(infos->type), infos->type);
printf("%sStart : %#x\n", decalStr, infos->start);
printf("%sLength : %#x\n\n", decalStr, infos->length);
}
else
printf(" (Empty)\n\n");
}
/**
* Permet de savoir si une zone memoire est entierement nulle
*
* @param pZone la zone memoire
* @param size la taille de la zone memoire
* @return vrai ou faux selon le cas
*/
BOOL isEmpty(void * pZone, unsigned int size)
{
unsigned int i;
BYTE * pByte = (BYTE *)pZone;
for(i=0; i < size; i++, pByte++)
{
if(*pByte != 0)
return FALSE;
}
return TRUE;
}
/**
* Permet de retourner le type de partition sous forme de chaine
*
* @param val la valeur representant le type de partition
* @return le type sous forme de chaine
*/
char * getStringType(int val)
{
char * strType = NULL;
switch(val)
{
case 0x00:
strType = "Empty";
break;
case 0x01:
strType = "FAT12";
break;
case 0x02:
strType = "XENIX root";
break;
case 0x03:
strType = "XENIX usr";
break;
case 0x04:
strType = "Small FAT16";
break;
case 0x05:
strType = "Extended";
break;
case 0x06:
strType = "FAT16";
break;
case 0x07:
strType = "HPFS/NTFS";
break;
case 0x08:
strType = "AIX";
break;
case 0x09:
strType = "AIX bootable";
break;
case 0x0a:
strType = "OS/2 boot mgr";
break;
case 0x0b:
strType = "FAT32";
break;
case 0x0c:
strType = "FAT32 LBA";
break;
case 0x0e:
strType = "FAT16 LBA";
break;
case 0x0f:
strType = "Extended LBA";
break;
case 0x10:
strType = "OPUS";
break;
case 0x11:
strType = "Hidden FAT12";
break;
case 0x12:
strType = "Compaq diag";
break;
case 0x14:
strType = "Hidd Sm FAT16";
break;
case 0x16:
strType = "Hidd FAT16";
break;
case 0x17:
strType = "Hidd HPFS/NTFS";
break;
case 0x18:
strType = "AST SmartSleep";
break;
case 0x1b:
strType = "Hidd FAT32";
break;
case 0x1c:
strType = "Hidd FAT32 LBA";
break;
case 0x1e:
strType = "Hidd FAT16 LBA";
break;
case 0x24:
strType = "NEC DOS";
break;
case 0x39:
strType = "Plan 9";
break;
case 0x3c:
strType = "PMagic recovery";
break;
case 0x40:
strType = "Venix 80286";
break;
case 0x41:
strType = "PPC PReP Boot";
break;
case 0x42:
strType = "SFS";
break;
case 0x4d:
strType = "QNX4.x";
break;
case 0x4e:
strType = "QNX4.x 2nd part";
break;
case 0x4f:
strType = "QNX4.x 3rd part";
break;
case 0x50:
strType = "OnTrack DM";
break;
case 0x51:
strType = "OnTrackDM6 Aux1";
break;
case 0x52:
strType = "CP/M";
break;
case 0x53:
strType = "OnTrackDM6 Aux3";
break;
case 0x54:
strType = "OnTrack DM6";
break;
case 0x55:
strType = "EZ Drive";
break;
case 0x56:
strType = "Golden Bow";
break;
case 0x5c:
strType = "Priam Edisk";
break;
case 0x61:
strType = "SpeedStor";
break;
case 0x63:
strType = "GNU HURD/SysV";
break;
case 0x64:
strType = "Netware 286";
break;
case 0x65:
strType = "Netware 386";
break;
case 0x70:
strType = "DiskSec MltBoot";
break;
case 0x75:
strType = "PC/IX";
break;
case 0x80:
strType = "Minix <1.4a";
break;
case 0x81:
strType = "Minix >1.4b";
break;
case 0x82:
strType = "Linux swap / Solaris";
break;
case 0x83:
strType = "Linux";
break;
case 0x84:
strType = "OS/2 hidden C:";
break;
case 0x85:
strType = "Linux extended";
break;
case 0x86:
strType = "NTFS volume se";
break;
case 0x87:
strType = "NTFS volume set";
break;
case 0x88:
strType = "Linux plaintext";
break;
case 0x8e:
strType = "Linux LVM";
break;
case 0x93:
strType = "Amoeba";
break;
case 0x94:
strType = "Amoeba BBT";
break;
case 0x9f:
strType = "BSD/OS";
break;
case 0xa0:
strType = "Thinkpad hib";
break;
case 0xa5:
strType = "FreeBSD";
break;
case 0xa6:
strType = "OpenBSD";
break;
case 0xa7:
strType = "NeXTSTEP";
break;
case 0xa8:
strType = "Darwin UFS";
break;
case 0xa9:
strType = "NetBSD";
break;
case 0xab:
strType = "Darwin boot";
break;
case 0xb7:
strType = "BSDI fs";
break;
case 0xb8:
strType = "BSDI swap";
break;
case 0xbb:
strType = "Boot Wizard Hi";
break;
case 0xbe:
strType = "Solaris boot";
break;
case 0xbf:
strType = "Solaris";
break;
case 0xc1:
strType = "DRDOS/2 FAT12";
break;
case 0xc4:
strType = "DRDOS/2 smFAT16";
break;
case 0xc6:
strType = "DRDOS/2 FAT16";
break;
case 0xc7:
strType = "Syrinx";
break;
case 0xda:
strType = "Non-FS dat";
break;
case 0xdb:
strType = "CP/M / CTOS";
break;
case 0xde:
strType = "Dell Utility";
break;
case 0xdf:
strType = "BootIt";
break;
case 0xe1:
strType = "DOS access";
break;
case 0xe3:
strType = "DOS R/O";
break;
case 0xe4:
strType = "SpeedStor";
break;
case 0xeb:
strType = "BeOS fs";
break;
case 0xee:
strType = "EFI GPT";
break;
case 0xef:
strType = "EFI FAT";
break;
case 0xf0:
strType = "Lnx/PA-RISC bt";
break;
case 0xf1:
strType = "SpeedStor";
break;
case 0xf2:
strType = "DOS secondary";
break;
case 0xf4:
strType = "SpeedStor";
break;
case 0xfd:
strType = "Lnx RAID auto";
break;
case 0xfe:
strType = "LANstep";
break;
case 0xff:
strType = "XENIX BB";
break;
}
return strType;
} |
Partager