fdisk.c (145765) | fdisk.c (148062) |
---|---|
1/* 2 * Mach Operating System 3 * Copyright (c) 1992 Carnegie Mellon University 4 * All Rights Reserved. 5 * 6 * Permission to use, copy, modify and distribute this software and its 7 * documentation is hereby granted, provided that both the copyright 8 * notice and this permission notice appear in all copies of the --- 11 unchanged lines hidden (view full) --- 20 * Carnegie Mellon University 21 * Pittsburgh PA 15213-3890 22 * 23 * any improvements or extensions that they make and grant Carnegie Mellon 24 * the rights to redistribute these changes. 25 */ 26 27#include <sys/cdefs.h> | 1/* 2 * Mach Operating System 3 * Copyright (c) 1992 Carnegie Mellon University 4 * All Rights Reserved. 5 * 6 * Permission to use, copy, modify and distribute this software and its 7 * documentation is hereby granted, provided that both the copyright 8 * notice and this permission notice appear in all copies of the --- 11 unchanged lines hidden (view full) --- 20 * Carnegie Mellon University 21 * Pittsburgh PA 15213-3890 22 * 23 * any improvements or extensions that they make and grant Carnegie Mellon 24 * the rights to redistribute these changes. 25 */ 26 27#include <sys/cdefs.h> |
28__FBSDID("$FreeBSD: head/sbin/fdisk_pc98/fdisk.c 145765 2005-05-01 10:08:35Z nyan $"); | 28__FBSDID("$FreeBSD: head/sbin/fdisk_pc98/fdisk.c 148062 2005-07-15 15:31:19Z nyan $"); |
29 30#include <sys/disk.h> 31#include <sys/disklabel.h> 32#include <sys/diskpc98.h> 33#include <sys/param.h> 34#include <sys/stat.h> 35#include <sys/mount.h> 36#include <ctype.h> 37#include <fcntl.h> 38#include <err.h> 39#include <errno.h> | 29 30#include <sys/disk.h> 31#include <sys/disklabel.h> 32#include <sys/diskpc98.h> 33#include <sys/param.h> 34#include <sys/stat.h> 35#include <sys/mount.h> 36#include <ctype.h> 37#include <fcntl.h> 38#include <err.h> 39#include <errno.h> |
40#include <libgeom.h> |
|
40#include <paths.h> 41#include <regex.h> 42#include <stdint.h> 43#include <stdio.h> 44#include <stdlib.h> 45#include <string.h> 46#include <unistd.h> 47 --- 28 unchanged lines hidden (view full) --- 76 unsigned char padding[2]; /* force the longs to be long aligned */ 77 unsigned char bootinst[510]; 78 unsigned short int signature; 79 struct pc98_partition parts[8]; 80 unsigned char large_sector_overflow[MAX_SEC_SIZE-MIN_SEC_SIZE]; 81}; 82 83static struct mboot mboot; | 41#include <paths.h> 42#include <regex.h> 43#include <stdint.h> 44#include <stdio.h> 45#include <stdlib.h> 46#include <string.h> 47#include <unistd.h> 48 --- 28 unchanged lines hidden (view full) --- 77 unsigned char padding[2]; /* force the longs to be long aligned */ 78 unsigned char bootinst[510]; 79 unsigned short int signature; 80 struct pc98_partition parts[8]; 81 unsigned char large_sector_overflow[MAX_SEC_SIZE-MIN_SEC_SIZE]; 82}; 83 84static struct mboot mboot; |
84static int fd, fdw; | 85static int fd; |
85 86#define ACTIVE 0x80 87 88static uint dos_cyls; 89static uint dos_heads; 90static uint dos_sectors; 91static uint dos_cylsecs; 92 --- 62 unchanged lines hidden (view full) --- 155static void print_params(void); 156static void change_active(int which); 157static void change_code(void); 158static void get_params_to_use(void); 159static char *get_rootdisk(void); 160static void dos(u_int32_t start, u_int32_t size, struct pc98_partition *partp); 161static int open_disk(int flag); 162static ssize_t read_disk(off_t sector, void *buf); | 86 87#define ACTIVE 0x80 88 89static uint dos_cyls; 90static uint dos_heads; 91static uint dos_sectors; 92static uint dos_cylsecs; 93 --- 62 unchanged lines hidden (view full) --- 156static void print_params(void); 157static void change_active(int which); 158static void change_code(void); 159static void get_params_to_use(void); 160static char *get_rootdisk(void); 161static void dos(u_int32_t start, u_int32_t size, struct pc98_partition *partp); 162static int open_disk(int flag); 163static ssize_t read_disk(off_t sector, void *buf); |
163static ssize_t write_disk(off_t sector, void *buf); | 164static int write_disk(off_t sector, void *buf); |
164static int get_params(void); 165static int read_s0(void); 166static int write_s0(void); 167static int ok(const char *str); 168static int decimal(const char *str, int *num, int deflt); 169static const char *get_type(int type); 170static void usage(void); 171static int string(const char *str, char **ans); --- 383 unchanged lines hidden (view full) --- 555 partp->dp_ehd = end % dos_cylsecs / dos_sectors; 556 partp->dp_esect = end % dos_sectors; 557} 558 559static int 560open_disk(int flag) 561{ 562 struct stat st; | 165static int get_params(void); 166static int read_s0(void); 167static int write_s0(void); 168static int ok(const char *str); 169static int decimal(const char *str, int *num, int deflt); 170static const char *get_type(int type); 171static void usage(void); 172static int string(const char *str, char **ans); --- 383 unchanged lines hidden (view full) --- 556 partp->dp_ehd = end % dos_cylsecs / dos_sectors; 557 partp->dp_esect = end % dos_sectors; 558} 559 560static int 561open_disk(int flag) 562{ 563 struct stat st; |
563 int rwmode, p; 564 char *s; | 564 int rwmode; |
565 | 565 |
566 fdw = -1; | |
567 if (stat(disk, &st) == -1) { 568 if (errno == ENOENT) 569 return -2; 570 warnx("can't get file status of %s", disk); 571 return -1; 572 } 573 if ( !(st.st_mode & S_IFCHR) ) 574 warnx("device %s is not character special", disk); 575 rwmode = a_flag || B_flag || flag ? O_RDWR : O_RDONLY; 576 fd = open(disk, rwmode); | 566 if (stat(disk, &st) == -1) { 567 if (errno == ENOENT) 568 return -2; 569 warnx("can't get file status of %s", disk); 570 return -1; 571 } 572 if ( !(st.st_mode & S_IFCHR) ) 573 warnx("device %s is not character special", disk); 574 rwmode = a_flag || B_flag || flag ? O_RDWR : O_RDONLY; 575 fd = open(disk, rwmode); |
576 if (fd == -1 && errno == EPERM && rwmode == O_RDWR) 577 fd = open(disk, O_RDONLY); |
|
577 if (fd == -1 && errno == ENXIO) 578 return -2; | 578 if (fd == -1 && errno == ENXIO) 579 return -2; |
579 if (fd == -1 && errno == EPERM && rwmode == O_RDWR) { 580 fd = open(disk, O_RDONLY); 581 if (fd == -1) 582 return -3; 583 for (p = 0; p < NDOSPART; p++) { 584 asprintf(&s, "%ss%d", disk, p + 1); 585 fdw = open(s, rwmode); 586 free(s); 587 if (fdw == -1) 588 continue; 589 break; 590 } 591 if (fdw == -1) 592 return -4; 593 } | |
594 if (fd == -1) { 595 warnx("can't open device %s", disk); 596 return -1; 597 } 598 if (get_params() == -1) { 599 warnx("can't get disk parameters on %s", disk); 600 return -1; 601 } --- 4 unchanged lines hidden (view full) --- 606read_disk(off_t sector, void *buf) 607{ 608 609 lseek(fd, (sector * 512), 0); 610 return read(fd, buf, 611 secsize > MIN_SEC_SIZE ? secsize : MIN_SEC_SIZE * 2); 612} 613 | 580 if (fd == -1) { 581 warnx("can't open device %s", disk); 582 return -1; 583 } 584 if (get_params() == -1) { 585 warnx("can't get disk parameters on %s", disk); 586 return -1; 587 } --- 4 unchanged lines hidden (view full) --- 592read_disk(off_t sector, void *buf) 593{ 594 595 lseek(fd, (sector * 512), 0); 596 return read(fd, buf, 597 secsize > MIN_SEC_SIZE ? secsize : MIN_SEC_SIZE * 2); 598} 599 |
614static ssize_t | 600static int |
615write_disk(off_t sector, void *buf) 616{ | 601write_disk(off_t sector, void *buf) 602{ |
603 int error; 604 struct gctl_req *grq; 605 const char *q; 606 char fbuf[BUFSIZ]; 607 int i, fdw; |
|
617 | 608 |
618 if (fdw != -1) { 619 return ioctl(fdw, DIOCSPC98, buf); 620 } else { 621 lseek(fd, (sector * 512), 0); 622 /* write out in the size that the read_disk found worked */ 623 return write(fd, buf, 624 secsize > MIN_SEC_SIZE ? secsize : MIN_SEC_SIZE * 2); | 609 grq = gctl_get_handle(); 610 gctl_ro_param(grq, "verb", -1, "write PC98"); 611 gctl_ro_param(grq, "class", -1, "PC98"); 612 q = strrchr(disk, '/'); 613 if (q == NULL) 614 q = disk; 615 else 616 q++; 617 gctl_ro_param(grq, "geom", -1, q); 618 gctl_ro_param(grq, "data", secsize, buf); 619 q = gctl_issue(grq); 620 if (q == NULL) 621 return(0); 622 warnx("%s", q); 623 624 error = pwrite(fd, buf, secsize, (sector * 512)); 625 if (error == secsize) 626 return (0); 627 628 for (i = 0; i < NDOSPART; i++) { 629 sprintf(fbuf, "%ss%d", disk, i + 1); 630 fdw = open(fbuf, O_RDWR, 0); 631 if (fdw < 0) 632 continue; 633 error = ioctl(fdw, DIOCSPC98, buf); 634 close(fdw); 635 if (error == 0) 636 return (0); |
625 } | 637 } |
638 warnx("Failed to write sector zero"); 639 return(EINVAL); |
|
626} 627 628static int 629get_params() 630{ 631 int error; 632 u_int u; 633 off_t o; --- 211 unchanged lines hidden --- | 640} 641 642static int 643get_params() 644{ 645 int error; 646 u_int u; 647 off_t o; --- 211 unchanged lines hidden --- |