-
Notifications
You must be signed in to change notification settings - Fork 0
/
hd.c
528 lines (458 loc) · 15.7 KB
/
hd.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
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
/*************************************************************************//**
*****************************************************************************
* @file hd.c
* @brief Hard disk (winchester) driver.
* The `device nr' in this file means minor device nr.
* @author Forrest Y. Yu
* @date 2005~2008
*****************************************************************************
*****************************************************************************/
#include "type.h"
#include "stdio.h"
#include "const.h"
#include "protect.h"
#include "string.h"
#include "fs.h"
#include "proc.h"
#include "tty.h"
#include "console.h"
#include "global.h"
#include "proto.h"
#include "hd.h"
PRIVATE void init_hd ();
PRIVATE void hd_open (int device);
PRIVATE void hd_close (int device);
PRIVATE void hd_rdwt (MESSAGE * p);
PRIVATE void hd_ioctl (MESSAGE * p);
PRIVATE void hd_cmd_out (struct hd_cmd* cmd);
PRIVATE void get_part_table (int drive, int sect_nr, struct part_ent * entry);
PRIVATE void partition (int device, int style);
/* PRIVATE void print_hdinfo (struct hd_info * hdi); */
PRIVATE int waitfor (int mask, int val, int timeout);
PRIVATE void interrupt_wait ();
PRIVATE void hd_identify (int drive);
PRIVATE void print_identify_info (u16* hdinfo);
PRIVATE u8 hd_status;
PRIVATE u8 hdbuf[SECTOR_SIZE * 2];
PRIVATE struct hd_info hd_info[1];
#define DRV_OF_DEV(dev) (dev <= MAX_PRIM ? \
dev / NR_PRIM_PER_DRIVE : \
(dev - MINOR_hd1a) / NR_SUB_PER_DRIVE)
/*****************************************************************************
* task_hd
*****************************************************************************/
/**
* Main loop of HD driver.
*
*****************************************************************************/
PUBLIC void task_hd()
{
MESSAGE msg;
init_hd();
while (1) {
send_recv(RECEIVE, ANY, &msg);
int src = msg.source;
switch (msg.type) {
case DEV_OPEN:
hd_open(msg.DEVICE);
break;
case DEV_CLOSE:
hd_close(msg.DEVICE);
break;
case DEV_READ:
case DEV_WRITE:
hd_rdwt(&msg);
break;
case DEV_IOCTL:
hd_ioctl(&msg);
break;
default:
dump_msg("HD driver::unknown msg", &msg);
spin("FS::main_loop (invalid msg.type)");
break;
}
send_recv(SEND, src, &msg);
}
}
/*****************************************************************************
* init_hd
*****************************************************************************/
/**
* <Ring 1> Check hard drive, set IRQ handler, enable IRQ and initialize data
* structures.
*****************************************************************************/
PRIVATE void init_hd()
{
int i;
/* Get the number of drives from the BIOS data area */
u8 * pNrDrives = (u8*)(0x475);
// printl("{HD} NrDrives:%d.\n", *pNrDrives);
assert(*pNrDrives);
put_irq_handler(AT_WINI_IRQ, hd_handler);
enable_irq(CASCADE_IRQ);
enable_irq(AT_WINI_IRQ);
for (i = 0; i < (sizeof(hd_info) / sizeof(hd_info[0])); i++)
memset(&hd_info[i], 0, sizeof(hd_info[0]));
hd_info[0].open_cnt = 0;
}
/*****************************************************************************
* hd_open
*****************************************************************************/
/**
* <Ring 1> This routine handles DEV_OPEN message. It identify the drive
* of the given device and read the partition table of the drive if it
* has not been read.
*
* @param device The device to be opened.
*****************************************************************************/
PRIVATE void hd_open(int device)
{
int drive = DRV_OF_DEV(device);
assert(drive == 0); /* only one drive */
hd_identify(drive);
if (hd_info[drive].open_cnt++ == 0) {
partition(drive * (NR_PART_PER_DRIVE + 1), P_PRIMARY);
/* print_hdinfo(&hd_info[drive]); */
}
printl("OS HD is running success\n");
}
/*****************************************************************************
* hd_close
*****************************************************************************/
/**
* <Ring 1> This routine handles DEV_CLOSE message.
*
* @param device The device to be opened.
*****************************************************************************/
PRIVATE void hd_close(int device)
{
int drive = DRV_OF_DEV(device);
assert(drive == 0); /* only one drive */
hd_info[drive].open_cnt--;
}
/*****************************************************************************
* hd_rdwt
*****************************************************************************/
/**
* <Ring 1> This routine handles DEV_READ and DEV_WRITE message.
*
* @param p Message ptr.
*****************************************************************************/
PRIVATE void hd_rdwt(MESSAGE * p)
{
int drive = DRV_OF_DEV(p->DEVICE);
u64 pos = p->POSITION;
assert((pos >> SECTOR_SIZE_SHIFT) < (1 << 31));
/**
* We only allow to R/W from a SECTOR boundary:
*/
assert((pos & 0x1FF) == 0);
u32 sect_nr = (u32)(pos >> SECTOR_SIZE_SHIFT); /* pos / SECTOR_SIZE */
int logidx = (p->DEVICE - MINOR_hd1a) % NR_SUB_PER_DRIVE;
sect_nr += p->DEVICE < MAX_PRIM ?
hd_info[drive].primary[p->DEVICE].base :
hd_info[drive].logical[logidx].base;
struct hd_cmd cmd;
cmd.features = 0;
cmd.count = (p->CNT + SECTOR_SIZE - 1) / SECTOR_SIZE;
cmd.lba_low = sect_nr & 0xFF;
cmd.lba_mid = (sect_nr >> 8) & 0xFF;
cmd.lba_high = (sect_nr >> 16) & 0xFF;
cmd.device = MAKE_DEVICE_REG(1, drive, (sect_nr >> 24) & 0xF);
cmd.command = (p->type == DEV_READ) ? ATA_READ : ATA_WRITE;
hd_cmd_out(&cmd);
int bytes_left = p->CNT;
void * la = (void*)va2la(p->PROC_NR, p->BUF);
while (bytes_left) {
int bytes = min(SECTOR_SIZE, bytes_left);
if (p->type == DEV_READ) {
interrupt_wait();
port_read(REG_DATA, hdbuf, SECTOR_SIZE);
phys_copy(la, (void*)va2la(TASK_HD, hdbuf), bytes);
}
else {
if (!waitfor(STATUS_DRQ, STATUS_DRQ, HD_TIMEOUT))
panic("hd writing error.");
port_write(REG_DATA, la, bytes);
interrupt_wait();
}
bytes_left -= SECTOR_SIZE;
la += SECTOR_SIZE;
}
}
/*****************************************************************************
* hd_ioctl
*****************************************************************************/
/**
* <Ring 1> This routine handles the DEV_IOCTL message.
*
* @param p Ptr to the MESSAGE.
*****************************************************************************/
PRIVATE void hd_ioctl(MESSAGE * p)
{
int device = p->DEVICE;
int drive = DRV_OF_DEV(device);
struct hd_info * hdi = &hd_info[drive];
if (p->REQUEST == DIOCTL_GET_GEO) {
void * dst = va2la(p->PROC_NR, p->BUF);
void * src = va2la(TASK_HD,
device < MAX_PRIM ?
&hdi->primary[device] :
&hdi->logical[(device - MINOR_hd1a) %
NR_SUB_PER_DRIVE]);
phys_copy(dst, src, sizeof(struct part_info));
}
else {
assert(0);
}
}
/*****************************************************************************
* get_part_table
*****************************************************************************/
/**
* <Ring 1> Get a partition table of a drive.
*
* @param drive Drive nr (0 for the 1st disk, 1 for the 2nd, ...)n
* @param sect_nr The sector at which the partition table is located.
* @param entry Ptr to part_ent struct.
*****************************************************************************/
PRIVATE void get_part_table(int drive, int sect_nr, struct part_ent * entry)
{
struct hd_cmd cmd;
cmd.features = 0;
cmd.count = 1;
cmd.lba_low = sect_nr & 0xFF;
cmd.lba_mid = (sect_nr >> 8) & 0xFF;
cmd.lba_high = (sect_nr >> 16) & 0xFF;
cmd.device = MAKE_DEVICE_REG(1, /* LBA mode*/
drive,
(sect_nr >> 24) & 0xF);
cmd.command = ATA_READ;
hd_cmd_out(&cmd);
interrupt_wait();
port_read(REG_DATA, hdbuf, SECTOR_SIZE);
memcpy(entry,
hdbuf + PARTITION_TABLE_OFFSET,
sizeof(struct part_ent) * NR_PART_PER_DRIVE);
}
/*****************************************************************************
* partition
*****************************************************************************/
/**
* <Ring 1> This routine is called when a device is opened. It reads the
* partition table(s) and fills the hd_info struct.
*
* @param device Device nr.
* @param style P_PRIMARY or P_EXTENDED.
*****************************************************************************/
PRIVATE void partition(int device, int style)
{
int i;
int drive = DRV_OF_DEV(device);
struct hd_info * hdi = &hd_info[drive];
struct part_ent part_tbl[NR_SUB_PER_DRIVE];
if (style == P_PRIMARY) {
get_part_table(drive, drive, part_tbl);
int nr_prim_parts = 0;
for (i = 0; i < NR_PART_PER_DRIVE; i++) { /* 0~3 */
if (part_tbl[i].sys_id == NO_PART)
continue;
nr_prim_parts++;
int dev_nr = i + 1; /* 1~4 */
hdi->primary[dev_nr].base = part_tbl[i].start_sect;
hdi->primary[dev_nr].size = part_tbl[i].nr_sects;
if (part_tbl[i].sys_id == EXT_PART) /* extended */
partition(device + dev_nr, P_EXTENDED);
}
assert(nr_prim_parts != 0);
}
else if (style == P_EXTENDED) {
int j = device % NR_PRIM_PER_DRIVE; /* 1~4 */
int ext_start_sect = hdi->primary[j].base;
int s = ext_start_sect;
int nr_1st_sub = (j - 1) * NR_SUB_PER_PART; /* 0/16/32/48 */
for (i = 0; i < NR_SUB_PER_PART; i++) {
int dev_nr = nr_1st_sub + i;/* 0~15/16~31/32~47/48~63 */
get_part_table(drive, s, part_tbl);
hdi->logical[dev_nr].base = s + part_tbl[0].start_sect;
hdi->logical[dev_nr].size = part_tbl[0].nr_sects;
s = ext_start_sect + part_tbl[1].start_sect;
/* no more logical partitions
in this extended partition */
if (part_tbl[1].sys_id == NO_PART)
break;
}
}
else {
assert(0);
}
}
/* /\***************************************************************************** */
/* * print_hdinfo */
/* *****************************************************************************\/ */
/* /\** */
/* * <Ring 1> Print disk info. */
/* * */
/* * @param hdi Ptr to struct hd_info. */
/* *****************************************************************************\/ */
/* PRIVATE void print_hdinfo(struct hd_info * hdi) */
/* { */
/* int i; */
/* for (i = 0; i < NR_PART_PER_DRIVE + 1; i++) { */
/* printl("{HD} %sPART_%d: base %d(0x%x), size %d(0x%x) (in sector)\n", */
/* i == 0 ? " " : " ", */
/* i, */
/* hdi->primary[i].base, */
/* hdi->primary[i].base, */
/* hdi->primary[i].size, */
/* hdi->primary[i].size); */
/* } */
/* for (i = 0; i < NR_SUB_PER_DRIVE; i++) { */
/* if (hdi->logical[i].size == 0) */
/* continue; */
/* printl("{HD} " */
/* "%d: base %d(0x%x), size %d(0x%x) (in sector)\n", */
/* i, */
/* hdi->logical[i].base, */
/* hdi->logical[i].base, */
/* hdi->logical[i].size, */
/* hdi->logical[i].size); */
/* } */
/* } */
/*****************************************************************************
* hd_identify
*****************************************************************************/
/**
* <Ring 1> Get the disk information.
*
* @param drive Drive Nr.
*****************************************************************************/
PRIVATE void hd_identify(int drive)
{
struct hd_cmd cmd;
cmd.device = MAKE_DEVICE_REG(0, drive, 0);
cmd.command = ATA_IDENTIFY;
hd_cmd_out(&cmd);
interrupt_wait();
port_read(REG_DATA, hdbuf, SECTOR_SIZE);
// print_identify_info((u16*)hdbuf);
u16* hdinfo = (u16*)hdbuf;
hd_info[drive].primary[0].base = 0;
/* Total Nr of User Addressable Sectors */
hd_info[drive].primary[0].size = ((int)hdinfo[61] << 16) + hdinfo[60];
}
/*****************************************************************************
* print_identify_info
*****************************************************************************/
/**
* <Ring 1> Print the hdinfo retrieved via ATA_IDENTIFY command.
*
* @param hdinfo The buffer read from the disk i/o port.
*****************************************************************************/
PRIVATE void print_identify_info(u16* hdinfo)
{
int i, k;
char s[64];
struct iden_info_ascii {
int idx;
int len;
char * desc;
} iinfo[] = {{10, 20, "HD SN"}, /* Serial number in ASCII */
{27, 40, "HD Model"} /* Model number in ASCII */ };
for (k = 0; k < sizeof(iinfo)/sizeof(iinfo[0]); k++) {
char * p = (char*)&hdinfo[iinfo[k].idx];
for (i = 0; i < iinfo[k].len/2; i++) {
s[i*2+1] = *p++;
s[i*2] = *p++;
}
s[i*2] = 0;
printl("{HD} %s: %s\n", iinfo[k].desc, s);
}
int capabilities = hdinfo[49];
printl("{HD} LBA supported: %s\n",
(capabilities & 0x0200) ? "Yes" : "No");
int cmd_set_supported = hdinfo[83];
printl("{HD} LBA48 supported: %s\n",
(cmd_set_supported & 0x0400) ? "Yes" : "No");
int sectors = ((int)hdinfo[61] << 16) + hdinfo[60];
printl("{HD} HD size: %dMB\n", sectors * 512 / 1000000);
}
/*****************************************************************************
* hd_cmd_out
*****************************************************************************/
/**
* <Ring 1> Output a command to HD controller.
*
* @param cmd The command struct ptr.
*****************************************************************************/
PRIVATE void hd_cmd_out(struct hd_cmd* cmd)
{
/**
* For all commands, the host must first check if BSY=1,
* and should proceed no further unless and until BSY=0
*/
if (!waitfor(STATUS_BSY, 0, HD_TIMEOUT))
panic("hd error.");
/* Activate the Interrupt Enable (nIEN) bit */
out_byte(REG_DEV_CTRL, 0);
/* Load required parameters in the Command Block Registers */
out_byte(REG_FEATURES, cmd->features);
out_byte(REG_NSECTOR, cmd->count);
out_byte(REG_LBA_LOW, cmd->lba_low);
out_byte(REG_LBA_MID, cmd->lba_mid);
out_byte(REG_LBA_HIGH, cmd->lba_high);
out_byte(REG_DEVICE, cmd->device);
/* Write the command code to the Command Register */
out_byte(REG_CMD, cmd->command);
}
/*****************************************************************************
* interrupt_wait
*****************************************************************************/
/**
* <Ring 1> Wait until a disk interrupt occurs.
*
*****************************************************************************/
PRIVATE void interrupt_wait()
{
MESSAGE msg;
send_recv(RECEIVE, INTERRUPT, &msg);
}
/*****************************************************************************
* waitfor
*****************************************************************************/
/**
* <Ring 1> Wait for a certain status.
*
* @param mask Status mask.
* @param val Required status.
* @param timeout Timeout in milliseconds.
*
* @return One if sucess, zero if timeout.
*****************************************************************************/
PRIVATE int waitfor(int mask, int val, int timeout)
{
int t = get_ticks();
while(((get_ticks() - t) * 1000 / HZ) < timeout)
if ((in_byte(REG_STATUS) & mask) == val)
return 1;
return 0;
}
/*****************************************************************************
* hd_handler
*****************************************************************************/
/**
* <Ring 0> Interrupt handler.
*
* @param irq IRQ nr of the disk interrupt.
*****************************************************************************/
PUBLIC void hd_handler(int irq)
{
/*
* Interrupts are cleared when the host
* - reads the Status Register,
* - issues a reset, or
* - writes to the Command Register.
*/
hd_status = in_byte(REG_STATUS);
inform_int(TASK_HD);
}