-
Notifications
You must be signed in to change notification settings - Fork 9
/
Copy pathdisk.c
103 lines (89 loc) · 1.89 KB
/
disk.c
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
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
#include <stdint.h>
#include <stdio.h>
#include <stdlib.h>
#include "disk.h"
#include "util.h"
#define SECTOR_SIZE 512
Disk *create_disk_device()
{
Disk *disk = malloc(sizeof(Disk));
disk->storage = malloc(DISK_SIZE);
disk->data = 0;
disk->sec_count = 0;
disk->lba_low = 0;
disk->lba_mid = 0;
disk->lba_high = 0;
disk->drive_head = 0;
disk->status_command = 0x40;
return disk;
}
void load_data_to_disk(Disk *disk, FILE *f)
{
fseek(f, 0L, SEEK_END);
long binary_size = ftell(f);
fseek(f, 0L, SEEK_SET);
if (binary_size > DISK_SIZE)
{
printf("Binary file bigger than disk size.");
panic();
}
fread(disk->storage, 1, binary_size, f);
}
void set_sec_count(Disk *disk, uint8_t val)
{
disk->sec_count = val;
}
void set_lba_low(Disk *disk, uint8_t val)
{
disk->lba_low = val;
}
void set_lba_mid(Disk *disk, uint8_t val)
{
disk->lba_mid = val;
}
void set_lba_high(Disk *disk, uint8_t val)
{
disk->lba_high = val;
}
void set_drive_head(Disk *disk, uint8_t val)
{
disk->drive_head = val;
}
static void set_head_index(Disk *disk)
{
uint32_t head_index = 0 | disk->lba_low | (disk->lba_mid << 8) | (disk->lba_high << 16) |
((disk->drive_head & 0x0F) << 24);
disk->head_index = head_index * SECTOR_SIZE;
}
void set_disk_command(Disk *disk, uint8_t val)
{
switch (val)
{
case 0x20:
/* read sectors */
set_head_index(disk);
break;
default:
break;
}
}
uint8_t get_disk_status(Disk *disk)
{
return disk->status_command;
}
uint8_t read_disk_data8(Disk *disk)
{
uint8_t data = disk->storage[disk->head_index];
disk->head_index += 1;
return data;
}
uint32_t read_disk_data32(Disk *disk)
{
int i;
uint32_t data = 0;
for (i = 0; i < 4; i++)
{
data |= read_disk_data8(disk) << (8 * i);
}
return data;
}