NutsOS/test/tools/readpt.c
2020-04-17 17:44:23 +02:00

85 lines
2.2 KiB
C

#include <stdlib.h>
#include <stdio.h>
#include <string.h>
#include <fcntl.h>
#include <stdint.h>
#define HD_SECTOR_SIZE 512
#define PARTITION_TABLE_START 0x1BE
#define PARTITION_TABLE_SIZE 64
typedef struct {
uint8_t bootSign;
uint8_t startHead;
uint8_t startSector:6;
uint16_t startCylinder:10;
uint8_t fsID;
uint8_t endHead;
uint8_t endSector:6;
uint16_t endCylinder:10;
uint32_t startLBA;
uint32_t sectorCount;
}__attribute__((packed)) T_PartitionTable;
int main(int argc, char* argv[]) {
if(argc == 4) {
char *diskPath = argv[1];
int partNum = atoi(argv[2]);
char* field = argv[3];
T_PartitionTable pt[4];
if(partNum < 0 || partNum > 3) {
fprintf(stderr, "Invalid partiton number\n");
return -1;
}
int fd = open(diskPath, O_RDONLY, 0664);
if(fd == -1) {
close(fd);
fprintf(stderr, "Error when openning the file <%s>\n", diskPath);
return -2;
}
lseek(fd, PARTITION_TABLE_START, SEEK_SET);
int bytesRead = read(fd, pt, PARTITION_TABLE_SIZE);
if(bytesRead == -1) {
close(fd);
fprintf(stderr, "Error when reading the file <%s>\n", diskPath);
return -3;
}
close(fd);
if(!strcmp(field, "boot_sign"))
printf("%u", pt[partNum].bootSign);
else if(!strcmp(field, "start_head"))
printf("%u", pt[partNum].startHead);
else if(!strcmp(field, "start_sector"))
printf("%u", pt[partNum].startSector);
else if(!strcmp(field, "start_cylinder"))
printf("%u", pt[partNum].startCylinder);
else if(!strcmp(field, "fs_ID"))
printf("%u", pt[partNum].fsID);
else if(!strcmp(field, "end_head"))
printf("%u", pt[partNum].endHead);
else if(!strcmp(field, "end_sector"))
printf("%u", pt[partNum].endSector);
else if(!strcmp(field, "end_cylinder"))
printf("%u", pt[partNum].endCylinder);
else if(!strcmp(field, "start_LBA"))
printf("%u", pt[partNum].startLBA);
else if(!strcmp(field, "sector_count"))
printf("%u", pt[partNum].sectorCount);
else {
perror("Invalid field name\n");
return -4;
}
} else {
fprintf(stderr, "[help] : readpt <disk> <partition> <field>\n");
fprintf(stderr, "Valid field names : boot_sign,start_head,start_sector,start_cylinder,fs_ID,end_head,end_sector,end_cylinder,start_LBA,sectorCount\n");
return -5;
}
return 0;
}