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
|
#include <com32.h>
#include <stdlib.h>
#include <string.h>
#include <disk/geom.h>
#include <disk/util.h>
#include <disk/read.h>
/**
**/
void *read_mbr(int drive)
{
struct driveinfo drive_info;
drive_info.disk = drive;
/* MBR: lba = 0, 1 sector */
return read_sectors(&drive_info, 0, 1);
}
/**
* dev_read - read from a drive
* @drive: Drive number
* @lba: Position to start reading from
* @sectors: Number of sectors to read
*
* High-level routine to read from a hard drive.
**/
void *dev_read(int drive, unsigned int lba, int sectors)
{
struct driveinfo drive_info;
drive_info.disk = drive;
return read_sectors(&drive_info, lba, sectors);
}
/**
* read_sectors - read several sectors from disk
* @drive_info: driveinfo struct describing the disk
* @lba: Position to read
* @sectors: Number of sectors to read
*
* Return a pointer to a malloc'ed buffer containing the data.
**/
void *read_sectors(struct driveinfo* drive_info, const unsigned int lba,
const int sectors)
{
com32sys_t inreg;
struct ebios_dapa *dapa = __com32.cs_bounce;
void *buf = (char *)__com32.cs_bounce + sectors * SECTOR;
void *data;
if (get_drive_parameters(drive_info) == -1)
return NULL;
memset(&inreg, 0, sizeof inreg);
if (drive_info->ebios) {
dapa->len = sizeof(*dapa);
dapa->count = sectors;
dapa->off = OFFS(buf);
dapa->seg = SEG(buf);
dapa->lba = lba;
inreg.esi.w[0] = OFFS(dapa);
inreg.ds = SEG(dapa);
inreg.edx.b[0] = drive_info->disk;
inreg.eax.b[1] = 0x42; /* Extended read */
} else {
unsigned int c, h, s;
if (!drive_info->cbios) {
/* We failed to get the geometry */
if (lba)
return NULL; /* Can only read MBR */
s = 1; h = 0; c = 0;
} else
lba_to_chs(drive_info, lba, &s, &h, &c);
if ( s > 63 || h > 256 || c > 1023 )
return NULL;
inreg.eax.w[0] = 0x0201; /* Read one sector */
inreg.ecx.b[1] = c & 0xff;
inreg.ecx.b[0] = s + (c >> 6);
inreg.edx.b[1] = h;
inreg.edx.b[0] = drive_info->disk;
inreg.ebx.w[0] = OFFS(buf);
inreg.es = SEG(buf);
}
/* Perform the read */
if (int13_retry(&inreg, NULL))
return NULL; /* Give up */
data = malloc(sectors * SECTOR);
if (data)
memcpy(data, buf, sectors * SECTOR);
return data;
}
|