Commit initial

This commit is contained in:
2020-04-17 17:44:23 +02:00
commit ea623dfa61
54 changed files with 7297 additions and 0 deletions

84
test/tools/readpt.c Normal file
View File

@ -0,0 +1,84 @@
#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;
}

77
test/tools/w_kernLdr.c Normal file
View File

@ -0,0 +1,77 @@
#include <stdlib.h>
#include <stdio.h>
#include <fcntl.h>
#define KERNLDR_START_SECTOR 1
#define HD_SECTOR_SIZE 512
int main(int argc, char* argv[])
{
if(argc == 3)
{
char *input = argv[1];
char *output = argv[2];
printf("Copy kernLdr from <%s> to <%s> :\n", input, output);
int inputFile = open(input, O_RDONLY , 0664);
if(inputFile == -1)
{
fprintf(stderr, "Error when openning file <%s>\n", input);
return 1;
}
int outputFile = open(output, O_WRONLY | O_CREAT, 0664);
if(outputFile == -1)
{
fprintf(stderr, "Error when openning file <%s>\n", output);
return 1;
}
lseek(inputFile, 0, SEEK_SET);
lseek(outputFile, HD_SECTOR_SIZE * KERNLDR_START_SECTOR, SEEK_SET);
struct stat info;
fstat(inputFile, &info);
unsigned int kernLdrCodeSize = info.st_size;
if(kernLdrCodeSize >= 32256)
{
perror("Error kernLdr is too large\n");
close(inputFile);
close(outputFile);
return 2;
}
unsigned char *kernLdrCode = malloc(kernLdrCodeSize);
int bytesRead = read(inputFile, kernLdrCode, kernLdrCodeSize);
if(bytesRead == -1)
{
perror("Reading error\n");
free(kernLdrCode);
close(inputFile);
close(outputFile);
return 3;
}
int bytesWrite = write(outputFile, kernLdrCode, kernLdrCodeSize);
if(bytesWrite == -1)
{
perror("Writing error\n");
free(kernLdrCode);
close(inputFile);
close(outputFile);
return 3;
}
free(kernLdrCode);
close(inputFile);
close(outputFile);
printf("done\n");
}
else
{
perror("[help] : w_kernLdr <kernLdr> <disk image>\n");
return 4;
}
return 0;
}

66
test/tools/w_mbr.c Normal file
View File

@ -0,0 +1,66 @@
#include <stdlib.h>
#include <stdio.h>
#include <fcntl.h>
#define BOOT_CODE_SIZE 446
int main(int argc, char* argv[])
{
if(argc == 3)
{
char *input = argv[1];
char *output = argv[2];
printf("Copy bootcode from <%s> to <%s> :\n", input, output);
int inputFile = open(input, O_RDONLY , 0664);
if(inputFile == -1)
{
fprintf(stderr, "Error when openning file <%s>\n", input);
close(inputFile);
return 1;
}
int outputFile = open(output, O_WRONLY | O_CREAT, 0664);
if(outputFile == -1)
{
fprintf(stderr, "Error when openning file <%s>\n", output);
close(outputFile);
return 1;
}
lseek(inputFile, 0, SEEK_SET);
lseek(outputFile, 0, SEEK_SET);
unsigned char *bootCode = malloc(BOOT_CODE_SIZE);
int bytesRead = read(inputFile, bootCode, BOOT_CODE_SIZE);
if(bytesRead == -1)
{
perror("Reading error\n");
free(bootCode);
close(inputFile);
close(outputFile);
return 2;
}
int bytesWrite = write(outputFile, bootCode, BOOT_CODE_SIZE);
if(bytesWrite == -1)
{
perror("Writing error\n");
free(bootCode);
close(inputFile);
close(outputFile);
return 2;
}
free(bootCode);
close(inputFile);
close(outputFile);
printf("done\n");
}
else
{
fprintf(stderr, "[help] : w_mbr <boot code> <disk image>\n");
return 3;
}
return 0;
}