implement CHS and LBA conversion functions. seem to be working right
This commit is contained in:
parent
9a0489c51d
commit
2658cb74a9
4 changed files with 45 additions and 13 deletions
|
|
@ -3,6 +3,25 @@
|
|||
|
||||
#include <stdint.h>
|
||||
|
||||
void decode_chs(const uint8_t* chs, uint16_t* cylinder, uint16_t* head, uint16_t* sector);
|
||||
typedef struct {
|
||||
uint8_t b[3];
|
||||
} __attribute__((packed)) chs_encoded_t;
|
||||
|
||||
typedef struct {
|
||||
uint16_t cylinder;
|
||||
uint16_t head;
|
||||
uint16_t sector;
|
||||
} chs_t;
|
||||
|
||||
typedef struct {
|
||||
uint16_t num_cylinders;
|
||||
uint16_t num_heads;
|
||||
uint16_t num_sectors;
|
||||
} chs_conf_t;
|
||||
|
||||
void chs_decode(const chs_encoded_t* enc, chs_t* chs);
|
||||
|
||||
uint32_t chs_to_lba(const chs_conf_t* conf, const chs_t* chs);
|
||||
void lba_to_chs(const chs_conf_t* conf, uint32_t lba, chs_t* chs);
|
||||
|
||||
#endif
|
||||
|
|
@ -2,12 +2,13 @@
|
|||
#define __MBR_H
|
||||
|
||||
#include <stdint.h>
|
||||
#include "chs.h"
|
||||
|
||||
typedef struct {
|
||||
uint8_t attributes;
|
||||
uint8_t chs_first[3];
|
||||
chs_encoded_t chs_first;
|
||||
uint8_t type;
|
||||
uint8_t chs_last[3];
|
||||
chs_encoded_t chs_last;
|
||||
uint32_t lba_start;
|
||||
uint32_t num_sectors;
|
||||
} __attribute__((packed)) mbr_part_t;
|
||||
|
|
|
|||
20
src/chs.c
20
src/chs.c
|
|
@ -1,8 +1,20 @@
|
|||
#include "chs.h"
|
||||
|
||||
void decode_chs(const uint8_t* chs, uint16_t* cylinder, uint16_t* head, uint16_t* sector)
|
||||
void chs_decode(const chs_encoded_t* enc, chs_t* chs)
|
||||
{
|
||||
*head = chs[0];
|
||||
*sector = chs[1] & 0x3F;
|
||||
*cylinder = (((uint16_t)chs[1]) & 0xC0) << 2 | (uint16_t)chs[2];
|
||||
chs->head = enc->b[0];
|
||||
chs->sector = enc->b[1] & 0x3F;
|
||||
chs->cylinder = (((uint16_t)enc->b[1]) & 0xC0) << 2 | (uint16_t)enc->b[2];
|
||||
}
|
||||
|
||||
uint32_t chs_to_lba(const chs_conf_t* conf, const chs_t* chs)
|
||||
{
|
||||
return (chs->cylinder * conf->num_heads + chs->head) * conf->num_sectors + (chs->sector - 1);
|
||||
}
|
||||
|
||||
void lba_to_chs(const chs_conf_t* conf, uint32_t lba, chs_t* chs)
|
||||
{
|
||||
chs->head = lba / (conf->num_heads * conf->num_sectors);
|
||||
chs->cylinder = (lba / conf->num_sectors) % conf->num_heads;
|
||||
chs->sector = (lba % conf->num_sectors) + 1;
|
||||
}
|
||||
|
|
|
|||
|
|
@ -37,17 +37,17 @@ int main(int argc, char** argv)
|
|||
for (unsigned i = 0; i < 4; i++)
|
||||
{
|
||||
const mbr_part_t* part = &mbr.part_table[i];
|
||||
uint16_t cylinder, head, sector;
|
||||
chs_t chs_first, chs_last;
|
||||
|
||||
chs_decode(&part->chs_first, &chs_first);
|
||||
chs_decode(&part->chs_last, &chs_last);
|
||||
|
||||
printf("Partition:\t%u\n", i);
|
||||
printf("\tAttributes:\t%x (%s)\n", part->attributes, part->attributes & 0x80 ? "active" : "");
|
||||
printf("\tType:\t%x\n", part->type);
|
||||
|
||||
decode_chs(part->chs_first, &cylinder, &head, §or);
|
||||
printf("\tCHS First:\tC %u\tH %u\tS %u\n", cylinder, head, sector);
|
||||
|
||||
decode_chs(part->chs_last, &cylinder, &head, §or);
|
||||
printf("\tCHS Last:\tC %u\tH %u\tS %u\n", cylinder, head, sector);
|
||||
printf("\tCHS First:\tC %u\tH %u\tS %u\n", chs_first.cylinder, chs_first.head, chs_first.sector);
|
||||
printf("\tCHS Last:\tC %u\tH %u\tS %u\n", chs_last.cylinder, chs_last.head, chs_last.sector);
|
||||
|
||||
printf("\tLBA:\t%u\n", part->lba_start);
|
||||
printf("\tTotal Sectors:\t%u\n", part->num_sectors);
|
||||
|
|
|
|||
Loading…
Add table
Reference in a new issue