StupidOS/sbin/parted/main.c

159 lines
2.8 KiB
C
Raw Normal View History

2024-04-14 05:35:43 +00:00
#include <stdlib.h>
#include <stdio.h>
#include <stdint.h>
#include <string.h>
2024-04-24 06:06:36 +00:00
#include <errno.h>
2024-05-02 11:34:27 +00:00
#include <libgen.h>
#include "mbr.h"
2024-04-14 05:35:43 +00:00
static char *prg_name = NULL;
static int dump_info = 0;
static const char *diskpath = NULL;
2024-05-02 11:34:27 +00:00
static FILE *diskfp = NULL;
static FILE *partfp[4] = {
NULL,
NULL,
NULL,
NULL
};
2024-04-14 05:35:43 +00:00
static MBRHeader header;
static void
dump_partition_info(Partition part)
{
printf("\tBootable: ");
if (part.attributes & MBR_PART_BOOTABLE)
{
printf("yes\n");
}
else
{
printf("no\n");
}
printf("\tType: %d\n", part.type);
printf("\tLBA Start: %d\n", part.lba_start);
printf("\tSectors: %d\n", part.sectors_count);
}
static void
dump_disk(void)
{
int idx;
printf("UID: ");
for (idx = 0; idx < 10; idx++)
{
printf("%02X", header.uid[idx]);
}
printf("\n");
for (idx = 0; idx < 4; idx++)
{
printf("Partition %d:\n", idx);
dump_partition_info(header.part[idx]);
}
}
static void
usage(int retcode)
{
if (retcode == EXIT_FAILURE)
{
fprintf(stderr, "Try '%s -h' for more information.\n", prg_name);
}
else
{
printf("Usage: %s [-hVd] disk\n", prg_name);
printf("\t-h\tdisplay this help and exit\n");
printf("\t-V\toutput version information.\n");
printf("\t-d\tdump disk information\n");
2024-05-02 11:34:27 +00:00
printf("\t-p0-3\t\n");
2024-04-14 05:35:43 +00:00
printf("\t-o out\twrite to file 'out'\n");
2024-05-02 11:34:27 +00:00
printf("\t-e\textract\n");
2024-04-24 06:06:36 +00:00
printf("\t-i img\t\n");
2024-04-14 05:35:43 +00:00
printf("\nReport bugs to <%s>\n", MK_BUGREPORT);
}
exit(retcode);
}
static void
version(void)
{
printf("%s commit %s\n", prg_name, MK_COMMIT);
exit(EXIT_SUCCESS);
}
int
main(int argc, char **argv)
{
2024-05-02 11:34:27 +00:00
char c;
prg_name = basename(argv[0]);
2024-04-14 05:35:43 +00:00
while ((argc > 1) && (argv[1][0] == '-'))
{
switch (argv[1][1])
{
case 'h':
usage(EXIT_SUCCESS);
break;
case 'V':
version();
break;
case 'd':
dump_info = 1;
break;
2024-05-02 11:34:27 +00:00
case 'p':
c = argv[1][2] - '0';
argv++;
argc--;
if (c != 0 && c != 1 && c != 2 && c != 3)
{
usage(EXIT_FAILURE);
}
partfp[c] = fopen(argv[2], "rb");
if (partfp[c] == NULL)
{
fprintf(stderr, "%s: %s\n", diskpath, strerror(errno));
return (EXIT_FAILURE);
}
break;
case 'e':
break;
2024-04-14 05:35:43 +00:00
default:
usage(EXIT_FAILURE);
}
argv++;
argc--;
}
if (argc <= 1) usage(EXIT_FAILURE);
diskpath = argv[1];
2024-05-02 11:34:27 +00:00
diskfp = fopen(diskpath, "rb");
if (diskfp == NULL)
2024-04-14 05:35:43 +00:00
{
fprintf(stderr, "%s: %s\n", diskpath, strerror(errno));
return (EXIT_FAILURE);
}
2024-05-02 11:34:27 +00:00
if (fread(&header, sizeof(uint8_t), sizeof(MBRHeader), diskfp) != sizeof(MBRHeader))
2024-04-14 05:35:43 +00:00
{
fprintf(stderr, "%s: can't read mbr header\n", diskpath);
return (EXIT_FAILURE);
}
if (header.magic[0] != MBR_MAGIC0 || header.magic[1] != MBR_MAGIC1)
{
fprintf(stderr, "%s: no valid MBR\n", diskpath);
return (EXIT_FAILURE);
}
if (dump_info)
{
dump_disk();
}
return (EXIT_SUCCESS);
}