1 #include <stdio.h> 2 #include <string.h> 3 #include <fcntl.h> 4 #include <sys/ioctl.h> 5 #include <Drivers.h> 6 #include <StorageDefs.h> 7 8 static void dump_dev_size(int dev) 9 { 10 size_t sz; 11 if (ioctl(dev, B_GET_DEVICE_SIZE, &sz, sizeof(sz)) < 0) { 12 perror("ioctl(B_GET_DEVICE_SIZE)"); 13 return; 14 } 15 printf("size: %ld bytes\n", sz); 16 puts(""); 17 } 18 19 static void dump_bios_id(int dev) 20 { 21 uint8 id; 22 if (ioctl(dev, B_GET_BIOS_DRIVE_ID, &id, sizeof(id)) < 0) { 23 perror("ioctl(B_GET_BIOS_DRIVE_ID)"); 24 return; 25 } 26 printf("bios id: %d, 0x%x\n", id, id); 27 puts(""); 28 } 29 30 static void dump_media_status(int dev) 31 { 32 uint32 st; 33 if (ioctl(dev, B_GET_MEDIA_STATUS, &st, sizeof(st)) < 0) { 34 perror("ioctl(B_GET_MEDIA_STATUS)"); 35 return; 36 } 37 printf("media status: %s\n", strerror(st)); 38 puts(""); 39 } 40 41 static const char *device_type(uint32 type) 42 { 43 if (type == B_DISK) return "disk"; 44 if (type == B_TAPE) return "tape"; 45 if (type == B_PRINTER) return "printer"; 46 if (type == B_CPU) return "cpu"; 47 if (type == B_WORM) return "worm"; 48 if (type == B_CD) return "cd"; 49 if (type == B_SCANNER) return "scanner"; 50 if (type == B_OPTICAL) return "optical"; 51 if (type == B_JUKEBOX) return "jukebox"; 52 if (type == B_NETWORK) return "network"; 53 return "<unknown>"; 54 } 55 56 static void dump_geom(int dev, bool bios) 57 { 58 device_geometry geom; 59 60 if (ioctl(dev, bios?B_GET_BIOS_GEOMETRY:B_GET_GEOMETRY, &geom, sizeof(geom)) < 0) { 61 perror(bios ? "ioctl(B_GET_BIOS_GEOMETRY)" : "ioctl(B_GET_GEOMETRY)"); 62 return; 63 } 64 printf("%sgeometry:\n", bios?"bios ":""); 65 printf("bytes_per_sector:\t%ld\n", geom.bytes_per_sector); 66 printf("sectors_per_track:\t%ld\n", geom.sectors_per_track); 67 printf("cylinder_count:\t%ld\n", geom.cylinder_count); 68 printf("head_count:\t%ld\n", geom.head_count); 69 printf("device_type:\t%d, %s\n", geom.device_type, device_type(geom.device_type)); 70 printf("%sremovable.\n", geom.removable?"":"not "); 71 printf("%sread_only.\n", geom.read_only?"":"not "); 72 printf("%swrite_once.\n", geom.write_once?"":"not "); 73 puts(""); 74 } 75 76 static void dump_misc(int dev) 77 { 78 char path[B_PATH_NAME_LENGTH]; 79 if (ioctl(dev, B_GET_DRIVER_FOR_DEVICE, path, sizeof(path)) < 0) { 80 perror("ioctl(B_GET_DRIVER_FOR_DEVICE)"); 81 } else { 82 printf("driver:\t%s\n", path); 83 } 84 #ifdef __HAIKU__ 85 if (ioctl(dev, B_GET_PATH_FOR_DEVICE, path, sizeof(path)) < 0) { 86 perror("ioctl(B_GET_PATH_FOR_DEVICE)"); 87 } else { 88 printf("device path:\t%s\n", path); 89 } 90 #endif 91 puts(""); 92 } 93 94 int main(int argc, char **argv) 95 { 96 int dev; 97 if (argc < 2) { 98 printf("%s device\n", argv[0]); 99 return 1; 100 } 101 dev = open(argv[1], O_RDONLY); 102 if (dev < 0) { 103 perror("open"); 104 return 1; 105 } 106 dump_dev_size(dev); 107 dump_bios_id(dev); 108 dump_media_status(dev); 109 dump_geom(dev, false); 110 dump_geom(dev, true); 111 dump_misc(dev); 112 return 0; 113 } 114