summaryrefslogtreecommitdiffstats
path: root/contrib/syslinux-4.02/com32/gpllib/disk/msdos.c
blob: affec43b46c538db6922287788fac44cfeab83f5 (plain) (blame)
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
/* ----------------------------------------------------------------------- *
 *
 *   Copyright 2009 Pierre-Alexandre Meyer
 *
 *   Some parts borrowed from chain.c32:
 *
 *   Copyright 2003-2009 H. Peter Anvin - All Rights Reserved
 *   Copyright 2009 Intel Corporation; author: H. Peter Anvin
 *
 *   This file is part of Syslinux, and is made available under
 *   the terms of the GNU General Public License version 2.
 *
 * ----------------------------------------------------------------------- */

#include <stdlib.h>

#include <disk/common.h>
#include <disk/geom.h>
#include <disk/msdos.h>
#include <disk/partition.h>
#include <disk/read.h>

static inline int is_extended_partition(struct part_entry *ptab)
{
    return (ptab->ostype == 0x05 ||
	    ptab->ostype == 0x0f || ptab->ostype == 0x85);
}

static inline int msdos_magic_present(const char *ptab)
{
    return (*(uint16_t *) (ptab + 0x1fe) == 0xaa55);
}

/**
 * process_extended_partition - execute a callback for each partition contained listed in an ebr
 * @drive_info:		driveinfo struct describing the drive
 * @partition_offset:	Absolute start (lba) of the extended partition
 * @ebr_offset:		Relative start (lba) of the current ebr processed within
 *			the extended partition
 * @callback:		Callback to execute
 * @error:		Buffer for I/O errors
 * @nb_part_seen:	Number of partitions found on the disk so far
 **/
static int process_extended_partition(struct driveinfo *drive_info,
				      const int partition_offset,
				      const int ebr_offset,
				      p_callback callback, int nb_part_seen)
{
    int status = 0;
    /* The ebr is located at the first sector of the extended partition */
    char *ebr = malloc(SECTOR * sizeof(char));

    if (read_sectors(drive_info, ebr, partition_offset + ebr_offset, 1) == -1)
	goto abort;

    /* Check msdos magic signature */
    if (!msdos_magic_present(ebr))
	goto abort;

    struct part_entry *ptab =
	(struct part_entry *)(ebr + PARTITION_TABLES_OFFSET);

    for (int i = 0; i < 4; i++) {
	if (status == -1)
	    goto abort;

	if (!is_extended_partition(&ptab[i])) {
	    /*
	     * This EBR partition table entry points to the
	     * logical partition associated to that EBR
	     */
	    int logical_partition_start = ebr_offset + ptab[i].start_lba;

	    /* Last EBR in the extended partition? */
	    if (!logical_partition_start)
		continue;

	    /*
	     * Check for garbage:
	     * 3rd and 4th entries in an EBR should be zero
	     * Some (malformed) partitioning software still add some
	     * data partitions there.
	     */
	    if (ptab[i].start_lba <= 0 || ptab[i].length <= 0)
		continue;

	    nb_part_seen++;
	    callback(drive_info,
		     &ptab[i],
		     partition_offset + logical_partition_start, nb_part_seen);
	} else
	    status = process_extended_partition(drive_info,
						partition_offset,
						ptab[i].start_lba,
						callback, nb_part_seen);
    }

    free(ebr);
    return 0;

abort:
    free(ebr);
    return -1;
}

/**
 * process_mbr - execute a callback for each partition contained in an {m,e}br
 * @drive_info:	driveinfo struct describing the drive
 * @ptab:	Pointer to the partition table
 * @callback:	Callback to execute
 **/
static int process_mbr(struct driveinfo *drive_info, struct part_entry *ptab,
		       p_callback callback)
{
    int status = 0;

    for (int i = 0; i < 4; i++) {
	if (status == -1)
	    return -1;

	if (ptab[i].start_sect > 0) {
	    if (is_extended_partition(&ptab[i])) {
		callback(drive_info, &ptab[i], ptab[i].start_lba, i + 1);
		status =
		    process_extended_partition(drive_info, ptab[i].start_lba, 0,
					       callback, 4);
	    } else
		callback(drive_info, &ptab[i], ptab[i].start_lba, i + 1);
	}
    }

    return 0;
}

/**
 * parse_partition_table - execute a callback for each partition entry
 * @d:		driveinfo struct describing the drive
 * @callback:	Callback to execute
 *
 * The signature of the callback should be the following:
 *
 * void callback(struct driveinfo *drive_info,
 *		 struct part_entry *ptab,
 *		 int offset_root,
 *		 int nb_part_seen)
 **/
int parse_partition_table(struct driveinfo *d, p_callback callback)
{
    char *mbr = malloc(SECTOR * sizeof(char));

    if (read_mbr(d->disk, mbr) == -1)
	return -1;
    else {
	/* Check msdos magic signature */
	if (!msdos_magic_present(mbr))
	    return -1;

	struct part_entry *ptab =
	    (struct part_entry *)(mbr + PARTITION_TABLES_OFFSET);
	return process_mbr(d, ptab, callback);
    }
}