Force use of indirect function calls in inline assembler.
[seabios.git] / src / block.c
1 // Disk setup and access
2 //
3 // Copyright (C) 2008,2009  Kevin O'Connor <kevin@koconnor.net>
4 // Copyright (C) 2002  MandrakeSoft S.A.
5 //
6 // This file may be distributed under the terms of the GNU LGPLv3 license.
7
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 "usb-msc.h" // process_usb_op
14
15 struct drives_s Drives VAR16VISIBLE;
16
17 struct drive_s *
18 getDrive(u8 exttype, u8 extdriveoffset)
19 {
20     if (extdriveoffset >= ARRAY_SIZE(Drives.idmap[0]))
21         return NULL;
22     struct drive_s *drive_gf = GET_GLOBAL(Drives.idmap[exttype][extdriveoffset]);
23     if (!drive_gf)
24         return NULL;
25     return GLOBALFLAT2GLOBAL(drive_gf);
26 }
27
28
29 /****************************************************************
30  * Disk geometry translation
31  ****************************************************************/
32
33 static u8
34 get_translation(struct drive_s *drive_g)
35 {
36     u8 type = GET_GLOBAL(drive_g->type);
37     if (! CONFIG_COREBOOT && type == DTYPE_ATA) {
38         // Emulators pass in the translation info via nvram.
39         u8 ataid = GET_GLOBAL(drive_g->cntl_id);
40         u8 channel = ataid / 2;
41         u8 translation = inb_cmos(CMOS_BIOS_DISKTRANSFLAG + channel/2);
42         translation >>= 2 * (ataid % 4);
43         translation &= 0x03;
44         return translation;
45     }
46
47     // Otherwise use a heuristic to determine translation type.
48     u16 heads = GET_GLOBAL(drive_g->pchs.heads);
49     u16 cylinders = GET_GLOBAL(drive_g->pchs.cylinders);
50     u16 spt = GET_GLOBAL(drive_g->pchs.spt);
51     u64 sectors = GET_GLOBAL(drive_g->sectors);
52     u64 psectors = (u64)heads * cylinders * spt;
53     if (!heads || !cylinders || !spt || psectors > sectors)
54         // pchs doesn't look valid - use LBA.
55         return TRANSLATION_LBA;
56
57     if (cylinders <= 1024 && heads <= 16 && spt <= 63)
58         return TRANSLATION_NONE;
59     if (cylinders * heads <= 131072)
60         return TRANSLATION_LARGE;
61     return TRANSLATION_LBA;
62 }
63
64 void
65 setup_translation(struct drive_s *drive_g)
66 {
67     u8 translation = get_translation(drive_g);
68     SET_GLOBAL(drive_g->translation, translation);
69
70     u16 heads = GET_GLOBAL(drive_g->pchs.heads);
71     u16 cylinders = GET_GLOBAL(drive_g->pchs.cylinders);
72     u16 spt = GET_GLOBAL(drive_g->pchs.spt);
73     u64 sectors = GET_GLOBAL(drive_g->sectors);
74     const char *desc = NULL;
75
76     switch (translation) {
77     default:
78     case TRANSLATION_NONE:
79         desc = "none";
80         break;
81     case TRANSLATION_LBA:
82         desc = "lba";
83         spt = 63;
84         if (sectors > 63*255*1024) {
85             heads = 255;
86             cylinders = 1024;
87             break;
88         }
89         u32 sect = (u32)sectors / 63;
90         heads = sect / 1024;
91         if (heads>128)
92             heads = 255;
93         else if (heads>64)
94             heads = 128;
95         else if (heads>32)
96             heads = 64;
97         else if (heads>16)
98             heads = 32;
99         else
100             heads = 16;
101         cylinders = sect / heads;
102         break;
103     case TRANSLATION_RECHS:
104         desc = "r-echs";
105         // Take care not to overflow
106         if (heads==16) {
107             if (cylinders>61439)
108                 cylinders=61439;
109             heads=15;
110             cylinders = (u16)((u32)(cylinders)*16/15);
111         }
112         // then go through the large bitshift process
113     case TRANSLATION_LARGE:
114         if (translation == TRANSLATION_LARGE)
115             desc = "large";
116         while (cylinders > 1024) {
117             cylinders >>= 1;
118             heads <<= 1;
119
120             // If we max out the head count
121             if (heads > 127)
122                 break;
123         }
124         break;
125     }
126     // clip to 1024 cylinders in lchs
127     if (cylinders > 1024)
128         cylinders = 1024;
129     dprintf(1, "drive %p: PCHS=%u/%d/%d translation=%s LCHS=%d/%d/%d s=%d\n"
130             , drive_g
131             , drive_g->pchs.cylinders, drive_g->pchs.heads, drive_g->pchs.spt
132             , desc
133             , cylinders, heads, spt
134             , (u32)sectors);
135
136     SET_GLOBAL(drive_g->lchs.heads, heads);
137     SET_GLOBAL(drive_g->lchs.cylinders, cylinders);
138     SET_GLOBAL(drive_g->lchs.spt, spt);
139 }
140
141
142 /****************************************************************
143  * Drive mapping
144  ****************************************************************/
145
146 // Fill in Fixed Disk Parameter Table (located in ebda).
147 static void
148 fill_fdpt(struct drive_s *drive_g, int hdid)
149 {
150     if (hdid > 1)
151         return;
152
153     u16 nlc   = GET_GLOBAL(drive_g->lchs.cylinders);
154     u16 nlh   = GET_GLOBAL(drive_g->lchs.heads);
155     u16 nlspt = GET_GLOBAL(drive_g->lchs.spt);
156
157     u16 npc   = GET_GLOBAL(drive_g->pchs.cylinders);
158     u16 nph   = GET_GLOBAL(drive_g->pchs.heads);
159     u16 npspt = GET_GLOBAL(drive_g->pchs.spt);
160
161     struct fdpt_s *fdpt = &get_ebda_ptr()->fdpt[hdid];
162     fdpt->precompensation = 0xffff;
163     fdpt->drive_control_byte = 0xc0 | ((nph > 8) << 3);
164     fdpt->landing_zone = npc;
165     fdpt->cylinders = nlc;
166     fdpt->heads = nlh;
167     fdpt->sectors = nlspt;
168
169     if (nlc != npc || nlh != nph || nlspt != npspt) {
170         // Logical mapping present - use extended structure.
171
172         // complies with Phoenix style Translated Fixed Disk Parameter
173         // Table (FDPT)
174         fdpt->phys_cylinders = npc;
175         fdpt->phys_heads = nph;
176         fdpt->phys_sectors = npspt;
177         fdpt->a0h_signature = 0xa0;
178
179         // Checksum structure.
180         fdpt->checksum -= checksum(fdpt, sizeof(*fdpt));
181     }
182
183     if (hdid == 0)
184         SET_IVT(0x41, SEGOFF(get_ebda_seg(), offsetof(
185                                  struct extended_bios_data_area_s, fdpt[0])));
186     else
187         SET_IVT(0x46, SEGOFF(get_ebda_seg(), offsetof(
188                                  struct extended_bios_data_area_s, fdpt[1])));
189 }
190
191 // Map a drive (that was registered via add_bcv_hd)
192 void
193 map_hd_drive(struct drive_s *drive_g)
194 {
195     // fill hdidmap
196     u8 hdcount = GET_BDA(hdcount);
197     if (hdcount >= ARRAY_SIZE(Drives.idmap[0])) {
198         warn_noalloc();
199         return;
200     }
201     dprintf(3, "Mapping hd drive %p to %d\n", drive_g, hdcount);
202     Drives.idmap[EXTTYPE_HD][hdcount] = drive_g;
203     SET_BDA(hdcount, hdcount + 1);
204
205     // Fill "fdpt" structure.
206     fill_fdpt(drive_g, hdcount);
207 }
208
209 // Find spot to add a drive
210 static void
211 add_ordered_drive(struct drive_s **idmap, u8 *count, struct drive_s *drive_g)
212 {
213     if (*count >= ARRAY_SIZE(Drives.idmap[0])) {
214         warn_noalloc();
215         return;
216     }
217     struct drive_s **pos = &idmap[*count];
218     *count = *count + 1;
219     if (CONFIG_THREADS) {
220         // Add to idmap with assured drive order.
221         struct drive_s **end = pos;
222         for (;;) {
223             struct drive_s **prev = pos - 1;
224             if (prev < idmap)
225                 break;
226             struct drive_s *prevdrive = *prev;
227             if (prevdrive->type < drive_g->type
228                 || (prevdrive->type == drive_g->type
229                     && prevdrive->cntl_id < drive_g->cntl_id))
230                 break;
231             pos--;
232         }
233         if (pos != end)
234             memmove(pos+1, pos, (void*)end-(void*)pos);
235     }
236     *pos = drive_g;
237 }
238
239 // Map a cd
240 void
241 map_cd_drive(struct drive_s *drive_g)
242 {
243     dprintf(3, "Mapping cd drive %p\n", drive_g);
244     add_ordered_drive(Drives.idmap[EXTTYPE_CD], &Drives.cdcount, drive_g);
245 }
246
247 // Map a floppy
248 void
249 map_floppy_drive(struct drive_s *drive_g)
250 {
251     // fill idmap
252     dprintf(3, "Mapping floppy drive %p\n", drive_g);
253     add_ordered_drive(Drives.idmap[EXTTYPE_FLOPPY], &Drives.floppycount
254                       , drive_g);
255
256     // Update equipment word bits for floppy
257     if (Drives.floppycount == 1) {
258         // 1 drive, ready for boot
259         SETBITS_BDA(equipment_list_flags, 0x01);
260         SET_BDA(floppy_harddisk_info, 0x07);
261     } else if (Drives.floppycount >= 2) {
262         // 2 drives, ready for boot
263         SETBITS_BDA(equipment_list_flags, 0x41);
264         SET_BDA(floppy_harddisk_info, 0x77);
265     }
266 }
267
268
269 /****************************************************************
270  * 16bit calling interface
271  ****************************************************************/
272
273 // Execute a disk_op request.
274 int
275 process_op(struct disk_op_s *op)
276 {
277     ASSERT16();
278     u8 type = GET_GLOBAL(op->drive_g->type);
279     switch (type) {
280     case DTYPE_FLOPPY:
281         return process_floppy_op(op);
282     case DTYPE_ATA:
283         return process_ata_op(op);
284     case DTYPE_ATAPI:
285         return process_atapi_op(op);
286     case DTYPE_RAMDISK:
287         return process_ramdisk_op(op);
288     case DTYPE_CDEMU:
289         return process_cdemu_op(op);
290     case DTYPE_USB:
291         return process_usb_op(op);
292     default:
293         op->count = 0;
294         return DISK_RET_EPARAM;
295     }
296 }
297
298 // Execute a "disk_op_s" request - this runs on a stack in the ebda.
299 static int
300 __send_disk_op(struct disk_op_s *op_far, u16 op_seg)
301 {
302     struct disk_op_s dop;
303     memcpy_far(GET_SEG(SS), &dop
304                , op_seg, op_far
305                , sizeof(dop));
306
307     dprintf(DEBUG_HDL_13, "disk_op d=%p lba=%d buf=%p count=%d cmd=%d\n"
308             , dop.drive_g, (u32)dop.lba, dop.buf_fl
309             , dop.count, dop.command);
310
311     int status = process_op(&dop);
312
313     // Update count with total sectors transferred.
314     SET_FARVAR(op_seg, op_far->count, dop.count);
315
316     return status;
317 }
318
319 // Execute a "disk_op_s" request by jumping to a stack in the ebda.
320 int
321 send_disk_op(struct disk_op_s *op)
322 {
323     ASSERT16();
324     if (! CONFIG_DRIVES)
325         return -1;
326
327     return stack_hop((u32)op, GET_SEG(SS), __send_disk_op);
328 }
329
330
331 /****************************************************************
332  * Setup
333  ****************************************************************/
334
335 void
336 drive_setup(void)
337 {
338     memset(&Drives, 0, sizeof(Drives));
339 }