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