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