summaryrefslogtreecommitdiffstats
path: root/image-hd.c
blob: 5f1bd217ee6a30a38dcc713c23394d600553bbc1 (plain)
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
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
/*
 * Copyright (c) 2011 Sascha Hauer <s.hauer@pengutronix.de>, Pengutronix
 *
 * This program is free software; you can redistribute it and/or modify
 * it under the terms of the GNU General Public License version 2
 * as published by the Free Software Foundation.
 *
 * 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., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
 */

#include <confuse.h>
#include <stdio.h>
#include <string.h>
#include <stdlib.h>
#include <errno.h>
#include <inttypes.h>

#include "genimage.h"

struct hdimage {
	cfg_bool_t partition_table;
	unsigned long long align;
	unsigned long long extended_lba;
};

struct partition_entry {
	unsigned char boot;

	unsigned char first_chs[3];

	unsigned char partition_type;

	unsigned char last_chs[3];

	uint32_t relative_sectors;
	uint32_t total_sectors;
};

static void hdimage_setup_chs(unsigned int lba, unsigned char *chs)
{
	const unsigned int hpc = 255;
	const unsigned int spt = 63;
	unsigned int s, c;

	chs[0] = (lba/spt)%hpc;
	c = (lba/(spt * hpc));
	s = (lba > 0) ?(lba%spt + 1) : 0;
	chs[1] = ((c & 0x300) >> 2) | (s & 0xff);
	chs[2] = (c & 0xff);
}

static int hdimage_setup_mbr(struct image *image, char *part_table)
{
	struct hdimage *hd = image->handler_priv;
	struct partition *part;
	int i = 0;

	image_log(image, 1, "writing MBR\n");
	list_for_each_entry(part, &image->partitions, list) {
		struct partition_entry *entry;

		if (!part->in_partition_table)
			continue;

		entry = (struct partition_entry *)(part_table + i *
				sizeof(struct partition_entry));

		entry->boot = part->bootable ? 0x80 : 0x00;
		if (!part->extended) {
			entry->partition_type = part->partition_type;
			entry->relative_sectors = part->offset/512;
			entry->total_sectors = part->size/512;
		}
		else {
			unsigned long long size = 0;
			struct partition *p = part;
			list_for_each_entry_from(p, &image->partitions, list) {
				if (!p->extended)
					break;
				size += hd->align + p->size;
			}
			entry->partition_type = 0x0F;
			entry->relative_sectors = (part->offset - hd->align)/512;
			entry->total_sectors = size/512;
		}
		hdimage_setup_chs(entry->relative_sectors, entry->first_chs);
		hdimage_setup_chs(entry->relative_sectors +
				entry->total_sectors - 1, entry->last_chs);

		if (part->extended)
			break;
		i++;
	}
	part_table += 4 * sizeof(struct partition_entry);
	part_table[0] = 0x55;
	part_table[1] = 0xaa;
	return 0;
}

static int hdimage_setup_ebr(struct image *image, struct partition *part, char *ebr)
{
	struct hdimage *hd = image->handler_priv;
	struct partition_entry *entry;

	image_log(image, 1, "writing EBR\n");

	entry = (struct partition_entry *)ebr;

	entry->boot = 0x00;
	entry->partition_type = part->partition_type;
	entry->relative_sectors = hd->align/512;
	entry->total_sectors = part->size/512;
	hdimage_setup_chs(entry->relative_sectors, entry->first_chs);
	hdimage_setup_chs(entry->relative_sectors +
			entry->total_sectors - 1, entry->last_chs);
	struct partition *p = part;
	list_for_each_entry_continue(p, &image->partitions, list) {
		++entry;
		entry->boot = 0x00;
		entry->partition_type = 0x0F;
		entry->relative_sectors = (p->offset - hd->align - hd->extended_lba)/512;
		entry->total_sectors = (p->size + hd->align)/512;
		hdimage_setup_chs(entry->relative_sectors, entry->first_chs);
		hdimage_setup_chs(entry->relative_sectors +
				entry->total_sectors - 1, entry->last_chs);
		break;
	}

	ebr += 4 * sizeof(struct partition_entry);
	ebr[0] = 0x55;
	ebr[1] = 0xaa;
	return 0;
}

static int hdimage_generate(struct image *image)
{
	struct partition *part;
	struct hdimage *hd = image->handler_priv;
	enum pad_mode mode = MODE_OVERWRITE;
	const char *outfile = imageoutfile(image);
	int ret;

	list_for_each_entry(part, &image->partitions, list) {
		struct image *child;
		const char *infile;

		ret = pad_file(NULL, outfile, part->offset, 0x0, mode);
		if (ret) {
			image_error(image, "failed to pad image to size %lld\n",
					part->offset);
			return ret;
		}
		mode = MODE_APPEND;

		if (part->extended) {
			char ebr[4*sizeof(struct partition_entry)+2];
			memset(ebr, 0, sizeof(ebr));
			ret = hdimage_setup_ebr(image, part, ebr);
			ret = insert_data(ebr, outfile, sizeof(ebr),
					part->offset - hd->align + 446);
			if (ret) {
				image_error(image, "failed to write EBR\n");
				return ret;
			}
		}

		if (!part->image)
			continue;

		child = image_get(part->image);
		infile = imageoutfile(child);

		ret = pad_file(infile, outfile, part->size, 0x0, MODE_APPEND);

		if (ret) {
			image_error(image, "failed to write image partition '%s'\n",
					part->name);
			return ret;
		}
	}

	if (hd->partition_table) {
		char part_table[4*sizeof(struct partition_entry)+2];

		memset(part_table, 0, sizeof(part_table));
		ret = hdimage_setup_mbr(image, part_table);
		if (ret)
			return ret;

		ret = insert_data(part_table, outfile, sizeof(part_table), 446);
		if (ret) {
			image_error(image, "failed to write MBR\n");
			return ret;
		}
		mode = MODE_APPEND;
	}

	return 0;
}

static unsigned long long roundup(unsigned long long value, unsigned long long align)
{
	return ((value - 1)/align + 1) * align;
}

static int hdimage_setup(struct image *image, cfg_t *cfg)
{
	struct partition *part;
	int has_extended;
	int partition_table_entries = 0;
	unsigned long long now = 0;
	struct hdimage *hd = xzalloc(sizeof(*hd));

	hd->align = cfg_getint_suffix(cfg, "align");
	hd->partition_table = cfg_getbool(cfg, "partition-table");

	if ((hd->align % 512) || (hd->align == 0)) {
		image_error(image, "partition alignment (%lld) must be a"
				"multiple of 1 sector (512 bytes)\n", hd->align);
		return -EINVAL;
	}
	list_for_each_entry(part, &image->partitions, list) {
		if (part->in_partition_table)
			++partition_table_entries;
	}
	has_extended = partition_table_entries > 4;
	partition_table_entries = 0;
	list_for_each_entry(part, &image->partitions, list) {
		if (part->image) {
			struct image *child = image_get(part->image);
			if (!child) {
				image_error(image, "could not find %s\n",
						part->image);
				return -EINVAL;
			}
			if (!part->size)
				part->size = roundup(child->size, hd->align);
		}
		if (!part->size) {
			image_error(image, "part %s size must not be zero\n",
					part->name);
			return -EINVAL;
		}
		if (part->size % 512) {
			image_error(image, "part %s size (%lld) must be a"
					"multiple of 1 sector (512 bytes)\n",
					part->name, part->size);
			return -EINVAL;
		}
		/* reserve space for extended boot record if necessary */
		if (part->in_partition_table)
			++partition_table_entries;
		if (has_extended && (partition_table_entries > 3))
			part->extended = cfg_true;
		if (part->extended) {
			if (!hd->extended_lba)
				hd->extended_lba = now;
			now = roundup(now, hd->align);
			now += hd->align;
		}
		if (part->in_partition_table && (part->offset % hd->align)) {
			image_error(image, "part %s offset (%lld) must be a"
					"multiple of %lld bytes\n",
					part->name, part->offset, hd->align);
			return -EINVAL;
		}
		if (part->offset || !part->in_partition_table) {
			if (now > part->offset) {
				image_error(image, "part %s overlaps with previous partition\n",
						part->name);
				return -EINVAL;
			}
		} else {
			if (!now && hd->partition_table)
				now = 512;
			part->offset = roundup(now, hd->align);
		}
		now = part->offset + part->size;
	}

	if (now > image->size) {
		image_error(image, "partitions exceed device size\n");
		return -EINVAL;
	}

	image->handler_priv = hd;

	return 0;
}

cfg_opt_t hdimage_opts[] = {
	CFG_STR("align", "512", CFGF_NONE),
	CFG_BOOL("partition-table", cfg_true, CFGF_NONE),
	CFG_END()
};

struct image_handler hdimage_handler = {
	.type = "hdimage",
	.generate = hdimage_generate,
	.setup = hdimage_setup,
	.opts = hdimage_opts,
};