ahdilabel.c revision 1.7 1 1.7 martin /* $NetBSD: ahdilabel.c,v 1.7 2008/04/28 20:23:15 martin Exp $ */
2 1.1 leo
3 1.1 leo /*
4 1.1 leo * Copyright (c) 1999 The NetBSD Foundation, Inc.
5 1.1 leo * All rights reserved.
6 1.1 leo *
7 1.1 leo * This code is derived from software contributed to The NetBSD Foundation
8 1.1 leo * by Julian Coleman.
9 1.1 leo *
10 1.1 leo * Redistribution and use in source and binary forms, with or without
11 1.1 leo * modification, are permitted provided that the following conditions
12 1.1 leo * are met:
13 1.1 leo * 1. Redistributions of source code must retain the above copyright
14 1.1 leo * notice, this list of conditions and the following disclaimer.
15 1.1 leo * 2. Redistributions in binary form must reproduce the above copyright
16 1.1 leo * notice, this list of conditions and the following disclaimer in the
17 1.1 leo * documentation and/or other materials provided with the distribution.
18 1.1 leo *
19 1.1 leo * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS
20 1.1 leo * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
21 1.1 leo * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
22 1.1 leo * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS
23 1.1 leo * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
24 1.1 leo * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
25 1.1 leo * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
26 1.1 leo * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
27 1.1 leo * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
28 1.1 leo * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
29 1.1 leo * POSSIBILITY OF SUCH DAMAGE.
30 1.1 leo */
31 1.1 leo
32 1.1 leo #include "privahdi.h"
33 1.1 leo #include <ctype.h>
34 1.1 leo #include <errno.h>
35 1.1 leo #include <stdio.h>
36 1.1 leo #include <stdlib.h>
37 1.1 leo #include <strings.h>
38 1.1 leo
39 1.1 leo /*
40 1.1 leo * I think we can safely assume a fixed blocksize - AHDI won't support
41 1.1 leo * something different...
42 1.1 leo */
43 1.1 leo #define BLPM ((1024 * 1024) / DEV_BSIZE)
44 1.1 leo #define UNITS_SECTORS 0
45 1.1 leo #define UNITS_CTS 1
46 1.3 jdc #define PART_ROOT 0
47 1.3 jdc #define PART_START 1
48 1.3 jdc #define PART_END 2
49 1.1 leo
50 1.1 leo int main (int, char*[]);
51 1.1 leo void show_parts (struct ahdi_ptable*, int, int, int);
52 1.3 jdc int get_input (char *, int);
53 1.1 leo char *sec_to_cts (struct ahdi_ptable*, u_int32_t, char *);
54 1.3 jdc u_int32_t read_sector (struct ahdi_ptable*, char *, int, int);
55 1.1 leo void change_part (struct ahdi_ptable*, int, int);
56 1.1 leo
57 1.1 leo int
58 1.1 leo main (argc, argv)
59 1.1 leo int argc;
60 1.1 leo char *argv[];
61 1.1 leo {
62 1.1 leo struct ahdi_ptable ptable;
63 1.1 leo int flags, rv, key, units;
64 1.1 leo
65 1.1 leo if (argc < 2) {
66 1.1 leo fprintf (stderr, "usage: %s raw_disk\n", argv[0]);
67 1.1 leo exit (EXIT_FAILURE);
68 1.1 leo }
69 1.1 leo
70 1.2 jdc flags = AHDI_IGN_CKSUM;
71 1.1 leo while ((rv = ahdi_readlabel(&ptable, argv[1], flags)) != 1) {
72 1.1 leo switch (rv) {
73 1.1 leo case -1:
74 1.1 leo fprintf (stderr,
75 1.1 leo "%s: %s: %s\n", argv[0], argv[1],
76 1.1 leo strerror (errno));
77 1.1 leo exit (EXIT_FAILURE);
78 1.1 leo break;
79 1.1 leo case -2:
80 1.1 leo fprintf (stderr,
81 1.1 leo "%s: disk not 512 bytes/sector\n", argv[0]);
82 1.1 leo exit (EXIT_FAILURE);
83 1.1 leo break;
84 1.1 leo case -3:
85 1.3 jdc (void) fpurge(stdin);
86 1.1 leo printf ("No AHDI partitions found. Continue (y/N)?");
87 1.1 leo if (toupper(getchar()) == 'Y') {
88 1.1 leo flags |= FORCE_AHDI;
89 1.1 leo } else
90 1.1 leo exit (EXIT_FAILURE);
91 1.1 leo break;
92 1.1 leo case -4:
93 1.1 leo case -5:
94 1.1 leo case -6:
95 1.3 jdc (void) fpurge(stdin);
96 1.1 leo printf ("Errors reading AHDI partition table. Override (y/N)? ");
97 1.1 leo if (toupper(getchar()) == 'Y') {
98 1.1 leo flags |= AHDI_IGN_EXISTS | AHDI_IGN_EXT |
99 1.2 jdc AHDI_IGN_SPU;
100 1.1 leo } else
101 1.1 leo exit (EXIT_FAILURE);
102 1.1 leo break;
103 1.1 leo case 1:
104 1.1 leo /* Everything is OK */
105 1.1 leo break;
106 1.1 leo default:
107 1.1 leo exit (EXIT_FAILURE);
108 1.1 leo break;
109 1.1 leo }
110 1.1 leo }
111 1.1 leo
112 1.1 leo units = UNITS_SECTORS;
113 1.3 jdc flags = AHDI_KEEP_BOOT | AHDI_KEEP_BSL;
114 1.1 leo show_parts (&ptable, 0, ptable.nparts, units);
115 1.3 jdc printf ("Preserve boot sector - ");
116 1.3 jdc flags & AHDI_KEEP_BOOT ? printf ("yes\n") :
117 1.3 jdc printf ("no\n");
118 1.3 jdc printf ("Preserve bad sector list - ");
119 1.3 jdc flags & AHDI_KEEP_BSL ? printf ("yes\n") :
120 1.3 jdc printf ("no\n");
121 1.1 leo key = 0;
122 1.1 leo while (key != 'Q') {
123 1.1 leo (void) fpurge(stdin);
124 1.3 jdc printf ("Change [a-p], r)ecalculate, s)how, u)nits, w)rite, z)ero or q)uit ");
125 1.1 leo key = toupper(getchar());
126 1.1 leo if (key == EOF)
127 1.1 leo key = 'Q';
128 1.3 jdc if (key >= 'A' && key <= 'P')
129 1.1 leo change_part (&ptable, key - 'A', units);
130 1.1 leo if (key == 'R') {
131 1.3 jdc if (ahdi_buildlabel (&ptable)) {
132 1.1 leo printf ("Partiton table adjusted\n");
133 1.3 jdc } else {
134 1.3 jdc printf ("No changes necessary\n");
135 1.3 jdc }
136 1.1 leo }
137 1.1 leo if (key == 'S') {
138 1.1 leo show_parts (&ptable, 0, ptable.nparts, units);
139 1.3 jdc printf ("Preserve boot sector - ");
140 1.3 jdc flags & AHDI_KEEP_BOOT ? printf ("yes\n") :
141 1.3 jdc printf ("no\n");
142 1.3 jdc printf ("Preserve bad sector list - ");
143 1.3 jdc flags & AHDI_KEEP_BSL ? printf ("yes\n") :
144 1.3 jdc printf ("no\n");
145 1.1 leo }
146 1.1 leo if (key == 'U') {
147 1.3 jdc if (units == UNITS_SECTORS) {
148 1.3 jdc printf ("Units now cylinder/track/sector\n");
149 1.1 leo units = UNITS_CTS;
150 1.3 jdc } else {
151 1.3 jdc printf ("Units now sector\n");
152 1.1 leo units = UNITS_SECTORS;
153 1.3 jdc }
154 1.1 leo }
155 1.1 leo if (key == 'W') {
156 1.3 jdc if ((rv = ahdi_writelabel (&ptable, argv[1], flags)) < 0) {
157 1.1 leo if (rv == -1)
158 1.1 leo perror ("\0");
159 1.1 leo if (rv == -2)
160 1.1 leo printf ("Invalid number of partitions!\n");
161 1.1 leo if (rv == -3)
162 1.1 leo printf ("GEM partition should be BGM or BGM partition should be GEM!\n");
163 1.1 leo if (rv == -4)
164 1.1 leo printf ("Partition overlaps root sector or bad sector list (starts before sector 2)!\n");
165 1.1 leo if (rv == -5)
166 1.1 leo printf ("Partition extends past end of disk!\n");
167 1.1 leo if (rv == -6)
168 1.1 leo printf ("Partitions overlap!\n");
169 1.1 leo if (rv == -7)
170 1.4 wiz printf ("Partition overlaps auxiliary root!\n");
171 1.1 leo if (rv == -8)
172 1.1 leo printf ("More than 4 partitions in root sector!\n");
173 1.1 leo if (rv == -9)
174 1.1 leo printf ("More than 1 partition in an auxiliary root!\n");
175 1.1 leo if (rv < -1 && ahdi_errp1 != -1)
176 1.1 leo printf ("\tpartition %c has errors.\n",
177 1.1 leo ahdi_errp1 + 'a');
178 1.1 leo if (rv < -1 && ahdi_errp2 != -1)
179 1.1 leo printf ("\tpartition %c has errors.\n",
180 1.1 leo ahdi_errp2 + 'a');
181 1.1 leo }
182 1.1 leo }
183 1.3 jdc if (key == 'Z') {
184 1.3 jdc (void) fpurge(stdin);
185 1.3 jdc printf ("Preserve boot sector? ");
186 1.3 jdc if (flags & AHDI_KEEP_BOOT) {
187 1.3 jdc printf ("[y] ");
188 1.3 jdc if (toupper (getchar ()) == 'N')
189 1.3 jdc flags &= ~AHDI_KEEP_BOOT;
190 1.3 jdc } else {
191 1.3 jdc printf ("[n] ");
192 1.3 jdc if (toupper (getchar ()) == 'Y')
193 1.3 jdc flags |= AHDI_KEEP_BOOT;
194 1.3 jdc }
195 1.3 jdc (void) fpurge(stdin);
196 1.3 jdc printf ("Preserve bad sector list? ");
197 1.3 jdc if (flags & AHDI_KEEP_BSL) {
198 1.3 jdc printf ("[y] ");
199 1.3 jdc if (toupper (getchar ()) == 'N')
200 1.3 jdc flags &= ~AHDI_KEEP_BSL;
201 1.3 jdc } else {
202 1.3 jdc printf ("[n] ");
203 1.3 jdc if (toupper (getchar ()) == 'Y')
204 1.3 jdc flags |= AHDI_KEEP_BSL;
205 1.3 jdc }
206 1.3 jdc }
207 1.1 leo }
208 1.1 leo return (0);
209 1.1 leo }
210 1.1 leo
211 1.1 leo void
212 1.1 leo show_parts (ptable, start, finish, units)
213 1.1 leo struct ahdi_ptable *ptable;
214 1.1 leo int start, finish, units;
215 1.1 leo {
216 1.1 leo int i;
217 1.1 leo
218 1.1 leo printf ("Disk information :\n");
219 1.1 leo printf (" sectors/track: %d\n", ptable->nsectors);
220 1.1 leo printf (" tracks/cylinder: %d\n", ptable->ntracks);
221 1.1 leo printf (" sectors/cylinder: %d\n", ptable->secpercyl);
222 1.1 leo printf (" cylinders: %d\n", ptable->ncylinders);
223 1.1 leo printf (" total sectors: %d\n", ptable->secperunit);
224 1.1 leo
225 1.1 leo if (units == UNITS_SECTORS) {
226 1.1 leo printf (" # id root start end size MBs\n");
227 1.1 leo for (i = start; i < finish; i++) {
228 1.3 jdc if (finish - start > 10 && i - start == 8) {
229 1.3 jdc (void) fpurge(stdin);
230 1.3 jdc printf ("-- Press return for more -- ");
231 1.3 jdc (void) getchar();
232 1.3 jdc }
233 1.1 leo printf (" %c %c%c%c %8u %8u %8u %8u (%4u)\n",
234 1.1 leo i + 'a', ptable->parts[i].id[0],
235 1.1 leo ptable->parts[i].id[1], ptable->parts[i].id[2],
236 1.1 leo ptable->parts[i].root, ptable->parts[i].start,
237 1.1 leo ptable->parts[i].start +
238 1.1 leo (ptable->parts[i].size ?
239 1.1 leo ptable->parts[i].size - 1 : 0),
240 1.1 leo ptable->parts[i].size,
241 1.1 leo (ptable->parts[i].size + (BLPM >> 1)) / BLPM);
242 1.1 leo }
243 1.1 leo } else {
244 1.1 leo u_int32_t cylinder, track, sector;
245 1.1 leo printf (" # id root start end size MBs\n");
246 1.1 leo for (i = start; i < finish; i++) {
247 1.3 jdc if (finish - start > 10 && i - start == 8) {
248 1.3 jdc (void) fpurge(stdin);
249 1.3 jdc printf ("-- Press return for more -- ");
250 1.3 jdc (void) getchar();
251 1.3 jdc }
252 1.1 leo printf (" %c %c%c%c ", i + 'a',
253 1.1 leo ptable->parts[i].id[0], ptable->parts[i].id[1],
254 1.1 leo ptable->parts[i].id[2]);
255 1.1 leo sector = ptable->parts[i].root;
256 1.1 leo cylinder = sector / ptable->secpercyl;
257 1.1 leo sector -= cylinder * ptable->secpercyl;
258 1.1 leo track = sector / ptable->nsectors;
259 1.1 leo sector -= track * ptable->nsectors;
260 1.1 leo printf ("%5u/%2u/%3u ", cylinder, track, sector);
261 1.1 leo sector = ptable->parts[i].start;
262 1.1 leo cylinder = sector / ptable->secpercyl;
263 1.1 leo sector -= cylinder * ptable->secpercyl;
264 1.1 leo track = sector / ptable->nsectors;
265 1.1 leo sector -= track * ptable->nsectors;
266 1.1 leo printf ("%5u/%2u/%3u ", cylinder, track, sector);
267 1.1 leo sector = ptable->parts[i].start +
268 1.1 leo (ptable->parts[i].size ?
269 1.1 leo ptable->parts[i].size - 1 : 0),
270 1.1 leo cylinder = sector / ptable->secpercyl;
271 1.1 leo sector -= cylinder * ptable->secpercyl;
272 1.1 leo track = sector / ptable->nsectors;
273 1.1 leo sector -= track * ptable->nsectors;
274 1.1 leo printf ("%5u/%2u/%3u ", cylinder, track, sector);
275 1.1 leo sector = ptable->parts[i].size;
276 1.1 leo cylinder = sector / ptable->secpercyl;
277 1.1 leo sector -= cylinder * ptable->secpercyl;
278 1.1 leo track = sector / ptable->nsectors;
279 1.1 leo sector -= track * ptable->nsectors;
280 1.1 leo printf ("%5u/%2u/%3u ", cylinder, track, sector);
281 1.1 leo printf ("(%4u)\n",
282 1.1 leo (ptable->parts[i].size + (BLPM >> 1)) / BLPM);
283 1.1 leo }
284 1.1 leo }
285 1.1 leo }
286 1.1 leo
287 1.3 jdc int
288 1.1 leo get_input (buf, len)
289 1.1 leo char *buf;
290 1.1 leo int len;
291 1.1 leo {
292 1.1 leo int count, key;
293 1.1 leo
294 1.1 leo count = 0;
295 1.1 leo (void) fpurge(stdin);
296 1.3 jdc while (count < (len - 1) && (key = getchar()) != '\n' && key != '\r') {
297 1.1 leo buf[count] = key;
298 1.1 leo count++;
299 1.1 leo }
300 1.1 leo buf[count] = '\0';
301 1.3 jdc return(count);
302 1.1 leo }
303 1.1 leo
304 1.1 leo char *
305 1.1 leo sec_to_cts (ptable, sector, cts)
306 1.1 leo struct ahdi_ptable *ptable;
307 1.1 leo u_int32_t sector;
308 1.1 leo char *cts;
309 1.1 leo {
310 1.1 leo u_int32_t cylinder, track;
311 1.1 leo
312 1.1 leo cylinder = sector / ptable->secpercyl;
313 1.1 leo sector -= cylinder * ptable->secpercyl;
314 1.1 leo track = sector / ptable->nsectors;
315 1.1 leo sector -= track * ptable->nsectors;
316 1.1 leo sprintf (cts, "%u/%u/%u", cylinder, track, sector);
317 1.1 leo return (cts);
318 1.1 leo }
319 1.1 leo
320 1.1 leo u_int32_t
321 1.3 jdc read_sector (ptable, buf, part, se)
322 1.1 leo struct ahdi_ptable *ptable;
323 1.3 jdc char *buf;
324 1.3 jdc int part, se;
325 1.1 leo {
326 1.1 leo u_int32_t sector, track, cylinder;
327 1.3 jdc int i;
328 1.1 leo
329 1.1 leo sector = track = cylinder = 0;
330 1.1 leo if ((strchr (buf, '/') != NULL) &&
331 1.1 leo ((sscanf (buf, "%u/%u/%u", &cylinder, &track, §or) == 3) ||
332 1.1 leo (sscanf (buf, "%u/%u/", &cylinder, &track) == 2) ||
333 1.1 leo (sscanf (buf, "%u/", &cylinder) == 1))) {
334 1.1 leo if (sector > ptable->nsectors || track > ptable->ntracks ||
335 1.1 leo cylinder > ptable->ncylinders)
336 1.1 leo return (0);
337 1.1 leo sector += ptable->nsectors * track;
338 1.1 leo sector += ptable->secpercyl * cylinder;
339 1.1 leo return (sector);
340 1.1 leo }
341 1.3 jdc if (buf[0] == '-' && buf[1]) {
342 1.3 jdc if (buf[1] == '1' && se == PART_END)
343 1.3 jdc /* Extend to end of disk */
344 1.3 jdc return (ptable->secperunit -
345 1.3 jdc ptable->parts[part].start);
346 1.5 he i = (int) (toupper ((unsigned char)(buf[1]) - 'A'));
347 1.3 jdc if (i >= 0 && i <= ptable->nparts ) {
348 1.3 jdc if (se == PART_ROOT && part > i)
349 1.3 jdc /* Root after partition ... */
350 1.3 jdc return (ptable->parts[i].start +
351 1.3 jdc ptable->parts[i].size);
352 1.3 jdc if (se == PART_START && part > i) {
353 1.3 jdc /* Start after partition ... */
354 1.3 jdc if (ptable->parts[part].root)
355 1.3 jdc return (ptable->parts[i].start +
356 1.3 jdc ptable->parts[i].size + 1);
357 1.3 jdc else
358 1.3 jdc return (ptable->parts[i].start +
359 1.3 jdc ptable->parts[i].size);
360 1.3 jdc }
361 1.3 jdc if (se == PART_END && part < i)
362 1.3 jdc /* End before partition ... */
363 1.3 jdc return (ptable->parts[i].root -
364 1.3 jdc ptable->parts[part].start);
365 1.3 jdc }
366 1.3 jdc return (0);
367 1.3 jdc }
368 1.3 jdc if (sscanf (buf, "%u", §or) == 1) {
369 1.3 jdc if (buf[strlen (buf) - 1] == 'm' ||
370 1.3 jdc buf[strlen (buf) - 1] == 'M')
371 1.3 jdc sector *= BLPM;
372 1.1 leo return (sector);
373 1.3 jdc }
374 1.1 leo return (0);
375 1.1 leo }
376 1.1 leo
377 1.1 leo void
378 1.1 leo change_part (ptable, part, units)
379 1.1 leo struct ahdi_ptable *ptable;
380 1.1 leo int part, units;
381 1.1 leo {
382 1.1 leo #define BUFLEN 20
383 1.1 leo #define CTSLEN 64
384 1.1 leo char buf[BUFLEN], cts[CTSLEN];
385 1.1 leo u_int32_t sector;
386 1.1 leo
387 1.1 leo if (part > ptable->nparts) {
388 1.1 leo part = ptable->nparts;
389 1.1 leo printf ("Changing partition %c!\n", part + 'a');
390 1.1 leo ptable->nparts++;
391 1.1 leo }
392 1.1 leo if (part == ptable->nparts)
393 1.1 leo ptable->nparts++;
394 1.1 leo show_parts (ptable, part, part + 1, units);
395 1.1 leo
396 1.1 leo printf ("id [%c%c%c] ", ptable->parts[part].id[0],
397 1.1 leo ptable->parts[part].id[1], ptable->parts[part].id[2]);
398 1.3 jdc if (get_input (&buf[0], BUFLEN) > 2) {
399 1.1 leo ptable->parts[part].id[0] = buf[0];
400 1.1 leo ptable->parts[part].id[1] = buf[1];
401 1.1 leo ptable->parts[part].id[2] = buf[2];
402 1.1 leo }
403 1.1 leo
404 1.1 leo printf ("root [%8u (%s)] ", ptable->parts[part].root,
405 1.1 leo sec_to_cts (ptable, ptable->parts[part].root, &cts[0]));
406 1.3 jdc if (get_input (&buf[0], BUFLEN)) {
407 1.3 jdc sector = read_sector (ptable, buf, part, PART_ROOT);
408 1.3 jdc ptable->parts[part].root = sector;
409 1.1 leo }
410 1.1 leo
411 1.1 leo printf ("start [%8u (%s)] ", ptable->parts[part].start,
412 1.1 leo sec_to_cts (ptable, ptable->parts[part].start, &cts[0]));
413 1.3 jdc if (get_input (&buf[0], BUFLEN)) {
414 1.3 jdc sector = read_sector (ptable, buf, part, PART_START);
415 1.3 jdc ptable->parts[part].start = sector;
416 1.1 leo }
417 1.1 leo
418 1.3 jdc printf ("size [%8u (%s) (%4uM)] ", ptable->parts[part].size,
419 1.3 jdc sec_to_cts (ptable, ptable->parts[part].size, &cts[0]),
420 1.3 jdc (ptable->parts[part].size + (BLPM >> 1)) / BLPM);
421 1.3 jdc if (get_input (&buf[0], BUFLEN)) {
422 1.3 jdc sector = read_sector (ptable, buf, part, PART_END);
423 1.3 jdc ptable->parts[part].size = sector;
424 1.1 leo }
425 1.1 leo
426 1.1 leo /*
427 1.1 leo printf ("NetBSD disk letter [%c] ", ptable->parts[part].letter + 'a');
428 1.3 jdc if (get_input (&buf[0], BUFLEN)) {
429 1.3 jdc buf[0] = tolower(buf[0]);
430 1.1 leo if (buf[0] == 'a' || (buf[0] >= 'd' && buf[0] <= 'p'))
431 1.1 leo ptable->parts[part].letter = buf[0] - 'a';
432 1.1 leo */
433 1.1 leo
434 1.1 leo if (!ptable->parts[part].start && !ptable->parts[part].size) {
435 1.1 leo if (part == ptable->nparts - 1)
436 1.1 leo ptable->nparts--;
437 1.1 leo }
438 1.1 leo }
439