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 u8 FloppyCount VAR16VISIBLE;
19 struct drive_s *IDMap[3][CONFIG_MAX_EXTDRIVE] VAR16VISIBLE;
20 u8 *bounce_buf_fl VAR16VISIBLE;
23 getDrive(u8 exttype, u8 extdriveoffset)
25 if (extdriveoffset >= ARRAY_SIZE(IDMap[0]))
27 struct drive_s *drive_gf = GET_GLOBAL(IDMap[exttype][extdriveoffset]);
30 return GLOBALFLAT2GLOBAL(drive_gf);
33 int getDriveId(u8 exttype, struct drive_s *drive_g)
36 for (i = 0; i < ARRAY_SIZE(IDMap[0]); i++)
37 if (getDrive(exttype, i) == drive_g)
42 int bounce_buf_init(void)
47 u8 *buf = malloc_low(CDROM_SECTOR_SIZE);
56 /****************************************************************
57 * Disk geometry translation
58 ****************************************************************/
61 get_translation(struct drive_s *drive_g)
63 u8 type = GET_GLOBAL(drive_g->type);
64 if (! CONFIG_COREBOOT && type == DTYPE_ATA) {
65 // Emulators pass in the translation info via nvram.
66 u8 ataid = GET_GLOBAL(drive_g->cntl_id);
67 u8 channel = ataid / 2;
68 u8 translation = inb_cmos(CMOS_BIOS_DISKTRANSFLAG + channel/2);
69 translation >>= 2 * (ataid % 4);
74 // Otherwise use a heuristic to determine translation type.
75 u16 heads = GET_GLOBAL(drive_g->pchs.heads);
76 u16 cylinders = GET_GLOBAL(drive_g->pchs.cylinders);
77 u16 spt = GET_GLOBAL(drive_g->pchs.spt);
78 u64 sectors = GET_GLOBAL(drive_g->sectors);
79 u64 psectors = (u64)heads * cylinders * spt;
80 if (!heads || !cylinders || !spt || psectors > sectors)
81 // pchs doesn't look valid - use LBA.
82 return TRANSLATION_LBA;
84 if (cylinders <= 1024 && heads <= 16 && spt <= 63)
85 return TRANSLATION_NONE;
86 if (cylinders * heads <= 131072)
87 return TRANSLATION_LARGE;
88 return TRANSLATION_LBA;
92 setup_translation(struct drive_s *drive_g)
94 u8 translation = get_translation(drive_g);
95 SET_GLOBAL(drive_g->translation, translation);
97 u16 heads = GET_GLOBAL(drive_g->pchs.heads);
98 u16 cylinders = GET_GLOBAL(drive_g->pchs.cylinders);
99 u16 spt = GET_GLOBAL(drive_g->pchs.spt);
100 u64 sectors = GET_GLOBAL(drive_g->sectors);
101 const char *desc = NULL;
103 switch (translation) {
105 case TRANSLATION_NONE:
108 case TRANSLATION_LBA:
111 if (sectors > 63*255*1024) {
116 u32 sect = (u32)sectors / 63;
128 cylinders = sect / heads;
130 case TRANSLATION_RECHS:
132 // Take care not to overflow
137 cylinders = (u16)((u32)(cylinders)*16/15);
139 // then go through the large bitshift process
140 case TRANSLATION_LARGE:
141 if (translation == TRANSLATION_LARGE)
143 while (cylinders > 1024) {
147 // If we max out the head count
153 // clip to 1024 cylinders in lchs
154 if (cylinders > 1024)
156 dprintf(1, "drive %p: PCHS=%u/%d/%d translation=%s LCHS=%d/%d/%d s=%d\n"
158 , drive_g->pchs.cylinders, drive_g->pchs.heads, drive_g->pchs.spt
160 , cylinders, heads, spt
163 SET_GLOBAL(drive_g->lchs.heads, heads);
164 SET_GLOBAL(drive_g->lchs.cylinders, cylinders);
165 SET_GLOBAL(drive_g->lchs.spt, spt);
169 /****************************************************************
171 ****************************************************************/
173 // Fill in Fixed Disk Parameter Table (located in ebda).
175 fill_fdpt(struct drive_s *drive_g, int hdid)
180 u16 nlc = GET_GLOBAL(drive_g->lchs.cylinders);
181 u16 nlh = GET_GLOBAL(drive_g->lchs.heads);
182 u16 nlspt = GET_GLOBAL(drive_g->lchs.spt);
184 u16 npc = GET_GLOBAL(drive_g->pchs.cylinders);
185 u16 nph = GET_GLOBAL(drive_g->pchs.heads);
186 u16 npspt = GET_GLOBAL(drive_g->pchs.spt);
188 struct fdpt_s *fdpt = &get_ebda_ptr()->fdpt[hdid];
189 fdpt->precompensation = 0xffff;
190 fdpt->drive_control_byte = 0xc0 | ((nph > 8) << 3);
191 fdpt->landing_zone = npc;
192 fdpt->cylinders = nlc;
194 fdpt->sectors = nlspt;
196 if (nlc != npc || nlh != nph || nlspt != npspt) {
197 // Logical mapping present - use extended structure.
199 // complies with Phoenix style Translated Fixed Disk Parameter
201 fdpt->phys_cylinders = npc;
202 fdpt->phys_heads = nph;
203 fdpt->phys_sectors = npspt;
204 fdpt->a0h_signature = 0xa0;
206 // Checksum structure.
207 fdpt->checksum -= checksum(fdpt, sizeof(*fdpt));
211 SET_IVT(0x41, SEGOFF(get_ebda_seg(), offsetof(
212 struct extended_bios_data_area_s, fdpt[0])));
214 SET_IVT(0x46, SEGOFF(get_ebda_seg(), offsetof(
215 struct extended_bios_data_area_s, fdpt[1])));
218 // Find spot to add a drive
220 add_drive(struct drive_s **idmap, u8 *count, struct drive_s *drive_g)
222 if (*count >= ARRAY_SIZE(IDMap[0])) {
226 idmap[*count] = drive_g;
232 map_hd_drive(struct drive_s *drive_g)
235 struct bios_data_area_s *bda = MAKE_FLATPTR(SEG_BDA, 0);
236 int hdid = bda->hdcount;
237 dprintf(3, "Mapping hd drive %p to %d\n", drive_g, hdid);
238 add_drive(IDMap[EXTTYPE_HD], &bda->hdcount, drive_g);
240 // Setup disk geometry translation.
241 setup_translation(drive_g);
243 // Fill "fdpt" structure.
244 fill_fdpt(drive_g, hdid);
249 map_cd_drive(struct drive_s *drive_g)
251 dprintf(3, "Mapping cd drive %p\n", drive_g);
252 add_drive(IDMap[EXTTYPE_CD], &CDCount, drive_g);
257 map_floppy_drive(struct drive_s *drive_g)
259 dprintf(3, "Mapping floppy drive %p\n", drive_g);
260 add_drive(IDMap[EXTTYPE_FLOPPY], &FloppyCount, drive_g);
262 // Update equipment word bits for floppy
263 if (FloppyCount == 1) {
264 // 1 drive, ready for boot
265 SETBITS_BDA(equipment_list_flags, 0x01);
266 SET_BDA(floppy_harddisk_info, 0x07);
267 } else if (FloppyCount >= 2) {
268 // 2 drives, ready for boot
269 SETBITS_BDA(equipment_list_flags, 0x41);
270 SET_BDA(floppy_harddisk_info, 0x77);
275 /****************************************************************
276 * 16bit calling interface
277 ****************************************************************/
279 // Execute a disk_op request.
281 process_op(struct disk_op_s *op)
284 u8 type = GET_GLOBAL(op->drive_g->type);
287 return process_floppy_op(op);
289 return process_ata_op(op);
291 return process_atapi_op(op);
293 return process_ramdisk_op(op);
295 return process_cdemu_op(op);
297 return process_usb_op(op);
299 return process_virtio_op(op);
301 return process_ahci_op(op);
304 return DISK_RET_EPARAM;
308 // Execute a "disk_op_s" request - this runs on a stack in the ebda.
310 __send_disk_op(struct disk_op_s *op_far, u16 op_seg)
312 struct disk_op_s dop;
313 memcpy_far(GET_SEG(SS), &dop
317 dprintf(DEBUG_HDL_13, "disk_op d=%p lba=%d buf=%p count=%d cmd=%d\n"
318 , dop.drive_g, (u32)dop.lba, dop.buf_fl
319 , dop.count, dop.command);
321 int status = process_op(&dop);
323 // Update count with total sectors transferred.
324 SET_FARVAR(op_seg, op_far->count, dop.count);
329 // Execute a "disk_op_s" request by jumping to a stack in the ebda.
331 send_disk_op(struct disk_op_s *op)
337 return stack_hop((u32)op, GET_SEG(SS), __send_disk_op);