1 1.11 chs /* $NetBSD: edahdi.c,v 1.11 2011/10/01 15:59:00 chs Exp $ */ 2 1.1 leo 3 1.1 leo /* 4 1.1 leo * Copyright (c) 1996 Leo Weppelman, Waldi Ravens. 5 1.1 leo * All rights reserved. 6 1.1 leo * 7 1.1 leo * Redistribution and use in source and binary forms, with or without 8 1.1 leo * modification, are permitted provided that the following conditions 9 1.1 leo * are met: 10 1.1 leo * 1. Redistributions of source code must retain the above copyright 11 1.1 leo * notice, this list of conditions and the following disclaimer. 12 1.1 leo * 2. Redistributions in binary form must reproduce the above copyright 13 1.1 leo * notice, this list of conditions and the following disclaimer in the 14 1.1 leo * documentation and/or other materials provided with the distribution. 15 1.2 leo * 3. All advertising materials mentioning features or use of this software 16 1.1 leo * must display the following acknowledgement: 17 1.1 leo * This product includes software developed by 18 1.1 leo * Leo Weppelman and Waldi Ravens. 19 1.2 leo * 4. The name of the author may not be used to endorse or promote products 20 1.2 leo * derived from this software without specific prior written permission. 21 1.1 leo * 22 1.1 leo * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR 23 1.1 leo * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES 24 1.1 leo * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. 25 1.1 leo * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT, 26 1.1 leo * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT 27 1.1 leo * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, 28 1.1 leo * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY 29 1.1 leo * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT 30 1.1 leo * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF 31 1.1 leo * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. 32 1.1 leo */ 33 1.1 leo 34 1.1 leo /* 35 1.1 leo * This code implements a simple editor for partition id's on disks containing 36 1.1 leo * AHDI partition info. 37 1.1 leo * 38 1.1 leo * Credits for the code handling disklabels goes to Waldi Ravens. 39 1.1 leo * 40 1.1 leo */ 41 1.1 leo #include <sys/types.h> 42 1.1 leo #include <sys/param.h> 43 1.1 leo #include <sys/stat.h> 44 1.1 leo #include <sys/disklabel.h> 45 1.1 leo 46 1.1 leo #include <machine/ahdilabel.h> 47 1.1 leo 48 1.1 leo #include <fcntl.h> 49 1.1 leo #include <stdlib.h> 50 1.10 roy #include <term.h> 51 1.1 leo #include <termios.h> 52 1.1 leo #include <unistd.h> 53 1.1 leo #include <stdio.h> 54 1.1 leo #include <string.h> 55 1.1 leo #include <err.h> 56 1.1 leo #include <ctype.h> 57 1.1 leo 58 1.1 leo /* 59 1.1 leo * Internal partition tables: 60 1.1 leo */ 61 1.1 leo typedef struct { 62 1.1 leo char id[4]; 63 1.1 leo u_int start; 64 1.1 leo u_int end; 65 1.1 leo u_int rsec; 66 1.1 leo u_int rent; 67 1.1 leo int mod; 68 1.1 leo } part_t; 69 1.1 leo 70 1.1 leo typedef struct { 71 1.1 leo int nparts; 72 1.1 leo part_t *parts; 73 1.1 leo } ptable_t; 74 1.1 leo 75 1.1 leo /* 76 1.1 leo * I think we can savely assume a fixed blocksize - AHDI won't support 77 1.1 leo * something different... 78 1.1 leo */ 79 1.1 leo #define BLPM ((1024 * 1024) / DEV_BSIZE) 80 1.1 leo 81 1.1 leo /* 82 1.1 leo * #Partition entries shown on the screen at once 83 1.1 leo */ 84 1.1 leo #define MAX_PSHOWN 16 /* #partitions shown on screen */ 85 1.1 leo 86 1.1 leo /* 87 1.1 leo * Tokens: 88 1.1 leo */ 89 1.1 leo #define T_INVAL 0 90 1.1 leo #define T_QUIT 1 91 1.1 leo #define T_WRITE 2 92 1.1 leo #define T_NEXT 3 93 1.1 leo #define T_PREV 4 94 1.1 leo #define T_NUMBER 5 95 1.1 leo #define T_EOF 6 96 1.1 leo 97 1.6 dsl void ahdi_cksum(void *); 98 1.6 dsl u_int ahdi_getparts(int, ptable_t *, u_int, u_int); 99 1.6 dsl int bsd_label(int, u_int); 100 1.6 dsl int dkcksum(struct disklabel *); 101 1.6 dsl int edit_parts(int, ptable_t *); 102 1.6 dsl void *disk_read(int, u_int, u_int); 103 1.6 dsl void disk_write(int, u_int, u_int, void *); 104 1.6 dsl char *get_id(void); 105 1.6 dsl int lex(int *); 106 1.6 dsl int show_parts(ptable_t *, int); 107 1.6 dsl void update_disk(ptable_t *, int, int); 108 1.1 leo 109 1.1 leo int 110 1.8 dsl main(int argc, char *argv[]) 111 1.1 leo { 112 1.1 leo int fd; 113 1.1 leo ptable_t ptable; 114 1.1 leo int rv; 115 1.1 leo struct stat st; 116 1.1 leo 117 1.1 leo if (argc != 2) { 118 1.1 leo char *prog = strrchr(argv[0], '/'); 119 1.1 leo 120 1.1 leo if (prog == NULL) 121 1.1 leo prog = argv[0]; 122 1.1 leo else prog++; 123 1.1 leo fprintf(stderr, "Usage: %s <raw_disk_device>", prog); 124 1.1 leo exit(1); 125 1.1 leo } 126 1.1 leo if ((fd = open(argv[1], O_RDWR)) < 0) 127 1.1 leo err(1, "Cannot open '%s'.", argv[1]); 128 1.1 leo if (fstat(fd, &st) < 0) 129 1.1 leo err(1, "Cannot stat '%s'.", argv[1]); 130 1.1 leo if (!S_ISCHR(st.st_mode)) 131 1.1 leo errx(1, "'%s' must be a character special device.", argv[1]); 132 1.1 leo 133 1.1 leo if ((rv = bsd_label(fd, LABELSECTOR)) < 0) 134 1.1 leo errx(1, "I/O error"); 135 1.1 leo if (rv == 0) { 136 1.1 leo warnx("Disk has no ahdi partitions"); 137 1.1 leo return (2); 138 1.1 leo } 139 1.1 leo 140 1.10 roy setupterm(NULL, STDOUT_FILENO, NULL); 141 1.1 leo 142 1.1 leo ptable.nparts = 0; 143 1.1 leo ptable.parts = NULL; 144 1.1 leo 145 1.1 leo if ((ahdi_getparts(fd, &ptable, AHDI_BBLOCK, AHDI_BBLOCK) != 0) 146 1.1 leo || (ptable.nparts == 0)) 147 1.1 leo exit (1); 148 1.1 leo 149 1.1 leo edit_parts(fd, &ptable); 150 1.1 leo return (0); 151 1.1 leo } 152 1.1 leo 153 1.1 leo int 154 1.7 dsl edit_parts(int fd, ptable_t *ptable) 155 1.1 leo { 156 1.1 leo int scr_base = 0; 157 1.1 leo int value; 158 1.1 leo char *error, *new_id; 159 1.1 leo 160 1.1 leo for (;;) { 161 1.1 leo error = NULL; 162 1.10 roy if (clear_screen) 163 1.10 roy tputs(clear_screen, 1, putchar); 164 1.1 leo show_parts(ptable, scr_base); 165 1.1 leo 166 1.1 leo printf("\n\n"); 167 1.1 leo printf("q : quit - don't update the disk\n"); 168 1.1 leo printf("w : write changes to disk\n"); 169 1.1 leo printf("> : next screen of partitions\n"); 170 1.1 leo printf("< : previous screen of partitions\n"); 171 1.1 leo printf("<nr> : modify id of partition <nr>\n"); 172 1.1 leo printf("\n\nCommand? "); 173 1.1 leo fflush(stdout); 174 1.1 leo 175 1.1 leo switch (lex(&value)) { 176 1.1 leo case T_EOF: 177 1.1 leo exit(0); 178 1.1 leo 179 1.1 leo case T_INVAL: 180 1.1 leo error = "Invalid command"; 181 1.1 leo break; 182 1.1 leo case T_QUIT : 183 1.1 leo for (value = 0; value < ptable->nparts; value++) { 184 1.1 leo if (ptable->parts[value].mod) { 185 1.1 leo printf("\nThere are unwritten changes." 186 1.1 leo " Quit anyway? [n] "); 187 1.1 leo value = getchar(); 188 1.1 leo if ((value == 'y') || (value == 'Y')) 189 1.1 leo exit (0); 190 1.1 leo while (value != '\n') 191 1.1 leo value = getchar(); 192 1.1 leo break; 193 1.1 leo } 194 1.1 leo } 195 1.1 leo if (value == ptable->nparts) 196 1.1 leo exit(0); 197 1.1 leo break; 198 1.1 leo case T_WRITE: 199 1.1 leo error = "No changes to write"; 200 1.1 leo for (value = 0; value < ptable->nparts; value++) { 201 1.1 leo if (ptable->parts[value].mod) { 202 1.1 leo update_disk(ptable, fd, value); 203 1.1 leo error = ""; 204 1.1 leo } 205 1.1 leo } 206 1.1 leo break; 207 1.1 leo case T_NEXT : 208 1.1 leo if ((scr_base + MAX_PSHOWN) < ptable->nparts) 209 1.1 leo scr_base += MAX_PSHOWN; 210 1.1 leo break; 211 1.1 leo case T_PREV : 212 1.1 leo scr_base -= MAX_PSHOWN; 213 1.1 leo if (scr_base < 0) 214 1.1 leo scr_base = 0; 215 1.1 leo break; 216 1.1 leo case T_NUMBER: 217 1.1 leo if (value >= ptable->nparts) { 218 1.1 leo error = "Not that many partitions"; 219 1.1 leo break; 220 1.1 leo } 221 1.1 leo if ((new_id = get_id()) == NULL) { 222 1.1 leo error = "Invalid id"; 223 1.1 leo break; 224 1.1 leo } 225 1.1 leo strncpy(ptable->parts[value].id, new_id, 3); 226 1.1 leo ptable->parts[value].mod = 1; 227 1.1 leo scr_base = (value / MAX_PSHOWN) * MAX_PSHOWN; 228 1.1 leo break; 229 1.1 leo default : 230 1.1 leo error = "Internal error - unknown token"; 231 1.1 leo break; 232 1.1 leo } 233 1.1 leo if (error != NULL) { 234 1.1 leo printf("\n\n%s", error); 235 1.1 leo fflush(stdout); 236 1.1 leo sleep(2); 237 1.1 leo } 238 1.1 leo } 239 1.1 leo } 240 1.1 leo 241 1.1 leo int 242 1.7 dsl show_parts(ptable_t *ptable, int nr) 243 1.1 leo { 244 1.1 leo int i; 245 1.1 leo part_t *p; 246 1.1 leo u_int megs; 247 1.1 leo 248 1.1 leo if (nr >= ptable->nparts) 249 1.1 leo return (0); /* Nothing to show */ 250 1.1 leo printf("\n\n"); 251 1.1 leo printf("nr root desc id start end MBs\n"); 252 1.1 leo 253 1.1 leo p = &ptable->parts[nr]; 254 1.1 leo i = nr; 255 1.1 leo for(; (i < ptable->nparts) && ((i - nr) < MAX_PSHOWN); i++, p++) { 256 1.1 leo megs = ((p->end - p->start + 1) + (BLPM >> 1)) / BLPM; 257 1.1 leo printf("%2d%s %8u %4u %s %8u %8u (%3u)\n", i, 258 1.1 leo p->mod ? "*" : " ", 259 1.1 leo p->rsec, p->rent, p->id, p->start, p->end, megs); 260 1.1 leo } 261 1.1 leo return (1); 262 1.1 leo } 263 1.1 leo 264 1.1 leo int 265 1.7 dsl lex(int *value) 266 1.1 leo { 267 1.1 leo char c[1]; 268 1.1 leo int rv, nch; 269 1.1 leo 270 1.1 leo rv = T_INVAL; 271 1.1 leo 272 1.1 leo *value = 0; 273 1.1 leo for (;;) { 274 1.1 leo if ((nch = read (0, c, 1)) != 1) { 275 1.1 leo if (nch == 0) 276 1.1 leo return (T_EOF); 277 1.1 leo else return (rv); 278 1.1 leo } 279 1.1 leo switch (*c) { 280 1.1 leo case 'q': 281 1.1 leo rv = T_QUIT; 282 1.1 leo goto out; 283 1.1 leo case 'w': 284 1.1 leo rv = T_WRITE; 285 1.1 leo goto out; 286 1.1 leo case '>': 287 1.1 leo rv = T_NEXT; 288 1.1 leo goto out; 289 1.1 leo case '<': 290 1.1 leo rv = T_PREV; 291 1.1 leo goto out; 292 1.1 leo default : 293 1.4 he if (isspace((unsigned char)*c)) { 294 1.1 leo if (rv == T_INVAL) 295 1.1 leo break; 296 1.1 leo goto out; 297 1.1 leo } 298 1.4 he else if (isdigit((unsigned char)*c)) { 299 1.1 leo *value = (10 * *value) + *c - '0'; 300 1.1 leo rv = T_NUMBER; 301 1.1 leo } 302 1.1 leo goto out; 303 1.1 leo } 304 1.1 leo } 305 1.1 leo /* NOTREACHED */ 306 1.1 leo out: 307 1.1 leo /* 308 1.1 leo * Flush rest of line before returning 309 1.1 leo */ 310 1.1 leo while (read (0, c, 1) == 1) 311 1.1 leo if ((*c == '\n') || (*c == '\r')) 312 1.1 leo break; 313 1.1 leo return (rv); 314 1.1 leo } 315 1.1 leo 316 1.1 leo char * 317 1.9 cegger get_id(void) 318 1.1 leo { 319 1.1 leo static char buf[5]; 320 1.1 leo int n; 321 1.1 leo printf("\nEnter new id: "); 322 1.1 leo if (fgets(buf, sizeof(buf), stdin) == NULL) 323 1.1 leo return (NULL); 324 1.1 leo for (n = 0; n < 3; n++) { 325 1.4 he if (!isalpha((unsigned char)buf[n])) 326 1.1 leo return (NULL); 327 1.4 he buf[n] = toupper((unsigned char)buf[n]); 328 1.1 leo } 329 1.1 leo buf[3] = '\0'; 330 1.1 leo return (buf); 331 1.1 leo } 332 1.1 leo 333 1.1 leo int 334 1.7 dsl bsd_label(int fd, u_int offset) 335 1.1 leo { 336 1.1 leo u_char *bblk; 337 1.1 leo u_int nsec; 338 1.1 leo int rv; 339 1.1 leo 340 1.1 leo nsec = (BBMINSIZE + (DEV_BSIZE - 1)) / DEV_BSIZE; 341 1.1 leo bblk = disk_read(fd, offset, nsec); 342 1.1 leo if (bblk) { 343 1.1 leo u_int *end, *p; 344 1.1 leo 345 1.1 leo end = (u_int *)&bblk[BBMINSIZE - sizeof(struct disklabel)]; 346 1.1 leo rv = 1; 347 1.1 leo for (p = (u_int *)bblk; p < end; ++p) { 348 1.1 leo struct disklabel *dl = (struct disklabel *)&p[1]; 349 1.1 leo if ( ( (p[0] == NBDAMAGIC && offset == 0) 350 1.1 leo || (p[0] == AHDIMAGIC && offset != 0) 351 1.1 leo || (u_char *)dl - bblk == 7168 352 1.1 leo ) 353 1.1 leo && dl->d_npartitions <= MAXPARTITIONS 354 1.1 leo && dl->d_magic2 == DISKMAGIC 355 1.1 leo && dl->d_magic == DISKMAGIC 356 1.1 leo && dkcksum(dl) == 0 357 1.1 leo ) { 358 1.1 leo rv = 0; 359 1.1 leo break; 360 1.1 leo } 361 1.1 leo } 362 1.1 leo free(bblk); 363 1.1 leo } 364 1.1 leo else rv = -1; 365 1.1 leo 366 1.1 leo return(rv); 367 1.1 leo } 368 1.1 leo 369 1.1 leo int 370 1.7 dsl dkcksum(struct disklabel *dl) 371 1.1 leo { 372 1.1 leo u_short *start, *end, sum = 0; 373 1.1 leo 374 1.1 leo start = (u_short *)dl; 375 1.1 leo end = (u_short *)&dl->d_partitions[dl->d_npartitions]; 376 1.1 leo while (start < end) 377 1.1 leo sum ^= *start++; 378 1.1 leo return(sum); 379 1.1 leo } 380 1.1 leo 381 1.1 leo void 382 1.7 dsl ahdi_cksum(void *buf) 383 1.1 leo { 384 1.1 leo unsigned short *p = (unsigned short *)buf; 385 1.1 leo unsigned short csum = 0; 386 1.1 leo int i; 387 1.1 leo 388 1.1 leo p[255] = 0; 389 1.1 leo for(i = 0; i < 256; i++) 390 1.1 leo csum += *p++; 391 1.1 leo *--p = (0x1234 - csum) & 0xffff; 392 1.1 leo } 393 1.1 leo 394 1.1 leo 395 1.1 leo u_int 396 1.1 leo ahdi_getparts(fd, ptable, rsec, esec) 397 1.1 leo int fd; 398 1.1 leo ptable_t *ptable; 399 1.1 leo u_int rsec, 400 1.1 leo esec; 401 1.1 leo { 402 1.1 leo struct ahdi_part *part, *end; 403 1.1 leo struct ahdi_root *root; 404 1.1 leo u_int rv; 405 1.1 leo 406 1.1 leo root = disk_read(fd, rsec, 1); 407 1.1 leo if (!root) { 408 1.1 leo rv = rsec + (rsec == 0); 409 1.1 leo goto done; 410 1.1 leo } 411 1.1 leo 412 1.1 leo if (rsec == AHDI_BBLOCK) 413 1.1 leo end = &root->ar_parts[AHDI_MAXRPD]; 414 1.1 leo else end = &root->ar_parts[AHDI_MAXARPD]; 415 1.1 leo for (part = root->ar_parts; part < end; ++part) { 416 1.11 chs u_int id; 417 1.11 chs 418 1.11 chs memcpy(&id, &part->ap_flg, sizeof (id)); 419 1.1 leo if (!(id & 0x01000000)) 420 1.1 leo continue; 421 1.1 leo if ((id &= 0x00ffffff) == AHDI_PID_XGM) { 422 1.1 leo u_int offs = part->ap_st + esec; 423 1.1 leo rv = ahdi_getparts(fd, ptable, offs, 424 1.1 leo esec == AHDI_BBLOCK ? offs : esec); 425 1.1 leo if (rv) 426 1.1 leo goto done; 427 1.1 leo } else { 428 1.1 leo part_t *p; 429 1.1 leo u_int i = ++ptable->nparts; 430 1.1 leo ptable->parts = realloc(ptable->parts, 431 1.1 leo i * sizeof *ptable->parts); 432 1.1 leo if (ptable->parts == NULL) { 433 1.1 leo fprintf(stderr, "Allocation error\n"); 434 1.1 leo rv = 1; 435 1.1 leo goto done; 436 1.1 leo } 437 1.1 leo p = &ptable->parts[--i]; 438 1.11 chs memcpy(&p->id, &id, sizeof (id)); 439 1.1 leo p->start = part->ap_st + rsec; 440 1.1 leo p->end = p->start + part->ap_size - 1; 441 1.1 leo p->rsec = rsec; 442 1.1 leo p->rent = part - root->ar_parts; 443 1.1 leo p->mod = 0; 444 1.1 leo } 445 1.1 leo } 446 1.1 leo rv = 0; 447 1.1 leo done: 448 1.1 leo free(root); 449 1.1 leo return(rv); 450 1.1 leo } 451 1.1 leo 452 1.1 leo void * 453 1.1 leo disk_read(fd, start, count) 454 1.1 leo int fd; 455 1.1 leo u_int start, 456 1.1 leo count; 457 1.1 leo { 458 1.1 leo char *buffer; 459 1.1 leo off_t offset; 460 1.1 leo size_t size; 461 1.1 leo 462 1.1 leo size = count * DEV_BSIZE; 463 1.1 leo offset = start * DEV_BSIZE; 464 1.1 leo if ((buffer = malloc(size)) == NULL) 465 1.1 leo errx(1, "No memory"); 466 1.1 leo 467 1.1 leo if (lseek(fd, offset, SEEK_SET) != offset) { 468 1.1 leo free(buffer); 469 1.1 leo err(1, "Seek error"); 470 1.1 leo } 471 1.1 leo if (read(fd, buffer, size) != size) { 472 1.1 leo free(buffer); 473 1.1 leo err(1, "Read error"); 474 1.1 leo exit(1); 475 1.1 leo } 476 1.1 leo return(buffer); 477 1.1 leo } 478 1.1 leo 479 1.1 leo void 480 1.8 dsl update_disk(ptable_t *ptable, int fd, int pno) 481 1.1 leo { 482 1.1 leo struct ahdi_root *root; 483 1.1 leo struct ahdi_part *apart; 484 1.1 leo part_t *lpart; 485 1.1 leo u_int rsec; 486 1.1 leo int i; 487 1.1 leo 488 1.1 leo rsec = ptable->parts[pno].rsec; 489 1.1 leo root = disk_read(fd, rsec, 1); 490 1.1 leo 491 1.1 leo /* 492 1.1 leo * Look for additional mods on the same sector 493 1.1 leo */ 494 1.1 leo for (i = 0; i < ptable->nparts; i++) { 495 1.1 leo lpart = &ptable->parts[i]; 496 1.1 leo if (lpart->mod && (lpart->rsec == rsec)) { 497 1.1 leo apart = &root->ar_parts[lpart->rent]; 498 1.1 leo 499 1.1 leo /* Paranoia.... */ 500 1.1 leo if ((lpart->end - lpart->start + 1) != apart->ap_size) 501 1.1 leo errx(1, "Updating wrong partition!"); 502 1.1 leo apart->ap_id[0] = lpart->id[0]; 503 1.1 leo apart->ap_id[1] = lpart->id[1]; 504 1.1 leo apart->ap_id[2] = lpart->id[2]; 505 1.1 leo lpart->mod = 0; 506 1.1 leo } 507 1.1 leo } 508 1.1 leo if (rsec == 0) 509 1.1 leo ahdi_cksum(root); 510 1.1 leo disk_write(fd, rsec, 1, root); 511 1.1 leo free(root); 512 1.1 leo } 513 1.1 leo 514 1.1 leo void 515 1.1 leo disk_write(fd, start, count, buf) 516 1.1 leo int fd; 517 1.1 leo u_int start, 518 1.1 leo count; 519 1.1 leo void *buf; 520 1.1 leo { 521 1.1 leo off_t offset; 522 1.1 leo size_t size; 523 1.1 leo 524 1.1 leo size = count * DEV_BSIZE; 525 1.1 leo offset = start * DEV_BSIZE; 526 1.1 leo 527 1.1 leo if (lseek(fd, offset, SEEK_SET) != offset) 528 1.1 leo err(1, "Seek error"); 529 1.1 leo if (write(fd, buf, size) != size) 530 1.1 leo err(1, "Write error"); 531 1.1 leo } 532