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
14 struct drives_s Drives VAR16VISIBLE;
17 getDrive(u8 exttype, u8 extdriveoffset)
19 if (extdriveoffset >= ARRAY_SIZE(Drives.idmap[0]))
21 return RETRIEVE_GLOBAL_PTR(GET_GLOBAL(Drives.idmap[exttype][extdriveoffset]));
25 /****************************************************************
26 * Disk geometry translation
27 ****************************************************************/
30 get_translation(struct drive_s *drive_g)
32 u8 type = GET_GLOBAL(drive_g->type);
33 if (! CONFIG_COREBOOT && type == DTYPE_ATA) {
34 // Emulators pass in the translation info via nvram.
35 u8 ataid = GET_GLOBAL(drive_g->cntl_id);
36 u8 channel = ataid / 2;
37 u8 translation = inb_cmos(CMOS_BIOS_DISKTRANSFLAG + channel/2);
38 translation >>= 2 * (ataid % 4);
43 // On COREBOOT, use a heuristic to determine translation type.
44 u16 heads = GET_GLOBAL(drive_g->pchs.heads);
45 u16 cylinders = GET_GLOBAL(drive_g->pchs.cylinders);
46 u16 spt = GET_GLOBAL(drive_g->pchs.spt);
48 if (cylinders <= 1024 && heads <= 16 && spt <= 63)
49 return TRANSLATION_NONE;
50 if (cylinders * heads <= 131072)
51 return TRANSLATION_LARGE;
52 return TRANSLATION_LBA;
56 setup_translation(struct drive_s *drive_g)
58 u8 translation = get_translation(drive_g);
59 SET_GLOBAL(drive_g->translation, translation);
61 u8 ataid = GET_GLOBAL(drive_g->cntl_id);
62 u8 channel = ataid / 2;
64 u16 heads = GET_GLOBAL(drive_g->pchs.heads);
65 u16 cylinders = GET_GLOBAL(drive_g->pchs.cylinders);
66 u16 spt = GET_GLOBAL(drive_g->pchs.spt);
67 u64 sectors = GET_GLOBAL(drive_g->sectors);
68 const char *desc = NULL;
70 switch (translation) {
72 case TRANSLATION_NONE:
78 if (sectors > 63*255*1024) {
83 u32 sect = (u32)sectors / 63;
95 cylinders = sect / heads;
97 case TRANSLATION_RECHS:
99 // Take care not to overflow
104 cylinders = (u16)((u32)(cylinders)*16/15);
106 // then go through the large bitshift process
107 case TRANSLATION_LARGE:
108 if (translation == TRANSLATION_LARGE)
110 while (cylinders > 1024) {
114 // If we max out the head count
120 // clip to 1024 cylinders in lchs
121 if (cylinders > 1024)
123 dprintf(1, "ata%d-%d: PCHS=%u/%d/%d translation=%s LCHS=%d/%d/%d\n"
125 , drive_g->pchs.cylinders, drive_g->pchs.heads, drive_g->pchs.spt
127 , cylinders, heads, spt);
129 SET_GLOBAL(drive_g->lchs.heads, heads);
130 SET_GLOBAL(drive_g->lchs.cylinders, cylinders);
131 SET_GLOBAL(drive_g->lchs.spt, spt);
135 /****************************************************************
137 ****************************************************************/
139 // Fill in Fixed Disk Parameter Table (located in ebda).
141 fill_fdpt(struct drive_s *drive_g, int hdid)
146 u16 nlc = GET_GLOBAL(drive_g->lchs.cylinders);
147 u16 nlh = GET_GLOBAL(drive_g->lchs.heads);
148 u16 nlspt = GET_GLOBAL(drive_g->lchs.spt);
150 u16 npc = GET_GLOBAL(drive_g->pchs.cylinders);
151 u16 nph = GET_GLOBAL(drive_g->pchs.heads);
152 u16 npspt = GET_GLOBAL(drive_g->pchs.spt);
154 struct fdpt_s *fdpt = &get_ebda_ptr()->fdpt[hdid];
155 fdpt->precompensation = 0xffff;
156 fdpt->drive_control_byte = 0xc0 | ((nph > 8) << 3);
157 fdpt->landing_zone = npc;
158 fdpt->cylinders = nlc;
160 fdpt->sectors = nlspt;
162 if (nlc != npc || nlh != nph || nlspt != npspt) {
163 // Logical mapping present - use extended structure.
165 // complies with Phoenix style Translated Fixed Disk Parameter
167 fdpt->phys_cylinders = npc;
168 fdpt->phys_heads = nph;
169 fdpt->phys_sectors = npspt;
170 fdpt->a0h_signature = 0xa0;
172 // Checksum structure.
173 fdpt->checksum -= checksum(fdpt, sizeof(*fdpt));
177 SET_IVT(0x41, SEGOFF(get_ebda_seg(), offsetof(
178 struct extended_bios_data_area_s, fdpt[0])));
180 SET_IVT(0x46, SEGOFF(get_ebda_seg(), offsetof(
181 struct extended_bios_data_area_s, fdpt[1])));
184 // Map a drive (that was registered via add_bcv_hd)
186 map_hd_drive(struct drive_s *drive_g)
189 u8 hdcount = GET_BDA(hdcount);
190 if (hdcount >= ARRAY_SIZE(Drives.idmap[0])) {
194 dprintf(3, "Mapping hd drive %p to %d\n", drive_g, hdcount);
195 Drives.idmap[EXTTYPE_HD][hdcount] = STORE_GLOBAL_PTR(drive_g);
196 SET_BDA(hdcount, hdcount + 1);
198 // Fill "fdpt" structure.
199 fill_fdpt(drive_g, hdcount);
202 // Find spot to add a drive
204 add_ordered_drive(struct drive_s **idmap, u8 *count, struct drive_s *drive_g)
206 if (*count >= ARRAY_SIZE(Drives.idmap[0])) {
210 struct drive_s **pos = &idmap[*count];
212 if (CONFIG_THREADS) {
213 // Add to idmap with assured drive order.
214 struct drive_s **end = pos;
216 struct drive_s **prev = pos - 1;
219 struct drive_s *prevdrive = *prev;
220 if (prevdrive->type < drive_g->type
221 || (prevdrive->type == drive_g->type
222 && prevdrive->cntl_id < drive_g->cntl_id))
227 memmove(pos+1, pos, (void*)end-(void*)pos);
229 *pos = STORE_GLOBAL_PTR(drive_g);
234 map_cd_drive(struct drive_s *drive_g)
236 dprintf(3, "Mapping cd drive %p\n", drive_g);
237 add_ordered_drive(Drives.idmap[EXTTYPE_CD], &Drives.cdcount, drive_g);
242 map_floppy_drive(struct drive_s *drive_g)
245 dprintf(3, "Mapping floppy drive %p\n", drive_g);
246 add_ordered_drive(Drives.idmap[EXTTYPE_FLOPPY], &Drives.floppycount
249 // Update equipment word bits for floppy
250 if (Drives.floppycount == 1) {
251 // 1 drive, ready for boot
252 SETBITS_BDA(equipment_list_flags, 0x01);
253 SET_BDA(floppy_harddisk_info, 0x07);
254 } else if (Drives.floppycount >= 2) {
255 // 2 drives, ready for boot
256 SETBITS_BDA(equipment_list_flags, 0x41);
257 SET_BDA(floppy_harddisk_info, 0x77);
261 // Show a one line description (without trailing newline) of a drive.
263 describe_drive(struct drive_s *drive_g)
266 u8 type = GET_GLOBAL(drive_g->type);
269 describe_floppy(drive_g);
272 describe_ata(drive_g);
275 describe_atapi(drive_g);
278 describe_ramdisk(drive_g);
287 /****************************************************************
288 * 16bit calling interface
289 ****************************************************************/
291 // Execute a disk_op request.
293 process_op(struct disk_op_s *op)
296 u8 type = GET_GLOBAL(op->drive_g->type);
299 return process_floppy_op(op);
301 return process_ata_op(op);
303 return process_atapi_op(op);
305 return process_ramdisk_op(op);
307 return process_cdemu_op(op);
310 return DISK_RET_EPARAM;
314 // Execute a "disk_op_s" request - this runs on a stack in the ebda.
316 __send_disk_op(struct disk_op_s *op_far, u16 op_seg)
318 struct disk_op_s dop;
319 memcpy_far(GET_SEG(SS), &dop
323 dprintf(DEBUG_HDL_13, "disk_op d=%p lba=%d buf=%p count=%d cmd=%d\n"
324 , dop.drive_g, (u32)dop.lba, dop.buf_fl
325 , dop.count, dop.command);
327 int status = process_op(&dop);
329 // Update count with total sectors transferred.
330 SET_FARVAR(op_seg, op_far->count, dop.count);
335 // Execute a "disk_op_s" request by jumping to a stack in the ebda.
337 send_disk_op(struct disk_op_s *op)
343 return stack_hop((u32)op, GET_SEG(SS), 0, __send_disk_op);
347 /****************************************************************
349 ****************************************************************/
354 memset(&Drives, 0, sizeof(Drives));