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