Training courses

Kernel and Embedded Linux

Bootlin training courses

Embedded Linux, kernel,
Yocto Project, Buildroot, real-time,
graphics, boot time, debugging...

Bootlin logo

Elixir Cross Referencer

  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
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
/* $NetBSD: ahdilabel.c,v 1.11 2016/10/09 14:51:26 christos Exp $ */

/*
 * Copyright (c) 1999 The NetBSD Foundation, Inc.
 * All rights reserved.
 *
 * This code is derived from software contributed to The NetBSD Foundation
 * by Julian Coleman.
 *
 * Redistribution and use in source and binary forms, with or without
 * modification, are permitted provided that the following conditions
 * are met:
 * 1. Redistributions of source code must retain the above copyright
 *    notice, this list of conditions and the following disclaimer.
 * 2. Redistributions in binary form must reproduce the above copyright
 *    notice, this list of conditions and the following disclaimer in the
 *    documentation and/or other materials provided with the distribution.
 *
 * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS
 * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
 * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
 * PURPOSE ARE DISCLAIMED.  IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS
 * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
 * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
 * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
 * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
 * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
 * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
 * POSSIBILITY OF SUCH DAMAGE.
 */

#include "privahdi.h"
#include <ctype.h>
#include <errno.h>
#include <stdio.h>
#include <stdlib.h>
#include <strings.h>

/*
 * I think we can safely assume a fixed blocksize - AHDI won't support
 * something different...
 */
#define	BLPM            ((1024 * 1024) / DEV_BSIZE)
#define	UNITS_SECTORS	0
#define	UNITS_CTS	1
#define	PART_ROOT	0
#define	PART_START	1
#define	PART_END	2

int		 main (int, char*[]);
void		 show_parts (struct ahdi_ptable*, int, int, int);
int		 get_input (char *, int);
char		*sec_to_cts (struct ahdi_ptable*, u_int32_t, char *, size_t);
u_int32_t	 read_sector (struct ahdi_ptable*, char *, int, int);
void		 change_part (struct ahdi_ptable*, int, int);

int
main (int argc, char *argv[])
{
	struct ahdi_ptable	ptable;
	int			flags, rv, key, units;

	if (argc < 2) {
		fprintf (stderr, "usage: %s raw_disk\n", argv[0]);
		exit (EXIT_FAILURE);
	}
	
	flags = AHDI_IGN_CKSUM;
	while ((rv = ahdi_readlabel(&ptable, argv[1], flags)) != 1) {
		switch (rv) {
		case -1:
			fprintf (stderr,
			    "%s: %s: %s\n", argv[0], argv[1],
			    strerror (errno));
			exit (EXIT_FAILURE);
			break;
		case -2:
			fprintf (stderr,
			    "%s: disk not 512 bytes/sector\n", argv[0]);
			exit (EXIT_FAILURE);
			break;
		case -3:
			(void) fpurge(stdin);
			printf ("No AHDI partitions found.  Continue (y/N)?");
			if (toupper(getchar()) == 'Y') {
				flags |= FORCE_AHDI;
			} else
				exit (EXIT_FAILURE);
			break;
		case -4:
		case -5:
		case -6:
			(void) fpurge(stdin);
			printf ("Errors reading AHDI partition table.  Override (y/N)? ");
			if (toupper(getchar()) == 'Y') {
				flags |= AHDI_IGN_EXISTS | AHDI_IGN_EXT |
				    AHDI_IGN_SPU;
			} else
				exit (EXIT_FAILURE);
			break;
		case 1:
			/* Everything is OK */
			break;
		default:
			exit (EXIT_FAILURE);
			break;
		}
	}

	units = UNITS_SECTORS;
	flags = AHDI_KEEP_BOOT | AHDI_KEEP_BSL;
	show_parts (&ptable, 0, ptable.nparts, units);
	printf ("Preserve boot sector - ");
	flags & AHDI_KEEP_BOOT ? printf ("yes\n") :
	    printf ("no\n");
	printf ("Preserve bad sector list - ");
	flags & AHDI_KEEP_BSL ? printf ("yes\n") :
	    printf ("no\n");
	key = 0;
	while (key != 'Q') {
		(void) fpurge(stdin);
		printf ("Change [a-p], r)ecalculate, s)how, u)nits, w)rite, z)ero or q)uit ");
		key = toupper(getchar());
		if (key == EOF)
			key = 'Q';
		if (key >= 'A' && key <= 'P')
			change_part (&ptable, key - 'A', units);
		if (key == 'R') {
			if (ahdi_buildlabel (&ptable)) {
				printf ("Partiton table adjusted\n");
			} else {
				printf ("No changes necessary\n");
			}
		}
		if (key == 'S') {
			show_parts (&ptable, 0, ptable.nparts, units);
			printf ("Preserve boot sector - ");
			flags & AHDI_KEEP_BOOT ? printf ("yes\n") :
			    printf ("no\n");
			printf ("Preserve bad sector list - ");
			flags & AHDI_KEEP_BSL ? printf ("yes\n") :
			    printf ("no\n");
		}
		if (key == 'U') {
			if (units == UNITS_SECTORS) {
				printf ("Units now cylinder/track/sector\n");
				units = UNITS_CTS;
			} else {
				printf ("Units now sector\n");
				units = UNITS_SECTORS;
			}
		}
		if (key == 'W') {
			if ((rv = ahdi_writelabel (&ptable, argv[1], flags)) < 0) {
				if (rv == -1)
					perror ("\0");
				if (rv == -2)
					printf ("Invalid number of partitions!\n");
				if (rv == -3)
					printf ("GEM partition should be BGM or BGM partition should be GEM!\n");
				if (rv == -4)
					printf ("Partition overlaps root sector or bad sector list (starts before sector 2)!\n");
				if (rv == -5)
					printf ("Partition extends past end of disk!\n");
				if (rv == -6)
					printf ("Partitions overlap!\n");
				if (rv == -7)
					printf ("Partition overlaps auxiliary root!\n");
				if (rv == -8)
					printf ("More than 4 partitions in root sector!\n");
				if (rv == -9)
					printf ("More than 1 partition in an auxiliary root!\n");
				if (rv < -1 && ahdi_errp1 != -1)
					printf ("\tpartition %c has errors.\n",
					    ahdi_errp1 + 'a');
				if (rv < -1 && ahdi_errp2 != -1)
					printf ("\tpartition %c has errors.\n",
					    ahdi_errp2 + 'a');
			}
		}
		if (key == 'Z') {
			(void) fpurge(stdin);
			printf ("Preserve boot sector? ");
			if (flags & AHDI_KEEP_BOOT) {
				printf ("[y] ");
				if (toupper (getchar ()) == 'N')
					flags &= ~AHDI_KEEP_BOOT;
			} else {
				printf ("[n] ");
				if (toupper (getchar ()) == 'Y')
					flags |= AHDI_KEEP_BOOT;
			}
			(void) fpurge(stdin);
			printf ("Preserve bad sector list? ");
			if (flags & AHDI_KEEP_BSL) {
				printf ("[y] ");
				if (toupper (getchar ()) == 'N')
					flags &= ~AHDI_KEEP_BSL;
			} else {
				printf ("[n] ");
				if (toupper (getchar ()) == 'Y')
					flags |= AHDI_KEEP_BSL;
			}
		}
	}
	return (0);
}

void
show_parts (struct ahdi_ptable *ptable, int start, int finish, int units)
{
	int	i;

	printf ("Disk information :\n");
	printf ("  sectors/track: %d\n", ptable->nsectors);
	printf ("  tracks/cylinder: %d\n", ptable->ntracks);
	printf ("  sectors/cylinder: %d\n", ptable->secpercyl);
	printf ("  cylinders: %d\n", ptable->ncylinders);
	printf ("  total sectors: %d\n", ptable->secperunit);

	if (units == UNITS_SECTORS) {
		printf ("  #  id      root     start       end      size   MBs\n");
		for (i = start; i < finish; i++) {
			if (finish - start > 10 && i - start == 8) {
				(void) fpurge(stdin);
				printf ("-- Press return for more -- ");
				(void) getchar();
			}
			printf ("  %c %c%c%c  %8u  %8u  %8u  %8u  (%4u)\n",
			    i + 'a', ptable->parts[i].id[0],
			    ptable->parts[i].id[1], ptable->parts[i].id[2],
			    ptable->parts[i].root, ptable->parts[i].start,
			    ptable->parts[i].start +
			    (ptable->parts[i].size ?
			    ptable->parts[i].size - 1 : 0),
			    ptable->parts[i].size,
			    (ptable->parts[i].size + (BLPM >> 1)) / BLPM);
		}
	} else {
		u_int32_t	cylinder, track, sector;
		printf ("  #  id          root         start           end          size    MBs\n");
		for (i = start; i < finish; i++) {
			if (finish - start > 10 && i - start == 8) {
				(void) fpurge(stdin);
				printf ("-- Press return for more -- ");
				(void) getchar();
			}
			printf ("  %c %c%c%c  ", i + 'a',
			    ptable->parts[i].id[0], ptable->parts[i].id[1],
			    ptable->parts[i].id[2]);
			sector = ptable->parts[i].root;
			cylinder = sector / ptable->secpercyl;
			sector -= cylinder * ptable->secpercyl;
			track = sector / ptable->nsectors;
			sector -= track * ptable->nsectors;
			printf ("%5u/%2u/%3u  ", cylinder, track, sector);
			sector = ptable->parts[i].start;
			cylinder = sector / ptable->secpercyl;
			sector -= cylinder * ptable->secpercyl;
			track = sector / ptable->nsectors;
			sector -= track * ptable->nsectors;
			printf ("%5u/%2u/%3u  ", cylinder, track, sector);
			sector = ptable->parts[i].start +
			    (ptable->parts[i].size ?
			    ptable->parts[i].size - 1 : 0);
			cylinder = sector / ptable->secpercyl;
			sector -= cylinder * ptable->secpercyl;
			track = sector / ptable->nsectors;
			sector -= track * ptable->nsectors;
			printf ("%5u/%2u/%3u  ", cylinder, track, sector);
			sector = ptable->parts[i].size;
			cylinder = sector / ptable->secpercyl;
			sector -= cylinder * ptable->secpercyl;
			track = sector / ptable->nsectors;
			sector -= track * ptable->nsectors;
			printf ("%5u/%2u/%3u   ", cylinder, track, sector);
			printf ("(%4u)\n",
			    (ptable->parts[i].size + (BLPM >> 1)) / BLPM);
		}
	}
}

int
get_input (char *buf, int len)
{
	int count, key;

	count = 0;
	(void) fpurge(stdin);
	while (count < (len - 1) && (key = getchar()) != '\n' && key != '\r') {
		buf[count] = key;
		count++;
	}
	buf[count] = '\0';
	return(count);
}

char *
sec_to_cts (struct ahdi_ptable *ptable, u_int32_t sector, char *cts, size_t len)
{
	u_int32_t	cylinder, track;

	cylinder = sector / ptable->secpercyl;
	sector -= cylinder * ptable->secpercyl;
	track = sector / ptable->nsectors;
	sector -= track * ptable->nsectors;
	snprintf (cts, len, "%u/%u/%u", cylinder, track, sector);
	return (cts);
}

u_int32_t
read_sector (struct ahdi_ptable *ptable, char *buf, int part, int se)
{
	u_int32_t	sector, track, cylinder;
	int		i;

	sector = track = cylinder = 0;
	if ((strchr (buf, '/') != NULL) &&
	    ((sscanf (buf, "%u/%u/%u", &cylinder, &track, &sector) == 3) ||
	    (sscanf (buf, "%u/%u/", &cylinder, &track) == 2) ||
	    (sscanf (buf, "%u/", &cylinder) == 1))) {
		if (sector > ptable->nsectors || track > ptable->ntracks ||
		    cylinder > ptable->ncylinders)
			return (0);
		sector += ptable->nsectors * track;
		sector += ptable->secpercyl * cylinder;
		return (sector);
	}
	if (buf[0] == '-' && buf[1]) {
		if (buf[1] == '1' && se == PART_END)
			/* Extend to end of disk */
			return (ptable->secperunit -
			    ptable->parts[part].start);
		i = (int) (toupper ((unsigned char)(buf[1]) - 'A'));
		if (i >= 0 && i <= ptable->nparts ) {
			if (se == PART_ROOT && part > i)
				/* Root after partition ... */
				return (ptable->parts[i].start +
				    ptable->parts[i].size);
			if (se == PART_START && part > i) {
				/* Start after partition ... */
				if (ptable->parts[part].root)
					return (ptable->parts[i].start +
					    ptable->parts[i].size + 1);
				else
					return (ptable->parts[i].start +
					    ptable->parts[i].size);
			}
			if (se == PART_END && part < i)
				/* End before partition ... */
				return (ptable->parts[i].root -
				    ptable->parts[part].start);
		}
		return (0);
	}
	if (sscanf (buf, "%u", &sector) == 1) {
		if (buf[strlen (buf) - 1] == 'm' ||
		    buf[strlen (buf) - 1] == 'M')
			sector *= BLPM;
		return (sector);
	}
	return (0);
}

void
change_part (struct ahdi_ptable *ptable, int part, int units)
{
#define BUFLEN	20
#define CTSLEN	64
	char		buf[BUFLEN], cts[CTSLEN];
	u_int32_t	sector;
	
	if (part > ptable->nparts) {
		part = ptable->nparts;
		printf ("Changing partition %c!\n", part + 'a');
		ptable->nparts++;
	}
	if (part == ptable->nparts)
		ptable->nparts++;
	show_parts (ptable, part, part + 1, units);

	printf ("id [%c%c%c] ", ptable->parts[part].id[0],
	    ptable->parts[part].id[1], ptable->parts[part].id[2]);
	if (get_input (&buf[0], BUFLEN) > 2) {
		ptable->parts[part].id[0] = buf[0];
		ptable->parts[part].id[1] = buf[1];
		ptable->parts[part].id[2] = buf[2];
	}

	printf ("root [%8u (%s)] ", ptable->parts[part].root,
	    sec_to_cts (ptable, ptable->parts[part].root, cts, sizeof(cts)));
	if (get_input (&buf[0], BUFLEN)) {
		sector = read_sector (ptable, buf, part, PART_ROOT);
		ptable->parts[part].root = sector;
	}

	printf ("start [%8u (%s)] ", ptable->parts[part].start,
	    sec_to_cts (ptable, ptable->parts[part].start, cts, sizeof(cts)));
	if (get_input (&buf[0], BUFLEN)) {
		sector = read_sector (ptable, buf, part, PART_START);
		ptable->parts[part].start = sector;
	}

	printf ("size [%8u (%s) (%4uM)] ", ptable->parts[part].size,
	    sec_to_cts (ptable, ptable->parts[part].size, cts, sizeof(cts)),
	    (ptable->parts[part].size + (BLPM >> 1)) / BLPM);
	if (get_input (&buf[0], BUFLEN)) {
		sector = read_sector (ptable, buf, part, PART_END);
		ptable->parts[part].size = sector;
	}

/*
	printf ("NetBSD disk letter [%c] ", ptable->parts[part].letter + 'a');
	if (get_input (&buf[0], BUFLEN)) {
		buf[0] = tolower(buf[0]);
		if (buf[0] == 'a' || (buf[0] >= 'd' && buf[0] <= 'p'))
			ptable->parts[part].letter = buf[0] - 'a';
*/

	if (!ptable->parts[part].start && !ptable->parts[part].size) {
	    if (part == ptable->nparts - 1)
		ptable->nparts--;
	}
}