1 // Disk setup and access
3 // Copyright (C) 2008,2009 Kevin O'Connor <kevin@koconnor.net>
4 // Copyright (C) 2002 MandrakeSoft S.A.
6 // This file may be distributed under the terms of the GNU LGPLv3 license.
8 #include "biosvar.h" // GET_GLOBAL
9 #include "block.h" // process_op
10 #include "hw/ata.h" // process_ata_op
11 #include "hw/ahci.h" // process_ahci_op
12 #include "hw/blockcmd.h" // cdb_*
13 #include "hw/pci.h" // pci_bdf_to_bus
14 #include "hw/rtc.h" // rtc_read
15 #include "hw/virtio-blk.h" // process_virtio_blk_op
16 #include "malloc.h" // malloc_low
17 #include "output.h" // dprintf
18 #include "stacks.h" // stack_hop
19 #include "std/disk.h" // struct dpte_s
20 #include "string.h" // checksum
21 #include "util.h" // process_floppy_op
23 u8 FloppyCount VARFSEG;
25 struct drive_s *IDMap[3][BUILD_MAX_EXTDRIVE] VARFSEG;
26 u8 *bounce_buf_fl VARFSEG;
29 getDrive(u8 exttype, u8 extdriveoffset)
31 if (extdriveoffset >= ARRAY_SIZE(IDMap[0]))
33 return GET_GLOBAL(IDMap[exttype][extdriveoffset]);
36 int getDriveId(u8 exttype, struct drive_s *drive)
40 for (i = 0; i < ARRAY_SIZE(IDMap[0]); i++)
41 if (getDrive(exttype, i) == drive)
46 int create_bounce_buf(void)
51 u8 *buf = malloc_low(CDROM_SECTOR_SIZE);
60 /****************************************************************
61 * Disk geometry translation
62 ****************************************************************/
65 get_translation(struct drive_s *drive)
67 u8 type = drive->type;
68 if (CONFIG_QEMU && type == DTYPE_ATA) {
69 // Emulators pass in the translation info via nvram.
70 u8 ataid = drive->cntl_id;
71 u8 channel = ataid / 2;
72 u8 translation = rtc_read(CMOS_BIOS_DISKTRANSFLAG + channel/2);
73 translation >>= 2 * (ataid % 4);
78 // Otherwise use a heuristic to determine translation type.
79 u16 heads = drive->pchs.head;
80 u16 cylinders = drive->pchs.cylinder;
81 u16 spt = drive->pchs.sector;
82 u64 sectors = drive->sectors;
83 u64 psectors = (u64)heads * cylinders * spt;
84 if (!heads || !cylinders || !spt || psectors > sectors)
85 // pchs doesn't look valid - use LBA.
86 return TRANSLATION_LBA;
88 if (cylinders <= 1024 && heads <= 16 && spt <= 63)
89 return TRANSLATION_NONE;
90 if (cylinders * heads <= 131072)
91 return TRANSLATION_LARGE;
92 return TRANSLATION_LBA;
96 setup_translation(struct drive_s *drive)
98 u8 translation = get_translation(drive);
99 drive->translation = translation;
101 u16 heads = drive->pchs.head ;
102 u16 cylinders = drive->pchs.cylinder;
103 u16 spt = drive->pchs.sector;
104 u64 sectors = drive->sectors;
105 const char *desc = NULL;
107 switch (translation) {
109 case TRANSLATION_NONE:
112 case TRANSLATION_LBA:
115 if (sectors > 63*255*1024) {
120 u32 sect = (u32)sectors / 63;
132 cylinders = sect / heads;
134 case TRANSLATION_RECHS:
136 // Take care not to overflow
141 cylinders = (u16)((u32)(cylinders)*16/15);
143 // then go through the large bitshift process
144 case TRANSLATION_LARGE:
145 if (translation == TRANSLATION_LARGE)
147 while (cylinders > 1024) {
151 // If we max out the head count
157 // clip to 1024 cylinders in lchs
158 if (cylinders > 1024)
160 dprintf(1, "drive %p: PCHS=%u/%d/%d translation=%s LCHS=%d/%d/%d s=%d\n"
162 , drive->pchs.cylinder, drive->pchs.head, drive->pchs.sector
164 , cylinders, heads, spt
167 drive->lchs.head = heads;
168 drive->lchs.cylinder = cylinders;
169 drive->lchs.sector = spt;
173 /****************************************************************
175 ****************************************************************/
177 // Fill in Fixed Disk Parameter Table (located in ebda).
179 fill_fdpt(struct drive_s *drive, int hdid)
184 u16 nlc = drive->lchs.cylinder;
185 u16 nlh = drive->lchs.head;
186 u16 nls = drive->lchs.sector;
188 u16 npc = drive->pchs.cylinder;
189 u16 nph = drive->pchs.head;
190 u16 nps = drive->pchs.sector;
192 struct fdpt_s *fdpt = &get_ebda_ptr()->fdpt[hdid];
193 fdpt->precompensation = 0xffff;
194 fdpt->drive_control_byte = 0xc0 | ((nph > 8) << 3);
195 fdpt->landing_zone = npc;
196 fdpt->cylinders = nlc;
200 if (nlc != npc || nlh != nph || nls != nps) {
201 // Logical mapping present - use extended structure.
203 // complies with Phoenix style Translated Fixed Disk Parameter
205 fdpt->phys_cylinders = npc;
206 fdpt->phys_heads = nph;
207 fdpt->phys_sectors = nps;
208 fdpt->a0h_signature = 0xa0;
210 // Checksum structure.
211 fdpt->checksum -= checksum(fdpt, sizeof(*fdpt));
215 SET_IVT(0x41, SEGOFF(get_ebda_seg(), offsetof(
216 struct extended_bios_data_area_s, fdpt[0])));
218 SET_IVT(0x46, SEGOFF(get_ebda_seg(), offsetof(
219 struct extended_bios_data_area_s, fdpt[1])));
222 // Find spot to add a drive
224 add_drive(struct drive_s **idmap, u8 *count, struct drive_s *drive)
226 if (*count >= ARRAY_SIZE(IDMap[0])) {
230 idmap[*count] = drive;
236 map_hd_drive(struct drive_s *drive)
239 struct bios_data_area_s *bda = MAKE_FLATPTR(SEG_BDA, 0);
240 int hdid = bda->hdcount;
241 dprintf(3, "Mapping hd drive %p to %d\n", drive, hdid);
242 add_drive(IDMap[EXTTYPE_HD], &bda->hdcount, drive);
244 // Setup disk geometry translation.
245 setup_translation(drive);
247 // Fill "fdpt" structure.
248 fill_fdpt(drive, hdid);
253 map_cd_drive(struct drive_s *drive)
256 dprintf(3, "Mapping cd drive %p\n", drive);
257 add_drive(IDMap[EXTTYPE_CD], &CDCount, drive);
262 map_floppy_drive(struct drive_s *drive)
265 dprintf(3, "Mapping floppy drive %p\n", drive);
266 add_drive(IDMap[EXTTYPE_FLOPPY], &FloppyCount, drive);
268 // Update equipment word bits for floppy
269 if (FloppyCount == 1) {
270 // 1 drive, ready for boot
271 set_equipment_flags(0x41, 0x01);
272 SET_BDA(floppy_harddisk_info, 0x07);
273 } else if (FloppyCount >= 2) {
274 // 2 drives, ready for boot
275 set_equipment_flags(0x41, 0x41);
276 SET_BDA(floppy_harddisk_info, 0x77);
281 /****************************************************************
282 * Extended Disk Drive (EDD) get drive parameters
283 ****************************************************************/
286 fill_generic_edd(u16 seg, struct int13dpt_s *param_far, struct drive_s *drive_gf
287 , u32 dpte_so, char *iface_type
288 , int bdf, u8 channel, u16 iobase, u64 device_path)
290 u16 size = GET_FARVAR(seg, param_far->size);
291 u16 t13 = size == 74;
293 // Buffer is too small
295 return DISK_RET_EPARAM;
299 u8 type = GET_GLOBALFLAT(drive_gf->type);
300 u16 npc = GET_GLOBALFLAT(drive_gf->pchs.cylinder);
301 u16 nph = GET_GLOBALFLAT(drive_gf->pchs.head);
302 u16 nps = GET_GLOBALFLAT(drive_gf->pchs.sector);
303 u64 lba = GET_GLOBALFLAT(drive_gf->sectors);
304 u16 blksize = GET_GLOBALFLAT(drive_gf->blksize);
306 dprintf(DEBUG_HDL_13, "disk_1348 size=%d t=%d chs=%d,%d,%d lba=%d bs=%d\n"
307 , size, type, npc, nph, nps, (u32)lba, blksize);
309 SET_FARVAR(seg, param_far->size, 26);
310 if (lba == (u64)-1) {
311 // 0x74 = removable, media change, lockable, max values
312 SET_FARVAR(seg, param_far->infos, 0x74);
313 SET_FARVAR(seg, param_far->cylinders, 0xffffffff);
314 SET_FARVAR(seg, param_far->heads, 0xffffffff);
315 SET_FARVAR(seg, param_far->spt, 0xffffffff);
317 if (lba > (u64)nps*nph*0x3fff) {
318 SET_FARVAR(seg, param_far->infos, 0x00); // geometry is invalid
319 SET_FARVAR(seg, param_far->cylinders, 0x3fff);
321 SET_FARVAR(seg, param_far->infos, 0x02); // geometry is valid
322 SET_FARVAR(seg, param_far->cylinders, (u32)npc);
324 SET_FARVAR(seg, param_far->heads, (u32)nph);
325 SET_FARVAR(seg, param_far->spt, (u32)nps);
327 SET_FARVAR(seg, param_far->sector_count, lba);
328 SET_FARVAR(seg, param_far->blksize, blksize);
330 if (size < 30 || !dpte_so)
331 return DISK_RET_SUCCESS;
335 SET_FARVAR(seg, param_far->size, 30);
336 SET_FARVAR(seg, param_far->dpte.segoff, dpte_so);
338 if (size < 66 || !iface_type)
339 return DISK_RET_SUCCESS;
342 SET_FARVAR(seg, param_far->key, 0xbedd);
343 SET_FARVAR(seg, param_far->dpi_length, t13 ? 44 : 36);
344 SET_FARVAR(seg, param_far->reserved1, 0);
345 SET_FARVAR(seg, param_far->reserved2, 0);
348 for (i=0; i<sizeof(param_far->iface_type); i++)
349 SET_FARVAR(seg, param_far->iface_type[i], GET_GLOBAL(iface_type[i]));
352 SET_FARVAR(seg, param_far->host_bus[0], 'P');
353 SET_FARVAR(seg, param_far->host_bus[1], 'C');
354 SET_FARVAR(seg, param_far->host_bus[2], 'I');
355 SET_FARVAR(seg, param_far->host_bus[3], ' ');
357 u32 path = (pci_bdf_to_bus(bdf) | (pci_bdf_to_dev(bdf) << 8)
358 | (pci_bdf_to_fn(bdf) << 16));
360 path |= channel << 24;
362 SET_FARVAR(seg, param_far->iface_path, path);
365 SET_FARVAR(seg, param_far->host_bus[0], 'I');
366 SET_FARVAR(seg, param_far->host_bus[1], 'S');
367 SET_FARVAR(seg, param_far->host_bus[2], 'A');
368 SET_FARVAR(seg, param_far->host_bus[3], ' ');
370 SET_FARVAR(seg, param_far->iface_path, iobase);
374 SET_FARVAR(seg, param_far->t13.device_path[0], device_path);
375 SET_FARVAR(seg, param_far->t13.device_path[1], 0);
377 SET_FARVAR(seg, param_far->t13.checksum
378 , -checksum_far(seg, (void*)param_far+30, 43));
380 SET_FARVAR(seg, param_far->phoenix.device_path, device_path);
382 SET_FARVAR(seg, param_far->phoenix.checksum
383 , -checksum_far(seg, (void*)param_far+30, 35));
386 return DISK_RET_SUCCESS;
389 struct dpte_s DefaultDPTE VARLOW;
392 fill_ata_edd(u16 seg, struct int13dpt_s *param_far, struct drive_s *drive_gf)
395 return DISK_RET_EPARAM;
398 struct atadrive_s *adrive_gf = container_of(
399 drive_gf, struct atadrive_s, drive);
400 struct ata_channel_s *chan_gf = GET_GLOBALFLAT(adrive_gf->chan_gf);
401 u8 slave = GET_GLOBALFLAT(adrive_gf->slave);
402 u16 iobase2 = GET_GLOBALFLAT(chan_gf->iobase2);
403 u8 irq = GET_GLOBALFLAT(chan_gf->irq);
404 u16 iobase1 = GET_GLOBALFLAT(chan_gf->iobase1);
405 int bdf = GET_GLOBALFLAT(chan_gf->pci_bdf);
406 u8 channel = GET_GLOBALFLAT(chan_gf->chanid);
409 if (GET_GLOBALFLAT(drive_gf->type) == DTYPE_ATA) {
410 u8 translation = GET_GLOBALFLAT(drive_gf->translation);
411 if (translation != TRANSLATION_NONE) {
412 options |= 1<<3; // CHS translation
413 if (translation == TRANSLATION_LBA)
415 if (translation == TRANSLATION_RECHS)
420 options |= 1<<5; // removable device
421 options |= 1<<6; // atapi device
423 options |= 1<<4; // lba translation
424 if (CONFIG_ATA_PIO32)
427 SET_LOW(DefaultDPTE.iobase1, iobase1);
428 SET_LOW(DefaultDPTE.iobase2, iobase2 + ATA_CB_DC);
429 SET_LOW(DefaultDPTE.prefix, ((slave ? ATA_CB_DH_DEV1 : ATA_CB_DH_DEV0)
431 SET_LOW(DefaultDPTE.unused, 0xcb);
432 SET_LOW(DefaultDPTE.irq, irq);
433 SET_LOW(DefaultDPTE.blkcount, 1);
434 SET_LOW(DefaultDPTE.dma, 0);
435 SET_LOW(DefaultDPTE.pio, 0);
436 SET_LOW(DefaultDPTE.options, options);
437 SET_LOW(DefaultDPTE.reserved, 0);
438 SET_LOW(DefaultDPTE.revision, 0x11);
440 u8 sum = checksum_far(SEG_LOW, &DefaultDPTE, 15);
441 SET_LOW(DefaultDPTE.checksum, -sum);
443 return fill_generic_edd(
444 seg, param_far, drive_gf, SEGOFF(SEG_LOW, (u32)&DefaultDPTE).segoff
445 , "ATA ", bdf, channel, iobase1, slave);
449 fill_edd(u16 seg, struct int13dpt_s *param_far, struct drive_s *drive_gf)
451 switch (GET_GLOBALFLAT(drive_gf->type)) {
453 case DTYPE_ATA_ATAPI:
454 return fill_ata_edd(seg, param_far, drive_gf);
455 case DTYPE_VIRTIO_BLK:
456 case DTYPE_VIRTIO_SCSI:
457 return fill_generic_edd(
458 seg, param_far, drive_gf, 0xffffffff
459 , "SCSI ", GET_GLOBALFLAT(drive_gf->cntl_id), 0, 0, 0);
461 return fill_generic_edd(seg, param_far, drive_gf, 0, NULL, 0, 0, 0, 0);
466 /****************************************************************
467 * 16bit calling interface
468 ****************************************************************/
471 process_atapi_op(struct disk_op_s *op)
473 switch (op->command) {
476 return DISK_RET_EWRITEPROTECT;
478 return scsi_process_op(op);
482 // Execute a disk_op request.
484 process_op(struct disk_op_s *op)
487 int ret, origcount = op->count;
488 if (origcount * GET_GLOBALFLAT(op->drive_gf->blksize) > 64*1024) {
490 return DISK_RET_EBOUNDARY;
492 u8 type = GET_GLOBALFLAT(op->drive_gf->type);
495 ret = process_floppy_op(op);
498 ret = process_ata_op(op);
501 ret = process_ramdisk_op(op);
504 ret = process_cdemu_op(op);
506 case DTYPE_VIRTIO_BLK:
507 ret = process_virtio_blk_op(op);
510 extern void _cfunc32flat_process_ahci_op(void);
511 ret = call32(_cfunc32flat_process_ahci_op
512 , (u32)MAKE_FLATPTR(GET_SEG(SS), op), DISK_RET_EPARAM);
514 case DTYPE_ATA_ATAPI:
515 ret = process_atapi_op(op);
517 case DTYPE_AHCI_ATAPI: ;
518 extern void _cfunc32flat_process_atapi_op(void);
519 ret = call32(_cfunc32flat_process_atapi_op
520 , (u32)MAKE_FLATPTR(GET_SEG(SS), op), DISK_RET_EPARAM);
523 extern void _cfunc32flat_process_sdcard_op(void);
524 ret = call32(_cfunc32flat_process_sdcard_op
525 , (u32)MAKE_FLATPTR(GET_SEG(SS), op), DISK_RET_EPARAM);
529 case DTYPE_VIRTIO_SCSI:
533 ret = scsi_process_op(op);
538 extern void _cfunc32flat_scsi_process_op(void);
539 ret = call32(_cfunc32flat_scsi_process_op
540 , (u32)MAKE_FLATPTR(GET_SEG(SS), op), DISK_RET_EPARAM);
543 ret = DISK_RET_EPARAM;
546 if (ret && op->count == origcount)
547 // If the count hasn't changed on error, assume no data transferred.
552 // Execute a "disk_op_s" request - this runs on the extra stack.
554 __send_disk_op(struct disk_op_s *op_far, u16 op_seg)
556 struct disk_op_s dop;
557 memcpy_far(GET_SEG(SS), &dop
561 dprintf(DEBUG_HDL_13, "disk_op d=%p lba=%d buf=%p count=%d cmd=%d\n"
562 , dop.drive_gf, (u32)dop.lba, dop.buf_fl
563 , dop.count, dop.command);
565 int status = process_op(&dop);
567 // Update count with total sectors transferred.
568 SET_FARVAR(op_seg, op_far->count, dop.count);
573 // Execute a "disk_op_s" request by jumping to the extra 16bit stack.
575 send_disk_op(struct disk_op_s *op)
581 return stack_hop((u32)op, GET_SEG(SS), __send_disk_op);