blob: dd7c46aec3c19913d6cf257d62b5a2f706580cd3 [file] [log] [blame]
Kevin O'Connorf076a3e2008-02-25 22:25:15 -05001// Definitions for X86 bios disks.
2//
3// Copyright (C) 2008 Kevin O'Connor <kevin@koconnor.net>
4//
Kevin O'Connorb1b7c2a2009-01-15 20:52:58 -05005// This file may be distributed under the terms of the GNU LGPLv3 license.
Kevin O'Connor786502d2008-02-27 10:41:41 -05006#ifndef __DISK_H
7#define __DISK_H
Kevin O'Connorf076a3e2008-02-25 22:25:15 -05008
Kevin O'Connor9521e262008-07-04 13:04:29 -04009#include "types.h" // u8
Kevin O'Connor609da232008-12-28 23:18:57 -050010#include "config.h" // CONFIG_*
Kevin O'Connor9f985422009-09-09 11:34:39 -040011#include "farptr.h" // struct segoff_s
Kevin O'Connorf076a3e2008-02-25 22:25:15 -050012
Kevin O'Connore43df9e2008-03-01 22:16:32 -050013#define DISK_RET_SUCCESS 0x00
14#define DISK_RET_EPARAM 0x01
Kevin O'Connorb74102d2008-03-03 21:57:30 -050015#define DISK_RET_EADDRNOTFOUND 0x02
16#define DISK_RET_EWRITEPROTECT 0x03
Kevin O'Connore43df9e2008-03-01 22:16:32 -050017#define DISK_RET_ECHANGED 0x06
18#define DISK_RET_EBOUNDARY 0x09
19#define DISK_RET_EBADTRACK 0x0c
20#define DISK_RET_ECONTROLLER 0x20
21#define DISK_RET_ETIMEOUT 0x80
Kevin O'Connorb74102d2008-03-03 21:57:30 -050022#define DISK_RET_ENOTLOCKED 0xb0
23#define DISK_RET_ELOCKED 0xb1
Kevin O'Connore43df9e2008-03-01 22:16:32 -050024#define DISK_RET_ENOTREMOVABLE 0xb2
Kevin O'Connorb74102d2008-03-03 21:57:30 -050025#define DISK_RET_ETOOMANYLOCKS 0xb4
Kevin O'Connore43df9e2008-03-01 22:16:32 -050026#define DISK_RET_EMEDIA 0xC0
27#define DISK_RET_ENOTREADY 0xAA
28
Kevin O'Connor609da232008-12-28 23:18:57 -050029
30/****************************************************************
31 * Interface structs
32 ****************************************************************/
33
Kevin O'Connore43df9e2008-03-01 22:16:32 -050034// Bios disk structures.
35struct int13ext_s {
36 u8 size;
37 u8 reserved;
38 u16 count;
Kevin O'Connor9f985422009-09-09 11:34:39 -040039 struct segoff_s data;
Kevin O'Connor1bb3b5c2008-05-14 00:43:13 -040040 u64 lba;
Kevin O'Connored128492008-03-11 11:14:59 -040041} PACKED;
Kevin O'Connore43df9e2008-03-01 22:16:32 -050042
43#define GET_INT13EXT(regs,var) \
44 GET_FARVAR((regs)->ds, ((struct int13ext_s*)((regs)->si+0))->var)
45#define SET_INT13EXT(regs,var,val) \
46 SET_FARVAR((regs)->ds, ((struct int13ext_s*)((regs)->si+0))->var, (val))
47
48// Disk Physical Table definition
49struct int13dpt_s {
50 u16 size;
51 u16 infos;
52 u32 cylinders;
53 u32 heads;
54 u32 spt;
Kevin O'Connor1bb3b5c2008-05-14 00:43:13 -040055 u64 sector_count;
Kevin O'Connore43df9e2008-03-01 22:16:32 -050056 u16 blksize;
57 u16 dpte_offset;
58 u16 dpte_segment;
59 u16 key;
60 u8 dpi_length;
61 u8 reserved1;
62 u16 reserved2;
63 u8 host_bus[4];
64 u8 iface_type[8];
Kevin O'Connor53236cc2008-08-31 11:16:33 -040065 u64 iface_path;
Gleb Natapovf5154e22011-01-10 10:50:27 +020066 union {
67 struct {
68 u64 device_path;
69 u8 reserved3;
70 u8 checksum;
71 } phoenix;
72 struct {
73 u64 device_path[2];
74 u8 reserved3;
75 u8 checksum;
76 } t13;
77 };
Kevin O'Connored128492008-03-11 11:14:59 -040078} PACKED;
Kevin O'Connore43df9e2008-03-01 22:16:32 -050079
80#define GET_INT13DPT(regs,var) \
81 GET_FARVAR((regs)->ds, ((struct int13dpt_s*)((regs)->si+0))->var)
82#define SET_INT13DPT(regs,var,val) \
83 SET_FARVAR((regs)->ds, ((struct int13dpt_s*)((regs)->si+0))->var, (val))
84
Kevin O'Connor44c631d2008-03-02 11:24:36 -050085// Floppy "Disk Base Table"
86struct floppy_dbt_s {
87 u8 specify1;
88 u8 specify2;
89 u8 shutoff_ticks;
90 u8 bps_code;
91 u8 sectors;
92 u8 interblock_len;
93 u8 data_len;
94 u8 gap_len;
95 u8 fill_byte;
96 u8 settle_time;
97 u8 startup_time;
Kevin O'Connored128492008-03-11 11:14:59 -040098} PACKED;
Kevin O'Connor44c631d2008-03-02 11:24:36 -050099
100struct floppy_ext_dbt_s {
101 struct floppy_dbt_s dbt;
102 // Extra fields
103 u8 max_track;
104 u8 data_rate;
105 u8 drive_type;
Kevin O'Connored128492008-03-11 11:14:59 -0400106} PACKED;
Kevin O'Connorf076a3e2008-02-25 22:25:15 -0500107
Kevin O'Connor31d8c8a2008-03-04 19:56:41 -0500108// Helper function for setting up a return code.
Kevin O'Connor9521e262008-07-04 13:04:29 -0400109struct bregs;
Kevin O'Connor05600342009-01-02 13:10:58 -0500110void __disk_ret(struct bregs *regs, u32 linecode, const char *fname);
Kevin O'Connor567e4e32008-04-05 11:37:51 -0400111#define disk_ret(regs, code) \
Kevin O'Connor05600342009-01-02 13:10:58 -0500112 __disk_ret((regs), (code) | (__LINE__ << 8), __func__)
Kevin O'Connordfefeb52009-12-13 13:04:17 -0500113void __disk_ret_unimplemented(struct bregs *regs, u32 linecode
114 , const char *fname);
115#define disk_ret_unimplemented(regs, code) \
116 __disk_ret_unimplemented((regs), (code) | (__LINE__ << 8), __func__)
Kevin O'Connor31d8c8a2008-03-04 19:56:41 -0500117
Kevin O'Connor609da232008-12-28 23:18:57 -0500118
119/****************************************************************
Kevin O'Connor95827c42009-02-07 00:04:57 -0500120 * Master boot record
121 ****************************************************************/
122
123struct packed_chs_s {
124 u8 heads;
125 u8 sptcyl;
126 u8 cyllow;
127};
128
129struct partition_s {
130 u8 status;
131 struct packed_chs_s first;
132 u8 type;
133 struct packed_chs_s last;
134 u32 lba;
135 u32 count;
136} PACKED;
137
138struct mbr_s {
139 u8 code[440];
140 // 0x01b8
141 u32 diskseg;
142 // 0x01bc
143 u16 null;
144 // 0x01be
145 struct partition_s partitions[4];
146 // 0x01fe
147 u16 signature;
148} PACKED;
149
150#define MBR_SIGNATURE 0xaa55
151
152
153/****************************************************************
Kevin O'Connor4524bf72008-12-31 00:31:03 -0500154 * Disk command request
155 ****************************************************************/
156
157struct disk_op_s {
158 u64 lba;
Kevin O'Connor35ae7262009-01-19 15:44:44 -0500159 void *buf_fl;
Kevin O'Connor77d227b2009-10-22 21:48:39 -0400160 struct drive_s *drive_g;
Kevin O'Connor4524bf72008-12-31 00:31:03 -0500161 u16 count;
Kevin O'Connor4524bf72008-12-31 00:31:03 -0500162 u8 command;
163};
164
Kevin O'Connor42337662009-08-10 00:06:37 -0400165#define CMD_RESET 0x00
166#define CMD_READ 0x02
167#define CMD_WRITE 0x03
168#define CMD_VERIFY 0x04
Kevin O'Connoraf5aabb2009-08-16 18:48:38 -0400169#define CMD_FORMAT 0x05
Kevin O'Connor42337662009-08-10 00:06:37 -0400170#define CMD_SEEK 0x07
171#define CMD_ISREADY 0x10
Kevin O'Connor4524bf72008-12-31 00:31:03 -0500172
173
174/****************************************************************
Kevin O'Connor609da232008-12-28 23:18:57 -0500175 * Global storage
176 ****************************************************************/
177
178struct chs_s {
179 u16 heads; // # heads
180 u16 cylinders; // # cylinders
181 u16 spt; // # sectors / track
182};
183
Kevin O'Connorc892b132009-08-11 21:59:37 -0400184struct drive_s {
Kevin O'Connora4bd9192010-02-17 22:55:36 -0500185 u8 type; // Driver type (DTYPE_*)
186 u8 floppy_type; // Type of floppy (only for floppy drives).
187 struct chs_s lchs; // Logical CHS
188 u64 sectors; // Total sectors count
Kevin O'Connor575ffc82010-02-21 23:20:10 -0500189 u32 cntl_id; // Unique id for a given driver type.
190 u8 removable; // Is media removable (currently unused)
Kevin O'Connora4bd9192010-02-17 22:55:36 -0500191
192 // Info for EDD calls
Kevin O'Connor575ffc82010-02-21 23:20:10 -0500193 u8 translation; // type of translation
Kevin O'Connora4bd9192010-02-17 22:55:36 -0500194 u16 blksize; // block size
195 struct chs_s pchs; // Physical CHS
Kevin O'Connor609da232008-12-28 23:18:57 -0500196};
197
Kevin O'Connor36c93a52009-09-12 19:35:04 -0400198#define DISK_SECTOR_SIZE 512
199#define CDROM_SECTOR_SIZE 2048
200
Paolo Bonzini0e7fb5f2011-11-16 13:02:55 +0100201#define DTYPE_NONE 0x00
202#define DTYPE_FLOPPY 0x01
203#define DTYPE_ATA 0x02
204#define DTYPE_ATAPI 0x03
205#define DTYPE_RAMDISK 0x04
206#define DTYPE_CDEMU 0x05
207#define DTYPE_USB 0x06
208#define DTYPE_VIRTIO_BLK 0x07
209#define DTYPE_AHCI 0x08
Kevin O'Connor42337662009-08-10 00:06:37 -0400210
Kevin O'Connor575ffc82010-02-21 23:20:10 -0500211#define MAXDESCSIZE 80
212
Kevin O'Connor42337662009-08-10 00:06:37 -0400213#define TRANSLATION_NONE 0
214#define TRANSLATION_LBA 1
215#define TRANSLATION_LARGE 2
216#define TRANSLATION_RECHS 3
217
Kevin O'Connor0a0e42e2009-08-16 12:09:44 -0400218#define EXTTYPE_FLOPPY 0
219#define EXTTYPE_HD 1
220#define EXTTYPE_CD 2
221
Kevin O'Connor51cfbe72009-08-18 22:38:49 -0400222#define EXTSTART_HD 0x80
223#define EXTSTART_CD 0xE0
224
Kevin O'Connor609da232008-12-28 23:18:57 -0500225
226/****************************************************************
227 * Function defs
228 ****************************************************************/
229
Kevin O'Connorc892b132009-08-11 21:59:37 -0400230// block.c
Kevin O'Connora0842f82010-12-29 11:05:46 -0500231extern u8 FloppyCount, CDCount;
Gerd Hoffmannd7a7cf32011-08-04 19:36:27 +0200232extern u8 *bounce_buf_fl;
Kevin O'Connor77d227b2009-10-22 21:48:39 -0400233struct drive_s *getDrive(u8 exttype, u8 extdriveoffset);
Gleb Natapov4c90a202010-12-07 13:50:54 +0200234int getDriveId(u8 exttype, struct drive_s *drive_g);
Kevin O'Connor77d227b2009-10-22 21:48:39 -0400235void map_floppy_drive(struct drive_s *drive_g);
236void map_hd_drive(struct drive_s *drive_g);
237void map_cd_drive(struct drive_s *drive_g);
Kevin O'Connor36c93a52009-09-12 19:35:04 -0400238int process_op(struct disk_op_s *op);
Kevin O'Connoraf5aabb2009-08-16 18:48:38 -0400239int send_disk_op(struct disk_op_s *op);
Gerd Hoffmannd7a7cf32011-08-04 19:36:27 +0200240int bounce_buf_init(void);
Kevin O'Connor609da232008-12-28 23:18:57 -0500241
Kevin O'Connorf076a3e2008-02-25 22:25:15 -0500242// floppy.c
Kevin O'Connor941d3e42008-03-04 19:45:04 -0500243extern struct floppy_ext_dbt_s diskette_param_table2;
Kevin O'Connor1ca05b02010-01-03 17:43:37 -0500244void floppy_setup(void);
Kevin O'Connorca2bc1c2010-12-29 21:41:19 -0500245struct drive_s *init_floppy(int floppyid, int ftype);
Kevin O'Connora3855ad2009-08-16 21:59:40 -0400246int find_floppy_type(u32 size);
Kevin O'Connoraf5aabb2009-08-16 18:48:38 -0400247int process_floppy_op(struct disk_op_s *op);
Kevin O'Connor1ca05b02010-01-03 17:43:37 -0500248void floppy_tick(void);
Kevin O'Connor786502d2008-02-27 10:41:41 -0500249
Kevin O'Connor31d8c8a2008-03-04 19:56:41 -0500250// cdrom.c
Kevin O'Connor8f469b92010-02-28 01:28:11 -0500251extern struct drive_s *cdemu_drive_gf;
Kevin O'Connor36c93a52009-09-12 19:35:04 -0400252int process_cdemu_op(struct disk_op_s *op);
Kevin O'Connor1ca05b02010-01-03 17:43:37 -0500253void cdemu_setup(void);
Kevin O'Connor31d8c8a2008-03-04 19:56:41 -0500254void cdemu_134b(struct bregs *regs);
Gleb Natapov4c90a202010-12-07 13:50:54 +0200255int cdrom_boot(struct drive_s *drive_g);
Kevin O'Connor31d8c8a2008-03-04 19:56:41 -0500256
Kevin O'Connora3855ad2009-08-16 21:59:40 -0400257// ramdisk.c
Kevin O'Connor1ca05b02010-01-03 17:43:37 -0500258void ramdisk_setup(void);
Kevin O'Connora3855ad2009-08-16 21:59:40 -0400259int process_ramdisk_op(struct disk_op_s *op);
260
Kevin O'Connor786502d2008-02-27 10:41:41 -0500261#endif // disk.h