-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathbtld_disk.h
78 lines (69 loc) · 1.49 KB
/
btld_disk.h
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
typedef struct{
u64 cyl;
u64 head;
u64 sec;
}CHS;
typedef struct{
u8 flag; /* bootstrap flags */
u8 bhd; /* begin head */
u8 bsec; /* begin sector */
u8 bcyl; /* begin cylinder */
u8 type; /* partition type (see below) */
u8 ehd; /* end head */
u8 esec; /* end sec2r */
u8 ecyl; /* end cylinder */
u32 beg; /* absolute starting sectoff number */
u32 size; /* partition size in sec2rs */
} __attribute__((packed)) DOSpart;
typedef struct{
u8 boot[DOSPARTOFF];
DOSpart parts[NDOSPART];
u16 sign;
} __attribute__((packed)) DOSmbr;
typedef struct
{
u64 bs, ns;
u8 flag, id;
}Part;
typedef struct
{
DOSpart parts[NDOSPART];
u32 bps; // byte per sector
u32 spt; // sector per track
u32 hpc; // header per cylinder
u32 nsec; // number of sectors;
u32 type; // major minor;
int fd;
char *name;
}Disk;
typedef struct
{
u32 ext;
u32 self;
u8 code[DOSPARTOFF];
Part parts[NDOSPART];
u16 sign;
u16 zeros;
}MBR;
static Disk
diskopen(char *name, int flag)
{
char buf[512];
u64 nsec;
struct stat st;
Disk d = {.bps=512, .spt=63, .hpc=255,};
d.name = name;
assert((d.fd = open(d.name, flag)) != -1);
assert(fstat(d.fd, &st) != -1);
if(S_ISBLK(st.st_mode))
assert(ioctl(d.fd, BLKGETSIZE, &nsec) != -1);
else if(S_ISREG(st.st_mode))
nsec = st.st_size/d.bps;
else
assert(0);
d.nsec = nsec < FAT16_MAX ? nsec : FAT16_MAX;
assert(sizeof(buf) == read(d.fd, buf, sizeof(buf)));
memcpy(d.parts, buf+DOSPARTOFF, sizeof(d.parts));
assert(lseek(d.fd, 0, SEEK_SET) == 0);
return d;
}