Last active
December 25, 2022 20:00
-
-
Save namazso/6a1d42a1b835aef22a81db80b9e18065 to your computer and use it in GitHub Desktop.
quick raw mbr partition editor
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#define _CRT_SECURE_NO_WARNINGS | |
#define _FILE_OFFSET_BITS 64 | |
#include <stdlib.h> | |
#include <stdint.h> | |
#include <string.h> | |
#include <stdbool.h> | |
#include <stdio.h> | |
enum { kSectorSize = 512 }; | |
void chs(uint8_t* out, uint32_t lba) | |
{ | |
if (lba >= 1024 * 255 * 63) | |
{ | |
memset(out, 0xFF, 3); | |
return; | |
} | |
enum { hpc = 16, spt = 63 }; | |
uint32_t c = lba / (hpc * spt); | |
uint32_t h = (lba / spt) % hpc; | |
uint32_t s = lba % spt + 1; | |
out[0] = h; | |
out[1] = s | (c >> 8 << 6); | |
out[2] = c; | |
} | |
int main(int argc, char** argv) | |
{ | |
if(argc < 5) | |
{ | |
printf("usage: mbrpart <file> <partition 0-3> <a|b|e|s|p> <value>"); | |
return 1; | |
} | |
FILE* fp = fopen(argv[1], "rwb"); | |
uint8_t bs[512]; | |
fread(bs, 512, 1, fp); | |
rewind(fp); | |
int vol = atoi(argv[2]); | |
int offset = 0x01BE + vol * 16; | |
int ishex = argv[4][0] == 'x' ? 1 : 0; | |
int val = strtol(ishex ? argv[4] + 1 : argv[4], NULL, ishex ? 16 : 10); | |
switch(*(argv[3])) | |
{ | |
case 'a': // active | |
*(uint8_t*)(bs + offset + 0) = val ? 0x80 : 0; | |
break; | |
case 'b': // begin | |
*(uint32_t*)(bs + offset + 8) = val; | |
chs(bs + offset + 1, val); | |
break; | |
case 'e': // end | |
chs(bs + offset + 5, val); | |
*(uint32_t*)(bs + offset + 12) = val - *(uint32_t*)(bs + offset + 8); | |
break; | |
case 's': // size | |
chs(bs + offset + 5, val + *(uint32_t*)(bs + offset + 8)); | |
*(uint32_t*)(bs + offset + 12) = val; | |
break; | |
case 'p': // platform | |
*(uint8_t*)(bs + offset + 1) = val; | |
break; | |
} | |
fwrite(bs, 512, 1, fp); | |
fclose(fp); | |
return 0; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment