disk.c revision 8250
1/* 2 * ---------------------------------------------------------------------------- 3 * "THE BEER-WARE LICENSE" (Revision 42): 4 * <phk@login.dknet.dk> wrote this file. As long as you retain this notice you 5 * can do whatever you want with this stuff. If we meet some day, and you think 6 * this stuff is worth it, you can buy me a beer in return. Poul-Henning Kamp 7 * ---------------------------------------------------------------------------- 8 * 9 * $Id: disk.c,v 1.14 1995/05/03 17:37:58 jkh Exp $ 10 * 11 */ 12 13#include <stdio.h> 14#include <stdlib.h> 15#include <unistd.h> 16#include <fcntl.h> 17#include <string.h> 18#include <err.h> 19#include <sys/types.h> 20#include <sys/stat.h> 21#include <sys/ioctl.h> 22#include <sys/disklabel.h> 23#include <sys/diskslice.h> 24#include "libdisk.h" 25 26#define DOSPTYP_EXTENDED 5 27#define DOSPTYP_ONTRACK 84 28 29char *chunk_n[] = { 30 "whole", 31 "unknown", 32 "fat", 33 "freebsd", 34 "extended", 35 "part", 36 "unused", 37 "reserved", 38 NULL 39}; 40 41struct disk * 42Open_Disk(char *name) 43{ 44 return Int_Open_Disk(name,0); 45} 46 47struct disk * 48Int_Open_Disk(char *name, u_long size) 49{ 50 int i,fd; 51 struct diskslices ds; 52 struct disklabel dl; 53 char device[64]; 54 struct disk *d; 55 struct dos_partition *dp; 56 void *p; 57 58 strcpy(device,"/dev/r"); 59 strcat(device,name); 60 61 d = (struct disk *)malloc(sizeof *d); 62 if(!d) err(1,"malloc failed"); 63 memset(d,0,sizeof *d); 64 65 fd = open(device,O_RDONLY); 66 if (fd < 0) { 67 warn("open(%s) failed",device); 68 return 0; 69 } 70 71 memset(&dl,0,sizeof dl); 72 ioctl(fd,DIOCGDINFO,&dl); 73 i = ioctl(fd,DIOCGSLICEINFO,&ds); 74 if (i < 0) { 75 warn("DIOCGSLICEINFO(%s) failed",device); 76 close(fd); 77 return 0; 78 } 79 80#ifdef DEBUG 81 for(i=0;i<ds.dss_nslices;i++) 82 if(ds.dss_slices[i].ds_openmask) 83 printf(" open(%d)=0x%2x", 84 i,ds.dss_slices[i].ds_openmask); 85 printf("\n"); 86#endif 87 88 if (!size) 89 size = ds.dss_slices[WHOLE_DISK_SLICE].ds_size; 90 91 p = read_block(fd,0); 92 dp = (struct dos_partition*)(p+DOSPARTOFF); 93 for(i=0;i<NDOSPART;i++) { 94 if (dp->dp_start >= size) continue; 95 if (dp->dp_start+dp->dp_size >= size) continue; 96 if (!dp->dp_size) continue; 97 98 if (dp->dp_typ == DOSPTYP_ONTRACK) 99 d->flags |= DISK_ON_TRACK; 100 101 } 102 free(p); 103 104 d->bios_sect = dl.d_nsectors; 105 d->bios_hd = dl.d_ntracks; 106 107 d->name = strdup(name); 108 109 110 if (dl.d_ntracks && dl.d_nsectors) 111 d->bios_cyl = size/(dl.d_ntracks*dl.d_nsectors); 112 113 if (Add_Chunk(d, 0, size, name,whole,0,0)) 114 warn("Failed to add 'whole' chunk"); 115 116 for(i=BASE_SLICE;i<ds.dss_nslices;i++) { 117 char sname[20]; 118 chunk_e ce; 119 u_long flags=0; 120 int subtype=0; 121 if (! ds.dss_slices[i].ds_size) 122 continue; 123 sprintf(sname,"%ss%d",name,i-1); 124 subtype = ds.dss_slices[i].ds_type; 125 switch (ds.dss_slices[i].ds_type) { 126 case 0xa5: 127 ce = freebsd; 128 break; 129 case 0x1: 130 case 0x4: 131 ce = fat; 132 break; 133 case DOSPTYP_EXTENDED: 134 ce = extended; 135 break; 136 default: 137 ce = unknown; 138 break; 139 } 140 flags |= CHUNK_ALIGN; 141 if (Add_Chunk(d,ds.dss_slices[i].ds_offset, 142 ds.dss_slices[i].ds_size, sname,ce,subtype,flags)) 143 warn("failed to add chunk for slice %d",i - 1); 144 145 if (ds.dss_slices[i].ds_type != 0xa5) 146 continue; 147 { 148 struct disklabel dl; 149 char pname[20]; 150 int j,k; 151 152 strcpy(pname,"/dev/r"); 153 strcat(pname,sname); 154 j = open(pname,O_RDONLY); 155 if (j < 0) { 156 warn("open(%s)",pname); 157 continue; 158 } 159 k = ioctl(j,DIOCGDINFO,&dl); 160 if (k < 0) { 161 warn("ioctl(%s,DIOCGDINFO)",pname); 162 close(j); 163 continue; 164 } 165 close(j); 166 167 for(j=0; j <= dl.d_npartitions; j++) { 168 if (j == RAW_PART) 169 continue; 170 if (j == 3) 171 continue; 172 if (j == dl.d_npartitions) { 173 j = 3; 174 dl.d_npartitions=0; 175 } 176 if (!dl.d_partitions[j].p_size) 177 continue; 178 if (dl.d_partitions[j].p_size + 179 dl.d_partitions[j].p_offset > 180 ds.dss_slices[i].ds_size) 181 continue; 182 sprintf(pname,"%s%c",sname,j+'a'); 183 if (Add_Chunk(d, 184 dl.d_partitions[j].p_offset + 185 ds.dss_slices[i].ds_offset, 186 dl.d_partitions[j].p_size, 187 pname,part, 188 dl.d_partitions[j].p_fstype, 189 0) && j != 3) 190 warn( 191 "Failed to add chunk for partition %c [%lu,%lu]", 192 j + 'a',dl.d_partitions[j].p_offset, 193 dl.d_partitions[j].p_size); 194 } 195 } 196 } 197 close(fd); 198 Fixup_Names(d); 199 return d; 200} 201 202void 203Debug_Disk(struct disk *d) 204{ 205 printf("Debug_Disk(%s)",d->name); 206 printf(" flags=%lx",d->flags); 207 printf(" real_geom=%lu/%lu/%lu",d->real_cyl,d->real_hd,d->real_sect); 208 printf(" bios_geom=%lu/%lu/%lu\n",d->bios_cyl,d->bios_hd,d->bios_sect); 209 printf(" boot1=%p, boot2=%p, bootmgr=%p\n", 210 d->boot1,d->boot2,d->bootmgr); 211 Debug_Chunk(d->chunks); 212} 213 214void 215Free_Disk(struct disk *d) 216{ 217 if(d->chunks) Free_Chunk(d->chunks); 218 if(d->name) free(d->name); 219 if(d->bootmgr) free(d->bootmgr); 220 if(d->boot1) free(d->boot1); 221 if(d->boot2) free(d->boot2); 222 free(d); 223} 224 225struct disk * 226Clone_Disk(struct disk *d) 227{ 228 struct disk *d2; 229 230 d2 = (struct disk*) malloc(sizeof *d2); 231 if(!d2) err(1,"malloc failed"); 232 *d2 = *d; 233 d2->name = strdup(d2->name); 234 d2->chunks = Clone_Chunk(d2->chunks); 235 if(d2->bootmgr) { 236 d2->bootmgr = malloc(DOSPARTOFF); 237 memcpy(d2->bootmgr,d->bootmgr,DOSPARTOFF); 238 } 239 if(d2->boot1) { 240 d2->boot1 = malloc(512); 241 memcpy(d2->boot1,d->boot1,512); 242 } 243 if(d2->boot2) { 244 d2->boot2 = malloc(512*15); 245 memcpy(d2->boot2,d->boot2,512*15); 246 } 247 return d2; 248} 249 250void 251Collapse_Disk(struct disk *d) 252{ 253 254 while(Collapse_Chunk(d,d->chunks)) 255 ; 256} 257 258static char * device_list[] = {"wd","sd",0}; 259 260char ** 261Disk_Names() 262{ 263 int i,j,k; 264 char disk[25]; 265 char diskname[25]; 266 struct stat st; 267 struct diskslices ds; 268 int fd; 269 static char **disks; 270 271 disks = malloc(sizeof *disks * (1 + MAX_NO_DISKS)); 272 memset(disks,0,sizeof *disks * (1 + MAX_NO_DISKS)); 273 k = 0; 274 for (j = 0; device_list[j]; j++) { 275 for (i = 0; i < 10; i++) { 276 sprintf(diskname, "%s%d", device_list[j], i); 277 sprintf(disk, "/dev/r%s", diskname); 278 if (stat(disk, &st) || !(st.st_mode & S_IFCHR)) 279 continue; 280 if ((fd = open(disk, O_RDWR)) == -1) 281 continue; 282 if (ioctl(fd, DIOCGSLICEINFO, &ds) == -1) { 283 close(fd); 284 continue; 285 } 286 disks[k++] = strdup(diskname); 287 if(k == MAX_NO_DISKS) 288 return disks; 289 } 290 } 291 return disks; 292} 293 294void 295Set_Boot_Mgr(struct disk *d, u_char *b) 296{ 297 if (d->bootmgr) 298 free(d->bootmgr); 299 if (!b) { 300 d->bootmgr = 0; 301 } else { 302 d->bootmgr = malloc(DOSPARTOFF); 303 if(!d->bootmgr) err(1,"malloc failed"); 304 memcpy(d->bootmgr,b,DOSPARTOFF); 305 } 306} 307 308void 309Set_Boot_Blocks(struct disk *d, u_char *b1, u_char *b2) 310{ 311 if (d->boot1) free(d->boot1); 312 d->boot1 = malloc(512); 313 if(!d->boot1) err(1,"malloc failed"); 314 memcpy(d->boot1,b1,512); 315 if (d->boot2) free(d->boot2); 316 d->boot2 = malloc(15*512); 317 if(!d->boot2) err(1,"malloc failed"); 318 memcpy(d->boot2,b2,15*512); 319} 320