1 | /*
|
---|
2 | * Copyright (C) 2006-2016 Oracle Corporation
|
---|
3 | *
|
---|
4 | * This file is part of VirtualBox Open Source Edition (OSE), as
|
---|
5 | * available from http://www.virtualbox.org. This file is free software;
|
---|
6 | * you can redistribute it and/or modify it under the terms of the GNU
|
---|
7 | * General Public License (GPL) as published by the Free Software
|
---|
8 | * Foundation, in version 2 as it comes in the "COPYING" file of the
|
---|
9 | * VirtualBox OSE distribution. VirtualBox OSE is distributed in the
|
---|
10 | * hope that it will be useful, but WITHOUT ANY WARRANTY of any kind.
|
---|
11 | * --------------------------------------------------------------------
|
---|
12 | *
|
---|
13 | * This code is based on:
|
---|
14 | *
|
---|
15 | * ROM BIOS for use with Bochs/Plex86/QEMU emulation environment
|
---|
16 | *
|
---|
17 | * Copyright (C) 2002 MandrakeSoft S.A.
|
---|
18 | *
|
---|
19 | * MandrakeSoft S.A.
|
---|
20 | * 43, rue d'Aboukir
|
---|
21 | * 75002 Paris - France
|
---|
22 | * http://www.linux-mandrake.com/
|
---|
23 | * http://www.mandrakesoft.com/
|
---|
24 | *
|
---|
25 | * This library is free software; you can redistribute it and/or
|
---|
26 | * modify it under the terms of the GNU Lesser General Public
|
---|
27 | * License as published by the Free Software Foundation; either
|
---|
28 | * version 2 of the License, or (at your option) any later version.
|
---|
29 | *
|
---|
30 | * This library is distributed in the hope that it will be useful,
|
---|
31 | * but WITHOUT ANY WARRANTY; without even the implied warranty of
|
---|
32 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
|
---|
33 | * Lesser General Public License for more details.
|
---|
34 | *
|
---|
35 | * You should have received a copy of the GNU Lesser General Public
|
---|
36 | * License along with this library; if not, write to the Free Software
|
---|
37 | * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
|
---|
38 | *
|
---|
39 | */
|
---|
40 |
|
---|
41 |
|
---|
42 | #include <stdint.h>
|
---|
43 |
|
---|
44 | /* Must be defined here (EBDA structures depend on these). */
|
---|
45 | #define BX_MAX_ATA_INTERFACES 4
|
---|
46 | #define BX_MAX_ATA_DEVICES (BX_MAX_ATA_INTERFACES*2)
|
---|
47 |
|
---|
48 | #define BX_USE_ATADRV 1
|
---|
49 | #define BX_ELTORITO_BOOT 1
|
---|
50 |
|
---|
51 | #ifdef VBOX_WITH_SCSI
|
---|
52 | /* Enough for now */
|
---|
53 | #define BX_MAX_SCSI_DEVICES 4
|
---|
54 | /* A SCSI device starts always at BX_MAX_ATA_DEVICES. */
|
---|
55 | #define VBOX_IS_SCSI_DEVICE(device_id) (device_id >= BX_MAX_ATA_DEVICES)
|
---|
56 | #define VBOX_GET_SCSI_DEVICE(device_id) (device_id - BX_MAX_ATA_DEVICES)
|
---|
57 | #else
|
---|
58 | #define BX_MAX_SCSI_DEVICES 0
|
---|
59 | #endif
|
---|
60 |
|
---|
61 | #ifdef VBOX_WITH_AHCI
|
---|
62 | /* Four should be enough for now */
|
---|
63 | #define BX_MAX_AHCI_DEVICES 4
|
---|
64 |
|
---|
65 | /* An AHCI device starts always at BX_MAX_ATA_DEVICES + BX_MAX_SCSI_DEVICES. */
|
---|
66 | #define VBOX_IS_AHCI_DEVICE(device_id) (device_id >= (BX_MAX_ATA_DEVICES + BX_MAX_SCSI_DEVICES))
|
---|
67 | #define VBOX_GET_AHCI_DEVICE(device_id) (device_id - (BX_MAX_ATA_DEVICES + BX_MAX_SCSI_DEVICES))
|
---|
68 | #else
|
---|
69 | #define BX_MAX_AHCI_DEVICES 0
|
---|
70 | #endif
|
---|
71 |
|
---|
72 | #define BX_MAX_STORAGE_DEVICES (BX_MAX_ATA_DEVICES + BX_MAX_SCSI_DEVICES + BX_MAX_AHCI_DEVICES)
|
---|
73 |
|
---|
74 | /* Generic storage device types. These depend on the controller type and
|
---|
75 | * determine which device access routines should be called.
|
---|
76 | */
|
---|
77 | enum dsk_type_enm {
|
---|
78 | DSK_TYPE_NONE, /* Unknown device. */
|
---|
79 | DSK_TYPE_UNKNOWN, /* Unknown ATA device. */
|
---|
80 | DSK_TYPE_ATA, /* ATA disk. */
|
---|
81 | DSK_TYPE_ATAPI, /* ATAPI device. */
|
---|
82 | DSK_TYPE_SCSI, /* SCSI disk. */
|
---|
83 | DSK_TYPE_AHCI, /* SATA disk via AHCI. */
|
---|
84 | DSKTYP_CNT /* Number of disk types. */
|
---|
85 | };
|
---|
86 |
|
---|
87 | /* Disk device types. */
|
---|
88 | //@todo: Do we really need these?
|
---|
89 | #define DSK_DEVICE_NONE 0x00 /* No device attached. */
|
---|
90 | #define DSK_DEVICE_HD 0xFF /* Device is a hard disk. */
|
---|
91 | #define DSK_DEVICE_CDROM 0x05 /* Device is a CD-ROM. */
|
---|
92 |
|
---|
93 | /* Geometry translation modes. */
|
---|
94 | enum geo_xlat_enm {
|
---|
95 | GEO_TRANSLATION_NONE, /* No geometry translation. */
|
---|
96 | GEO_TRANSLATION_LBA, /* LBA translation. */
|
---|
97 | GEO_TRANSLATION_LARGE, /* Large CHS translation. */
|
---|
98 | GEO_TRANSLATION_RECHS
|
---|
99 | };
|
---|
100 |
|
---|
101 | #if 1 //BX_USE_ATADRV
|
---|
102 |
|
---|
103 | /* Note: The DPTE and FDPT structures are industry standards and
|
---|
104 | * may not be modified. The other disk-related structures are
|
---|
105 | * internal to the BIOS.
|
---|
106 | */
|
---|
107 |
|
---|
108 | /* Translated DPT (Device Parameter Table). */
|
---|
109 | typedef struct {
|
---|
110 | uint16_t iobase1;
|
---|
111 | uint16_t iobase2;
|
---|
112 | uint8_t prefix;
|
---|
113 | uint8_t unused;
|
---|
114 | uint8_t irq;
|
---|
115 | uint8_t blkcount;
|
---|
116 | uint8_t dma;
|
---|
117 | uint8_t pio;
|
---|
118 | uint16_t options;
|
---|
119 | uint16_t reserved;
|
---|
120 | uint8_t revision;
|
---|
121 | uint8_t checksum;
|
---|
122 | } dpte_t;
|
---|
123 |
|
---|
124 | ct_assert(sizeof(dpte_t) == 16); /* Ensure correct size. */
|
---|
125 |
|
---|
126 | #pragma pack(0)
|
---|
127 |
|
---|
128 | /* FDPT - Fixed Disk Parameter Table. PC/AT compatible; note
|
---|
129 | * that this structure is slightly misaligned.
|
---|
130 | */
|
---|
131 | typedef struct {
|
---|
132 | uint16_t lcyl;
|
---|
133 | uint8_t lhead;
|
---|
134 | uint8_t sig;
|
---|
135 | uint8_t spt;
|
---|
136 | uint8_t resvd1[4];
|
---|
137 | uint16_t cyl;
|
---|
138 | uint8_t head;
|
---|
139 | uint8_t resvd2[2];
|
---|
140 | uint8_t lspt;
|
---|
141 | uint8_t csum;
|
---|
142 | } fdpt_t;
|
---|
143 |
|
---|
144 | #pragma pack()
|
---|
145 |
|
---|
146 | ct_assert(sizeof(fdpt_t) == 16); /* Ensure correct size. */
|
---|
147 |
|
---|
148 |
|
---|
149 | /* C/H/S geometry information. */
|
---|
150 | typedef struct {
|
---|
151 | uint16_t heads; /* Number of heads. */
|
---|
152 | uint16_t cylinders; /* Number of cylinders. */
|
---|
153 | uint16_t spt; /* Number of sectors per track. */
|
---|
154 | } chs_t;
|
---|
155 |
|
---|
156 | /* IDE/ATA specific device information. */
|
---|
157 | typedef struct {
|
---|
158 | uint8_t iface; /* ISA or PCI. */
|
---|
159 | uint8_t irq; /* IRQ (on the PIC). */
|
---|
160 | uint16_t iobase1; /* I/O base 1. */
|
---|
161 | uint16_t iobase2; /* I/O base 2. */
|
---|
162 | } ata_chan_t;
|
---|
163 |
|
---|
164 | #ifdef VBOX_WITH_SCSI
|
---|
165 |
|
---|
166 | /* SCSI specific device information. */
|
---|
167 | typedef struct {
|
---|
168 | uint16_t io_base; /* Port base for HBA communication. */
|
---|
169 | uint8_t target_id; /* Target ID. */
|
---|
170 | } scsi_dev_t;
|
---|
171 |
|
---|
172 | #endif
|
---|
173 |
|
---|
174 | #ifdef VBOX_WITH_AHCI
|
---|
175 |
|
---|
176 | /* AHCI specific device information. */
|
---|
177 | typedef struct {
|
---|
178 | uint8_t port; /* SATA port. */
|
---|
179 | } ahci_dev_t;
|
---|
180 |
|
---|
181 | #endif
|
---|
182 |
|
---|
183 | /* Generic disk information. */
|
---|
184 | typedef struct {
|
---|
185 | uint8_t type; /* Device type (ATA/ATAPI/SCSI/none/unknown). */
|
---|
186 | uint8_t device; /* Detected type of attached device (HD/CD/none). */
|
---|
187 | uint8_t removable; /* Removable device flag. */
|
---|
188 | uint8_t lock; /* Lock count for removable devices. */
|
---|
189 | //@todo: ATA specific - move?
|
---|
190 | uint8_t mode; /* Transfer mode: PIO 16/32 bits - IRQ - ISADMA - PCIDMA. */
|
---|
191 | uint8_t translation; /* Type of geometry translation. */
|
---|
192 | uint16_t blksize; /* Disk block size. */
|
---|
193 | chs_t lchs; /* Logical CHS geometry. */
|
---|
194 | chs_t pchs; /* Physical CHS geometry. */
|
---|
195 | uint64_t sectors; /* Total sector count. */
|
---|
196 | } disk_dev_t;
|
---|
197 |
|
---|
198 | /* A structure for passing disk request information around. This structure
|
---|
199 | * is designed for saving stack space. As BIOS requests cannot be overlapped,
|
---|
200 | * one such structure is sufficient.
|
---|
201 | */
|
---|
202 | typedef struct {
|
---|
203 | uint64_t lba; /* Starting LBA. */
|
---|
204 | void __far *buffer; /* Read/write data buffer pointer. */
|
---|
205 | uint8_t dev_id; /* Device ID; index into devices array. */
|
---|
206 | uint16_t nsect; /* Number of sectors to be transferred. */
|
---|
207 | uint16_t sect_sz; /* Size of a sector in bytes. */
|
---|
208 | uint16_t cylinder; /* Starting cylinder (CHS only). */
|
---|
209 | uint16_t head; /* Starting head (CHS only). */
|
---|
210 | uint16_t sector; /* Starting sector (CHS only). */
|
---|
211 | uint16_t trsfsectors; /* Actual sectors transferred. */
|
---|
212 | uint32_t trsfbytes; /* Actual bytes transferred. */
|
---|
213 | uint16_t skip_b; /* Bytes to skip before transfer. */
|
---|
214 | uint16_t skip_a; /* Bytes to skip after transfer. */
|
---|
215 | } disk_req_t;
|
---|
216 |
|
---|
217 | extern uint16_t ahci_cmd_packet(uint16_t device_id, uint8_t cmdlen, char __far *cmdbuf,
|
---|
218 | uint16_t header, uint32_t length, uint8_t inout, char __far *buffer);
|
---|
219 | extern uint16_t scsi_cmd_packet(uint16_t device, uint8_t cmdlen, char __far *cmdbuf,
|
---|
220 | uint16_t header, uint32_t length, uint8_t inout, char __far *buffer);
|
---|
221 | extern uint16_t ata_cmd_packet(uint16_t device, uint8_t cmdlen, char __far *cmdbuf,
|
---|
222 | uint16_t header, uint32_t length, uint8_t inout, char __far *buffer);
|
---|
223 |
|
---|
224 | extern uint16_t ata_soft_reset(uint16_t device);
|
---|
225 |
|
---|
226 | /* All BIOS disk information. Disk-related code in the BIOS should not need
|
---|
227 | * anything outside of this structure.
|
---|
228 | */
|
---|
229 | typedef struct {
|
---|
230 | disk_req_t drqp; /* Disk request packet. */
|
---|
231 |
|
---|
232 | /* Bus-independent disk device information. */
|
---|
233 | disk_dev_t devices[BX_MAX_STORAGE_DEVICES];
|
---|
234 |
|
---|
235 | uint8_t hdcount; /* Total number of BIOS disks. */
|
---|
236 | /* Map between (BIOS disk ID - 0x80) and ATA/SCSI/AHCI disks. */
|
---|
237 | uint8_t hdidmap[BX_MAX_STORAGE_DEVICES];
|
---|
238 |
|
---|
239 | uint8_t cdcount; /* Number of CD-ROMs. */
|
---|
240 | /* Map between (BIOS CD-ROM ID - 0xE0) and ATA/SCSI/AHCI devices. */
|
---|
241 | uint8_t cdidmap[BX_MAX_STORAGE_DEVICES];
|
---|
242 |
|
---|
243 | /* ATA bus-specific device information. */
|
---|
244 | ata_chan_t channels[BX_MAX_ATA_INTERFACES];
|
---|
245 |
|
---|
246 | #ifdef VBOX_WITH_SCSI
|
---|
247 | /* SCSI bus-specific device information. */
|
---|
248 | scsi_dev_t scsidev[BX_MAX_SCSI_DEVICES];
|
---|
249 | uint8_t scsi_devcount; /* Number of SCSI devices. */
|
---|
250 | #endif
|
---|
251 |
|
---|
252 | #ifdef VBOX_WITH_AHCI
|
---|
253 | /* SATA (AHCI) bus-specific device information. */
|
---|
254 | ahci_dev_t ahcidev[BX_MAX_AHCI_DEVICES];
|
---|
255 | uint8_t ahci_devcnt; /* Number of SATA devices. */
|
---|
256 | uint16_t ahci_seg; /* Segment of AHCI data block. */
|
---|
257 | #endif
|
---|
258 |
|
---|
259 | dpte_t dpte; /* Buffer for building a DPTE. */
|
---|
260 | } bio_dsk_t;
|
---|
261 |
|
---|
262 | #if BX_ELTORITO_BOOT
|
---|
263 | /* El Torito device emulation state. */
|
---|
264 | typedef struct {
|
---|
265 | uint8_t active;
|
---|
266 | uint8_t media;
|
---|
267 | uint8_t emulated_drive;
|
---|
268 | uint8_t controller_index;
|
---|
269 | uint16_t device_spec;
|
---|
270 | uint16_t buffer_segment;
|
---|
271 | uint32_t ilba;
|
---|
272 | uint16_t load_segment;
|
---|
273 | uint16_t sector_count;
|
---|
274 | chs_t vdevice; /* Virtual device geometry. */
|
---|
275 | } cdemu_t;
|
---|
276 | #endif
|
---|
277 |
|
---|
278 | // for access to EBDA area
|
---|
279 | // The EBDA structure should conform to
|
---|
280 | // http://www.frontiernet.net/~fys/rombios.htm document
|
---|
281 | // I made the ata and cdemu structs begin at 0x121 in the EBDA seg
|
---|
282 | /* MS-DOS KEYB.COM may overwrite the word at offset 0x117 in the EBDA
|
---|
283 | * which contains the keyboard ID for PS/2 BIOSes.
|
---|
284 | */
|
---|
285 | typedef struct {
|
---|
286 | uint8_t filler1[0x3D];
|
---|
287 |
|
---|
288 | fdpt_t fdpt0; /* FDPTs for the first two ATA disks. */
|
---|
289 | fdpt_t fdpt1;
|
---|
290 |
|
---|
291 | uint8_t filler2[0xC4];
|
---|
292 |
|
---|
293 | bio_dsk_t bdisk; /* Disk driver data (ATA/SCSI/AHCI). */
|
---|
294 |
|
---|
295 | #if BX_ELTORITO_BOOT
|
---|
296 | cdemu_t cdemu; /* El Torito floppy/HD emulation data. */
|
---|
297 | #endif
|
---|
298 |
|
---|
299 | unsigned char uForceBootDrive;
|
---|
300 | unsigned char uForceBootDevice;
|
---|
301 | } ebda_data_t;
|
---|
302 |
|
---|
303 | ct_assert(sizeof(ebda_data_t) < 0x380); /* Must be under 1K in size. */
|
---|
304 |
|
---|
305 | // the last 16 bytes of the EBDA segment are used for the MPS floating
|
---|
306 | // pointer structure (though only if an I/O APIC is present)
|
---|
307 |
|
---|
308 | #define EbdaData ((ebda_data_t *) 0)
|
---|
309 |
|
---|
310 | /* Note: Using fastcall reduces stack usage a little. */
|
---|
311 | int __fastcall ata_read_sectors(bio_dsk_t __far *bios_dsk);
|
---|
312 | int __fastcall ata_write_sectors(bio_dsk_t __far *bios_dsk);
|
---|
313 |
|
---|
314 | int __fastcall scsi_read_sectors(bio_dsk_t __far *bios_dsk);
|
---|
315 | int __fastcall scsi_write_sectors(bio_dsk_t __far *bios_dsk);
|
---|
316 |
|
---|
317 | int __fastcall ahci_read_sectors(bio_dsk_t __far *bios_dsk);
|
---|
318 | int __fastcall ahci_write_sectors(bio_dsk_t __far *bios_dsk);
|
---|
319 |
|
---|
320 | extern void set_geom_lba(chs_t __far *lgeo, uint64_t nsectors);
|
---|
321 |
|
---|
322 | // @todo: put this elsewhere (and change/eliminate?)
|
---|
323 | #define SET_DISK_RET_STATUS(status) write_byte(0x0040, 0x0074, status)
|
---|
324 |
|
---|
325 | #endif
|
---|