blob: 4c3f8cd9e9353c0446742076848062527e9d0686 [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;
66 u64 device_path;
Kevin O'Connore43df9e2008-03-01 22:16:32 -050067 u8 reserved3;
68 u8 checksum;
Kevin O'Connored128492008-03-11 11:14:59 -040069} PACKED;
Kevin O'Connore43df9e2008-03-01 22:16:32 -050070
71#define GET_INT13DPT(regs,var) \
72 GET_FARVAR((regs)->ds, ((struct int13dpt_s*)((regs)->si+0))->var)
73#define SET_INT13DPT(regs,var,val) \
74 SET_FARVAR((regs)->ds, ((struct int13dpt_s*)((regs)->si+0))->var, (val))
75
Kevin O'Connor44c631d2008-03-02 11:24:36 -050076// Floppy "Disk Base Table"
77struct floppy_dbt_s {
78 u8 specify1;
79 u8 specify2;
80 u8 shutoff_ticks;
81 u8 bps_code;
82 u8 sectors;
83 u8 interblock_len;
84 u8 data_len;
85 u8 gap_len;
86 u8 fill_byte;
87 u8 settle_time;
88 u8 startup_time;
Kevin O'Connored128492008-03-11 11:14:59 -040089} PACKED;
Kevin O'Connor44c631d2008-03-02 11:24:36 -050090
91struct floppy_ext_dbt_s {
92 struct floppy_dbt_s dbt;
93 // Extra fields
94 u8 max_track;
95 u8 data_rate;
96 u8 drive_type;
Kevin O'Connored128492008-03-11 11:14:59 -040097} PACKED;
Kevin O'Connorf076a3e2008-02-25 22:25:15 -050098
Kevin O'Connor31d8c8a2008-03-04 19:56:41 -050099// Helper function for setting up a return code.
Kevin O'Connor9521e262008-07-04 13:04:29 -0400100struct bregs;
Kevin O'Connor05600342009-01-02 13:10:58 -0500101void __disk_ret(struct bregs *regs, u32 linecode, const char *fname);
Kevin O'Connor567e4e32008-04-05 11:37:51 -0400102#define disk_ret(regs, code) \
Kevin O'Connor05600342009-01-02 13:10:58 -0500103 __disk_ret((regs), (code) | (__LINE__ << 8), __func__)
Kevin O'Connor31d8c8a2008-03-04 19:56:41 -0500104
Kevin O'Connor609da232008-12-28 23:18:57 -0500105
106/****************************************************************
Kevin O'Connor95827c42009-02-07 00:04:57 -0500107 * Master boot record
108 ****************************************************************/
109
110struct packed_chs_s {
111 u8 heads;
112 u8 sptcyl;
113 u8 cyllow;
114};
115
116struct partition_s {
117 u8 status;
118 struct packed_chs_s first;
119 u8 type;
120 struct packed_chs_s last;
121 u32 lba;
122 u32 count;
123} PACKED;
124
125struct mbr_s {
126 u8 code[440];
127 // 0x01b8
128 u32 diskseg;
129 // 0x01bc
130 u16 null;
131 // 0x01be
132 struct partition_s partitions[4];
133 // 0x01fe
134 u16 signature;
135} PACKED;
136
137#define MBR_SIGNATURE 0xaa55
138
139
140/****************************************************************
Kevin O'Connor4524bf72008-12-31 00:31:03 -0500141 * Disk command request
142 ****************************************************************/
143
144struct disk_op_s {
145 u64 lba;
Kevin O'Connor35ae7262009-01-19 15:44:44 -0500146 void *buf_fl;
Kevin O'Connor4524bf72008-12-31 00:31:03 -0500147 u16 count;
148 u8 driveid;
149 u8 command;
150};
151
Kevin O'Connor42337662009-08-10 00:06:37 -0400152#define CMD_RESET 0x00
153#define CMD_READ 0x02
154#define CMD_WRITE 0x03
155#define CMD_VERIFY 0x04
Kevin O'Connoraf5aabb2009-08-16 18:48:38 -0400156#define CMD_FORMAT 0x05
Kevin O'Connor42337662009-08-10 00:06:37 -0400157#define CMD_SEEK 0x07
158#define CMD_ISREADY 0x10
Kevin O'Connor4524bf72008-12-31 00:31:03 -0500159
160
161/****************************************************************
Kevin O'Connor609da232008-12-28 23:18:57 -0500162 * Global storage
163 ****************************************************************/
164
165struct chs_s {
166 u16 heads; // # heads
167 u16 cylinders; // # cylinders
168 u16 spt; // # sectors / track
169};
170
Kevin O'Connorc892b132009-08-11 21:59:37 -0400171struct drive_s {
172 u8 type; // Detected type of drive (ata/atapi/none)
Kevin O'Connor609da232008-12-28 23:18:57 -0500173 u8 removable; // Removable device flag
Kevin O'Connor609da232008-12-28 23:18:57 -0500174 u16 blksize; // block size
Kevin O'Connor51fd0a12009-09-12 13:20:14 -0400175 u32 cntl_id;
176 u32 cntl_info;
Kevin O'Connor48410fd2009-08-16 14:13:36 -0400177 u8 floppy_type; // Type of floppy (only for floppy drives).
Kevin O'Connor609da232008-12-28 23:18:57 -0500178
Kevin O'Connor0a924122009-02-08 19:43:47 -0500179 char model[41];
180
Kevin O'Connor609da232008-12-28 23:18:57 -0500181 u8 translation; // type of translation
182 struct chs_s lchs; // Logical CHS
183 struct chs_s pchs; // Physical CHS
184
185 u64 sectors; // Total sectors count
186};
187
Kevin O'Connor42337662009-08-10 00:06:37 -0400188#define DTYPE_NONE 0x00
Kevin O'Connoraf5aabb2009-08-16 18:48:38 -0400189#define DTYPE_FLOPPY 0x01
Kevin O'Connor42337662009-08-10 00:06:37 -0400190#define DTYPE_ATA 0x02
191#define DTYPE_ATAPI 0x03
Kevin O'Connora3855ad2009-08-16 21:59:40 -0400192#define DTYPE_RAMDISK 0x04
Kevin O'Connor42337662009-08-10 00:06:37 -0400193
194#define TRANSLATION_NONE 0
195#define TRANSLATION_LBA 1
196#define TRANSLATION_LARGE 2
197#define TRANSLATION_RECHS 3
198
Kevin O'Connorc892b132009-08-11 21:59:37 -0400199struct drives_s {
200 // info on each internally handled drive
201 struct drive_s drives[CONFIG_MAX_DRIVES];
Kevin O'Connor0a0e42e2009-08-16 12:09:44 -0400202 u8 drivecount;
Kevin O'Connor609da232008-12-28 23:18:57 -0500203 //
Kevin O'Connor0a0e42e2009-08-16 12:09:44 -0400204 // map between bios floppy/hd/cd id and driveid index into drives[]
205 u8 floppycount;
Kevin O'Connor4a16ef62008-12-31 00:09:28 -0500206 u8 cdcount;
Kevin O'Connor0a0e42e2009-08-16 12:09:44 -0400207 u8 idmap[3][CONFIG_MAX_EXTDRIVE];
Kevin O'Connor609da232008-12-28 23:18:57 -0500208};
209
Kevin O'Connor0a0e42e2009-08-16 12:09:44 -0400210#define EXTTYPE_FLOPPY 0
211#define EXTTYPE_HD 1
212#define EXTTYPE_CD 2
213
Kevin O'Connor51cfbe72009-08-18 22:38:49 -0400214#define EXTSTART_HD 0x80
215#define EXTSTART_CD 0xE0
216
Kevin O'Connor609da232008-12-28 23:18:57 -0500217
218/****************************************************************
219 * Function defs
220 ****************************************************************/
221
Kevin O'Connorc892b132009-08-11 21:59:37 -0400222// block.c
223extern struct drives_s Drives;
224void setup_translation(int driveid);
Kevin O'Connor0a0e42e2009-08-16 12:09:44 -0400225void map_floppy_drive(int driveid);
Kevin O'Connorc892b132009-08-11 21:59:37 -0400226void map_hd_drive(int driveid);
227void map_cd_drive(int driveid);
Kevin O'Connor51fd0a12009-09-12 13:20:14 -0400228void describe_drive(int driveid);
Kevin O'Connoraf5aabb2009-08-16 18:48:38 -0400229int send_disk_op(struct disk_op_s *op);
Kevin O'Connorc892b132009-08-11 21:59:37 -0400230void drive_setup();
Kevin O'Connor609da232008-12-28 23:18:57 -0500231
Kevin O'Connorf076a3e2008-02-25 22:25:15 -0500232// floppy.c
Kevin O'Connor941d3e42008-03-04 19:45:04 -0500233extern struct floppy_ext_dbt_s diskette_param_table2;
Kevin O'Connorc892b132009-08-11 21:59:37 -0400234void floppy_setup();
Kevin O'Connor51fd0a12009-09-12 13:20:14 -0400235int addFloppy(int floppyid, int ftype, int driver);
236void describe_floppy(int driveid);
Kevin O'Connora3855ad2009-08-16 21:59:40 -0400237int find_floppy_type(u32 size);
Kevin O'Connoraf5aabb2009-08-16 18:48:38 -0400238int process_floppy_op(struct disk_op_s *op);
Kevin O'Connorf076a3e2008-02-25 22:25:15 -0500239void floppy_tick();
Kevin O'Connor786502d2008-02-27 10:41:41 -0500240
Kevin O'Connor31d8c8a2008-03-04 19:56:41 -0500241// disk.c
Kevin O'Connor707298a2009-08-11 22:27:51 -0400242void disk_13(struct bregs *regs, u8 driveid);
243void disk_13XX(struct bregs *regs, u8 driveid);
244void cdemu_access(struct bregs *regs, u8 driveid, u16 command);
Kevin O'Connor31d8c8a2008-03-04 19:56:41 -0500245
246// cdrom.c
Kevin O'Connor707298a2009-08-11 22:27:51 -0400247void cdrom_13(struct bregs *regs, u8 driveid);
Kevin O'Connor31d8c8a2008-03-04 19:56:41 -0500248void cdemu_13(struct bregs *regs);
249void cdemu_134b(struct bregs *regs);
Kevin O'Connor7da1dcd2009-02-16 10:51:24 -0500250int cdrom_boot(int cdid);
Kevin O'Connor31d8c8a2008-03-04 19:56:41 -0500251
Kevin O'Connora3855ad2009-08-16 21:59:40 -0400252// ramdisk.c
Kevin O'Connor51fd0a12009-09-12 13:20:14 -0400253void describe_ramdisk(int driveid);
Kevin O'Connora3855ad2009-08-16 21:59:40 -0400254void ramdisk_setup();
255int process_ramdisk_op(struct disk_op_s *op);
256
Kevin O'Connor786502d2008-02-27 10:41:41 -0500257#endif // disk.h