-
Notifications
You must be signed in to change notification settings - Fork 15
/
int13.c
212 lines (203 loc) · 6.01 KB
/
int13.c
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
/*
* Copyright (C) 2012 Michael Brown <mbrown@fensystems.co.uk>.
*
* This program is free software; you can redistribute it and/or
* modify it under the terms of the GNU General Public License as
* published by the Free Software Foundation; either version 2 of the
* License, or (at your option) any later version.
*
* This program is distributed in the hope that it will be useful, but
* WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
* General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program; if not, write to the Free Software
* Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA
* 02110-1301, USA.
*/
#include <string.h>
#include <stdio.h>
#include <ntboot.h>
#include <int13.h>
#include <vdisk.h>
/** Emulated drive number */
static int vdisk_drive;
/**
* Initialise emulation
*
* @ret drive Emulated drive number
*/
int initialise_int13 (void)
{
/* Determine drive number */
vdisk_drive = (0x80 | INT13_DRIVE_COUNT++);
DBG ("Emulating drive %#02x\n", vdisk_drive);
return vdisk_drive;
}
/**
* INT 13, 08 - Get drive parameters
*
* @ret ch Low bits of maximum cylinder number
* @ret cl (bits 7:6) High bits of maximum cylinder number
* @ret cl (bits 5:0) Maximum sector number
* @ret dh Maximum head number
* @ret dl Number of drives
* @ret ah Status code
*/
static void int13_get_parameters (struct bootapp_callback_params *params)
{
unsigned int max_cylinder = (VDISK_CYLINDERS - 1);
unsigned int max_head = (VDISK_HEADS - 1);
unsigned int max_sector = (VDISK_SECTORS_PER_TRACK - 0 /* sic */);
unsigned int num_drives;
unsigned int min_num_drives;
/* Calculate number of drives to report */
num_drives = INT13_DRIVE_COUNT;
min_num_drives = ((vdisk_drive & 0x7f) + 1);
if (num_drives < min_num_drives)
num_drives = min_num_drives;
/* Fill in drive parameters */
params->ch = (max_cylinder & 0xff);
params->cl = (((max_cylinder >> 8) << 6) | max_sector);
params->dh = max_head;
params->dl = num_drives;
DBG2 ("Get parameters: C/H/S = %d/%d/%d, drives = %d\n",
(max_cylinder + 1), (max_head + 1), max_sector, num_drives);
/* Success */
params->ah = 0;
}
/**
* INT 13, 15 - Get disk type
*
* @ret cx:dx Sector count
* @ret ah Type code
*/
static void int13_get_disk_type (struct bootapp_callback_params *params)
{
uint32_t sector_count = VDISK_COUNT;
uint8_t drive_type = INT13_DISK_TYPE_HDD;
/* Fill in disk type */
params->cx = (sector_count >> 16);
params->dx = (sector_count & 0xffff);
params->ah = drive_type;
DBG2 ("Get disk type: sectors = %#08x, type = %d\n",
sector_count, drive_type);
}
/**
* INT 13, 41 - Extensions installation check
*
* @v bx 0x55aa
* @ret bx 0xaa55
* @ret cx Extensions API support bitmap
* @ret ah API version
*/
static void int13_extension_check (struct bootapp_callback_params *params)
{
/* Fill in extension information */
params->bx = 0xaa55;
params->cx = INT13_EXTENSION_LINEAR;
params->ah = INT13_EXTENSION_VER_1_X;
DBG2 ("Extensions installation check\n");
}
/**
* INT 13, 48 - Get extended parameters
*
* @v ds:si Drive parameter table
* @ret ah Status code
*/
static void
int13_get_extended_parameters (struct bootapp_callback_params *params)
{
struct int13_disk_parameters *disk_params;
/* Fill in extended parameters */
disk_params = REAL_PTR (params->ds, params->si);
memset (disk_params, 0, sizeof (*disk_params));
disk_params->bufsize = sizeof (*disk_params);
disk_params->flags = INT13_FL_DMA_TRANSPARENT;
disk_params->cylinders = VDISK_CYLINDERS;
disk_params->heads = VDISK_HEADS;
disk_params->sectors_per_track = VDISK_SECTORS_PER_TRACK;
disk_params->sectors = VDISK_COUNT;
disk_params->sector_size = VDISK_SECTOR_SIZE;
DBG2 ("Get extended parameters: C/H/S = %d/%d/%d, sectors = %#08llx "
"(%d bytes)\n", disk_params->cylinders, disk_params->heads,
disk_params->sectors_per_track, disk_params->sectors,
disk_params->sector_size);
/* Success */
params->ah = 0;
}
/**
* INT 13, 42 - Extended read
*
* @v ds:si Disk address packet
* @ret ah Status code
*/
static void int13_extended_read (struct bootapp_callback_params *params)
{
struct int13_disk_address *disk_address;
void *data;
/* Read from emulated disk */
disk_address = REAL_PTR (params->ds, params->si);
data = REAL_PTR (disk_address->buffer.segment,
disk_address->buffer.offset);
vdisk_read (disk_address->lba, disk_address->count, data);
/* Success */
params->ah = 0;
}
/**
* Emulate INT 13 drive
*
* @v params Parameters
*/
void emulate_int13 (struct bootapp_callback_params *params)
{
int command = params->ah;
int drive = params->dl;
int min_num_drives;
unsigned long eflags;
if (drive == vdisk_drive)
{
/* Emulated drive - handle internally */
/* Populate eflags with a sensible starting value */
__asm__ ("pushf\n\t"
"pop %0\n\t"
: "=r" (eflags));
params->eflags = (eflags & ~CF);
/* Handle command */
switch (command)
{
case INT13_GET_PARAMETERS:
int13_get_parameters (params);
break;
case INT13_GET_DISK_TYPE:
int13_get_disk_type (params);
break;
case INT13_EXTENSION_CHECK:
int13_extension_check (params);
break;
case INT13_GET_EXTENDED_PARAMETERS:
int13_get_extended_parameters (params);
break;
case INT13_EXTENDED_READ:
int13_extended_read (params);
break;
default:
DBG ("Unrecognised INT 13,%02x\n", command);
params->eflags |= CF;
break;
}
}
else
{
/* Pass through command to underlying INT 13 */
call_interrupt (params);
/* Modify drive count, if applicable */
if (command == INT13_GET_PARAMETERS)
{
min_num_drives = ((vdisk_drive & 0x7f) + 1);
if (params->dl < min_num_drives)
params->dl = min_num_drives;
}
}
}