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 "disk.h" // struct ata_s
9 #include "biosvar.h" // GET_GLOBAL
10 #include "cmos.h" // inb_cmos
11 #include "util.h" // dprintf
12 #include "ata.h" // process_ata_op
13 #include "ahci.h" // process_ahci_op
14 #include "usb-msc.h" // process_usb_op
15 #include "virtio-blk.h" // process_virtio_op
17 struct drives_s Drives VAR16VISIBLE;
20 getDrive(u8 exttype, u8 extdriveoffset)
22 if (extdriveoffset >= ARRAY_SIZE(Drives.idmap[0]))
24 struct drive_s *drive_gf = GET_GLOBAL(Drives.idmap[exttype][extdriveoffset]);
27 return GLOBALFLAT2GLOBAL(drive_gf);
30 int getDriveId(u8 exttype, struct drive_s *drive_g)
33 for (i = 0; i < ARRAY_SIZE(Drives.idmap[0]); i++)
34 if (getDrive(exttype, i) == drive_g)
40 /****************************************************************
41 * Disk geometry translation
42 ****************************************************************/
45 get_translation(struct drive_s *drive_g)
47 u8 type = GET_GLOBAL(drive_g->type);
48 if (! CONFIG_COREBOOT && type == DTYPE_ATA) {
49 // Emulators pass in the translation info via nvram.
50 u8 ataid = GET_GLOBAL(drive_g->cntl_id);
51 u8 channel = ataid / 2;
52 u8 translation = inb_cmos(CMOS_BIOS_DISKTRANSFLAG + channel/2);
53 translation >>= 2 * (ataid % 4);
58 // Otherwise use a heuristic to determine translation type.
59 u16 heads = GET_GLOBAL(drive_g->pchs.heads);
60 u16 cylinders = GET_GLOBAL(drive_g->pchs.cylinders);
61 u16 spt = GET_GLOBAL(drive_g->pchs.spt);
62 u64 sectors = GET_GLOBAL(drive_g->sectors);
63 u64 psectors = (u64)heads * cylinders * spt;
64 if (!heads || !cylinders || !spt || psectors > sectors)
65 // pchs doesn't look valid - use LBA.
66 return TRANSLATION_LBA;
68 if (cylinders <= 1024 && heads <= 16 && spt <= 63)
69 return TRANSLATION_NONE;
70 if (cylinders * heads <= 131072)
71 return TRANSLATION_LARGE;
72 return TRANSLATION_LBA;
76 setup_translation(struct drive_s *drive_g)
78 u8 translation = get_translation(drive_g);
79 SET_GLOBAL(drive_g->translation, translation);
81 u16 heads = GET_GLOBAL(drive_g->pchs.heads);
82 u16 cylinders = GET_GLOBAL(drive_g->pchs.cylinders);
83 u16 spt = GET_GLOBAL(drive_g->pchs.spt);
84 u64 sectors = GET_GLOBAL(drive_g->sectors);
85 const char *desc = NULL;
87 switch (translation) {
89 case TRANSLATION_NONE:
95 if (sectors > 63*255*1024) {
100 u32 sect = (u32)sectors / 63;
112 cylinders = sect / heads;
114 case TRANSLATION_RECHS:
116 // Take care not to overflow
121 cylinders = (u16)((u32)(cylinders)*16/15);
123 // then go through the large bitshift process
124 case TRANSLATION_LARGE:
125 if (translation == TRANSLATION_LARGE)
127 while (cylinders > 1024) {
131 // If we max out the head count
137 // clip to 1024 cylinders in lchs
138 if (cylinders > 1024)
140 dprintf(1, "drive %p: PCHS=%u/%d/%d translation=%s LCHS=%d/%d/%d s=%d\n"
142 , drive_g->pchs.cylinders, drive_g->pchs.heads, drive_g->pchs.spt
144 , cylinders, heads, spt
147 SET_GLOBAL(drive_g->lchs.heads, heads);
148 SET_GLOBAL(drive_g->lchs.cylinders, cylinders);
149 SET_GLOBAL(drive_g->lchs.spt, spt);
153 /****************************************************************
155 ****************************************************************/
157 // Fill in Fixed Disk Parameter Table (located in ebda).
159 fill_fdpt(struct drive_s *drive_g, int hdid)
164 u16 nlc = GET_GLOBAL(drive_g->lchs.cylinders);
165 u16 nlh = GET_GLOBAL(drive_g->lchs.heads);
166 u16 nlspt = GET_GLOBAL(drive_g->lchs.spt);
168 u16 npc = GET_GLOBAL(drive_g->pchs.cylinders);
169 u16 nph = GET_GLOBAL(drive_g->pchs.heads);
170 u16 npspt = GET_GLOBAL(drive_g->pchs.spt);
172 struct fdpt_s *fdpt = &get_ebda_ptr()->fdpt[hdid];
173 fdpt->precompensation = 0xffff;
174 fdpt->drive_control_byte = 0xc0 | ((nph > 8) << 3);
175 fdpt->landing_zone = npc;
176 fdpt->cylinders = nlc;
178 fdpt->sectors = nlspt;
180 if (nlc != npc || nlh != nph || nlspt != npspt) {
181 // Logical mapping present - use extended structure.
183 // complies with Phoenix style Translated Fixed Disk Parameter
185 fdpt->phys_cylinders = npc;
186 fdpt->phys_heads = nph;
187 fdpt->phys_sectors = npspt;
188 fdpt->a0h_signature = 0xa0;
190 // Checksum structure.
191 fdpt->checksum -= checksum(fdpt, sizeof(*fdpt));
195 SET_IVT(0x41, SEGOFF(get_ebda_seg(), offsetof(
196 struct extended_bios_data_area_s, fdpt[0])));
198 SET_IVT(0x46, SEGOFF(get_ebda_seg(), offsetof(
199 struct extended_bios_data_area_s, fdpt[1])));
202 // Find spot to add a drive
204 add_drive(struct drive_s **idmap, u8 *count, struct drive_s *drive_g)
206 if (*count >= ARRAY_SIZE(Drives.idmap[0])) {
210 idmap[*count] = drive_g;
216 map_hd_drive(struct drive_s *drive_g)
219 struct bios_data_area_s *bda = MAKE_FLATPTR(SEG_BDA, 0);
220 int hdid = bda->hdcount;
221 dprintf(3, "Mapping hd drive %p to %d\n", drive_g, hdid);
222 add_drive(Drives.idmap[EXTTYPE_HD], &bda->hdcount, drive_g);
224 // Fill "fdpt" structure.
225 fill_fdpt(drive_g, hdid);
230 map_cd_drive(struct drive_s *drive_g)
232 dprintf(3, "Mapping cd drive %p\n", drive_g);
233 add_drive(Drives.idmap[EXTTYPE_CD], &Drives.cdcount, drive_g);
238 map_floppy_drive(struct drive_s *drive_g)
240 dprintf(3, "Mapping floppy drive %p\n", drive_g);
241 add_drive(Drives.idmap[EXTTYPE_FLOPPY], &Drives.floppycount, drive_g);
243 // Update equipment word bits for floppy
244 if (Drives.floppycount == 1) {
245 // 1 drive, ready for boot
246 SETBITS_BDA(equipment_list_flags, 0x01);
247 SET_BDA(floppy_harddisk_info, 0x07);
248 } else if (Drives.floppycount >= 2) {
249 // 2 drives, ready for boot
250 SETBITS_BDA(equipment_list_flags, 0x41);
251 SET_BDA(floppy_harddisk_info, 0x77);
256 /****************************************************************
257 * 16bit calling interface
258 ****************************************************************/
260 // Execute a disk_op request.
262 process_op(struct disk_op_s *op)
265 u8 type = GET_GLOBAL(op->drive_g->type);
268 return process_floppy_op(op);
270 return process_ata_op(op);
272 return process_atapi_op(op);
274 return process_ramdisk_op(op);
276 return process_cdemu_op(op);
278 return process_usb_op(op);
280 return process_virtio_op(op);
282 return process_ahci_op(op);
285 return DISK_RET_EPARAM;
289 // Execute a "disk_op_s" request - this runs on a stack in the ebda.
291 __send_disk_op(struct disk_op_s *op_far, u16 op_seg)
293 struct disk_op_s dop;
294 memcpy_far(GET_SEG(SS), &dop
298 dprintf(DEBUG_HDL_13, "disk_op d=%p lba=%d buf=%p count=%d cmd=%d\n"
299 , dop.drive_g, (u32)dop.lba, dop.buf_fl
300 , dop.count, dop.command);
302 int status = process_op(&dop);
304 // Update count with total sectors transferred.
305 SET_FARVAR(op_seg, op_far->count, dop.count);
310 // Execute a "disk_op_s" request by jumping to a stack in the ebda.
312 send_disk_op(struct disk_op_s *op)
318 return stack_hop((u32)op, GET_SEG(SS), __send_disk_op);