Index: head/lib/libdisk/create_chunk.c =================================================================== --- head/lib/libdisk/create_chunk.c (revision 40675) +++ head/lib/libdisk/create_chunk.c (revision 40676) @@ -1,361 +1,360 @@ /* * ---------------------------------------------------------------------------- * "THE BEER-WARE LICENSE" (Revision 42): * wrote this file. As long as you retain this notice you * can do whatever you want with this stuff. If we meet some day, and you think * this stuff is worth it, you can buy me a beer in return. Poul-Henning Kamp * ---------------------------------------------------------------------------- * - * $Id: create_chunk.c,v 1.30 1998/03/20 23:43:03 jkh Exp $ + * $Id: create_chunk.c,v 1.31 1998/09/15 10:23:17 gibbs Exp $ * */ #include #include #include #include #include #include #include #include #include #include #include #include #include #include "libdisk.h" /* Clone these two from sysinstall because we need our own copies * due to link order problems with `crunch'. Feh! */ static int isDebug() { static int debug = 0; /* Allow debugger to tweak it */ return debug; } /* Write something to the debugging port */ static void msgDebug(char *fmt, ...) { va_list args; char *dbg; static int DebugFD = -1; if (DebugFD == -1) DebugFD = open("/dev/ttyv1", O_RDWR); dbg = (char *)alloca(FILENAME_MAX); strcpy(dbg, "DEBUG: "); va_start(args, fmt); vsnprintf((char *)(dbg + strlen(dbg)), FILENAME_MAX, fmt, args); va_end(args); write(DebugFD, dbg, strlen(dbg)); } void Fixup_FreeBSD_Names(struct disk *d, struct chunk *c) { struct chunk *c1, *c3; int j; if (!strcmp(c->name, "X")) return; /* reset all names to "X" */ for (c1 = c->part; c1 ; c1 = c1->next) { c1->oname = c1->name; c1->name = malloc(12); if(!c1->name) err(1,"Malloc failed"); strcpy(c1->name,"X"); } /* Allocate the first swap-partition we find */ for (c1 = c->part; c1 ; c1 = c1->next) { if (c1->type == unused) continue; if (c1->subtype != FS_SWAP) continue; sprintf(c1->name,"%s%c",c->name,SWAP_PART+'a'); break; } /* Allocate the first root-partition we find */ for (c1 = c->part; c1 ; c1 = c1->next) { if (c1->type == unused) continue; if (!(c1->flags & CHUNK_IS_ROOT)) continue; sprintf(c1->name,"%s%c",c->name,0+'a'); break; } /* Try to give them the same as they had before */ for (c1 = c->part; c1 ; c1 = c1->next) { if (strcmp(c1->name,"X")) continue; for(c3 = c->part; c3 ; c3 = c3->next) if (c1 != c3 && !strcmp(c3->name, c1->oname)) { goto newname; } strcpy(c1->name,c1->oname); newname: } /* Allocate the rest sequentially */ for (c1 = c->part; c1 ; c1 = c1->next) { const char order[] = "efghabd"; if (c1->type == unused) continue; if (strcmp("X",c1->name)) continue; for(j=0;jname,"%s%c",c->name,order[j]); for(c3 = c->part; c3 ; c3 = c3->next) if (c1 != c3 && !strcmp(c3->name, c1->name)) goto match; break; match: strcpy(c1->name,"X"); continue; } } for (c1 = c->part; c1 ; c1 = c1->next) { free(c1->oname); c1->oname = 0; } } void Fixup_Extended_Names(struct disk *d, struct chunk *c) { struct chunk *c1; int j=5; for (c1 = c->part; c1 ; c1 = c1->next) { if (c1->type == unused) continue; free(c1->name); c1->name = malloc(12); if(!c1->name) err(1,"malloc failed"); sprintf(c1->name,"%ss%d",d->chunks->name,j++); if (c1->type == freebsd) Fixup_FreeBSD_Names(d,c1); } } void Fixup_Names(struct disk *d) { struct chunk *c1, *c2, *c3; int i,j; c1 = d->chunks; for(i=1,c2 = c1->part; c2 ; c2 = c2->next) { c2->flags &= ~CHUNK_BSD_COMPAT; if (c2->type == unused) continue; if (strcmp(c2->name,"X")) continue; c2->oname = malloc(12); if(!c2->oname) err(1,"malloc failed"); for(j=1;j<=NDOSPART;j++) { sprintf(c2->oname,"%ss%d",c1->name,j); for(c3 = c1->part; c3 ; c3 = c3->next) if (c3 != c2 && !strcmp(c3->name, c2->oname)) goto match; free(c2->name); c2->name = c2->oname; c2->oname = 0; break; match: continue; } if (c2->oname) free(c2->oname); } for(c2 = c1->part; c2 ; c2 = c2->next) { if (c2->type == freebsd) { c2->flags |= CHUNK_BSD_COMPAT; break; } } for(c2 = c1->part; c2 ; c2 = c2->next) { if (c2->type == freebsd) Fixup_FreeBSD_Names(d,c2); if (c2->type == extended) Fixup_Extended_Names(d,c2); } } int Create_Chunk(struct disk *d, u_long offset, u_long size, chunk_e type, int subtype, u_long flags) { int i; u_long l; if(!(flags & CHUNK_FORCE_ALL)) { /* Never use the first track */ if (!offset) { offset += d->bios_sect; size -= d->bios_sect; } /* Always end on cylinder boundary */ l = (offset+size) % (d->bios_sect * d->bios_hd); size -= l; } i = Add_Chunk(d,offset,size,"X",type,subtype,flags); Fixup_Names(d); return i; } struct chunk * Create_Chunk_DWIM(struct disk *d, struct chunk *parent , u_long size, chunk_e type, int subtype, u_long flags) { int i; struct chunk *c1; u_long offset,edge; if (!parent) parent = d->chunks; for (c1=parent->part; c1 ; c1 = c1->next) { if (c1->type != unused) continue; if (c1->size < size) continue; offset = c1->offset; goto found; } return 0; found: if (parent->flags & CHUNK_BAD144) { edge = c1->end - d->bios_sect - 127; if (offset > edge) return 0; if (offset + size > edge) size = edge - offset + 1; } i = Add_Chunk(d,offset,size,"X",type,subtype,flags); if (i) return 0; Fixup_Names(d); for (c1=parent->part; c1 ; c1 = c1->next) if (c1->offset == offset) return c1; err(1,"Serious internal trouble"); } int MakeDev(struct chunk *c1, const char *path) { char *p = c1->name; u_long cmaj, bmaj, min, unit, part, slice; char buf[BUFSIZ], buf2[BUFSIZ]; *buf2 = '\0'; if (isDebug()) msgDebug("MakeDev: Called with %s on path %s\n", p, path); if (!strcmp(p, "X")) return 0; if (!strncmp(p, "wd", 2)) - bmaj = 0, cmaj = 3; + bmaj = 0, cmaj = 3, p += 2; else if (!strncmp(p, "sd", 2)) - bmaj = 4, cmaj = 13; + bmaj = 4, cmaj = 13, p += 2; else if (!strncmp(p, "wfd", 3)) - bmaj = 1, cmaj = 87; + bmaj = 1, cmaj = 87, p += 3; else if (!strncmp(p, "da", 2)) /* CAM support */ - bmaj = 4, cmaj = 13; + bmaj = 4, cmaj = 13, p += 2; else { msgDebug("MakeDev: Unknown major/minor for devtype %s\n", p); return 0; } - p += 2; if (!isdigit(*p)) { msgDebug("MakeDev: Invalid disk unit passed: %s\n", p); return 0; } unit = *p - '0'; p++; if (!*p) { slice = 1; part = 2; goto done; } else if (isdigit(*p)) { unit *= 10; unit += (*p - '0'); p++; } if (*p != 's') { msgDebug("MakeDev: `%s' is not a valid slice delimiter\n", p); return 0; } p++; if (!isdigit(*p)) { msgDebug("MakeDev: `%s' is an invalid slice number\n", p); return 0; } slice = *p - '0'; p++; if (isdigit(*p)) { slice *= 10; slice += (*p - '0'); p++; } slice = slice + 1; if (!*p) { part = 2; if(c1->type == freebsd) sprintf(buf2, "%sc", c1->name); goto done; } if (*p < 'a' || *p > 'h') { msgDebug("MakeDev: `%s' is not a valid partition name.\n", p); return 0; } part = *p - 'a'; done: if (isDebug()) msgDebug("MakeDev: Unit %d, Slice %d, Part %d\n", unit, slice, part); if (unit > 32) return 0; if (slice > 32) return 0; min = unit * 8 + 65536 * slice + part; sprintf(buf, "%s/r%s", path, c1->name); unlink(buf); if (mknod(buf, S_IFCHR|0640, makedev(cmaj,min)) == -1) { msgDebug("mknod of %s returned failure status!\n", buf); return 0; } if (*buf2) { sprintf(buf, "%s/r%s", path, buf2); unlink(buf); if (mknod(buf, S_IFCHR|0640, makedev(cmaj,min)) == -1) { msgDebug("mknod of %s returned failure status!\n", buf); return 0; } } sprintf(buf, "%s/%s", path, c1->name); unlink(buf); if (mknod(buf, S_IFBLK|0640, makedev(bmaj,min)) == -1) { msgDebug("mknod of %s returned failure status!\n", buf); return 0; } return 1; } int MakeDevChunk(struct chunk *c1, const char *path) { int i; i = MakeDev(c1, path); if (c1->next) MakeDevChunk(c1->next, path); if (c1->part) MakeDevChunk(c1->part, path); return i; } int MakeDevDisk(struct disk *d, const char *path) { return MakeDevChunk(d->chunks, path); } Index: head/lib/libdisk/write_disk.c =================================================================== --- head/lib/libdisk/write_disk.c (revision 40675) +++ head/lib/libdisk/write_disk.c (revision 40676) @@ -1,261 +1,262 @@ /* * ---------------------------------------------------------------------------- * "THE BEER-WARE LICENSE" (Revision 42): * wrote this file. As long as you retain this notice you * can do whatever you want with this stuff. If we meet some day, and you think * this stuff is worth it, you can buy me a beer in return. Poul-Henning Kamp * ---------------------------------------------------------------------------- * - * $Id: write_disk.c,v 1.22 1998/09/30 21:40:51 jkh Exp $ + * $Id: write_disk.c,v 1.23 1998/10/06 11:57:08 dfr Exp $ * */ #include #include #include #include #include #include #include #include #include #include #include #include "libdisk.h" #define DOSPTYP_EXTENDED 5 #define BBSIZE 8192 #define SBSIZE 8192 #define DEF_RPM 3600 #define DEF_INTERLEAVE 1 #define WHERE(offset,disk) (disk->flags & DISK_ON_TRACK ? offset + 63 : offset) int Write_FreeBSD(int fd, struct disk *new, struct disk *old, struct chunk *c1) { struct disklabel *dl; struct chunk *c2; int i,j; void *p; u_char buf[BBSIZE]; #ifdef __alpha__ u_long *lp, sum; #endif for(i=0;ioffset,new)); memcpy(buf+512*i,p,512); free(p); } #if defined(__i386__) if(new->boot1) memcpy(buf,new->boot1,512); if(new->boot2) memcpy(buf+512,new->boot2,BBSIZE-512); #elif defined(__alpha__) if(new->boot1) memcpy(buf+512,new->boot1,BBSIZE-512); #endif dl = (struct disklabel *) (buf+512*LABELSECTOR+LABELOFFSET); memset(dl,0,sizeof *dl); for(c2=c1->part;c2;c2=c2->next) { if (c2->type == unused) continue; if (!strcmp(c2->name,"X")) continue; - j = c2->name[5] - 'a'; + j = c2->name[strlen(new->name) + 2] - 'a'; if (j < 0 || j >= MAXPARTITIONS || j == RAW_PART) { #ifdef DEBUG - warn("Weird parititon letter %c",c2->name[5]); + warn("Weird parititon letter %c",c2->name[strlen(new->name) + 2]); #endif continue; } dl->d_partitions[j].p_size = c2->size; dl->d_partitions[j].p_offset = c2->offset; dl->d_partitions[j].p_fstype = c2->subtype; } dl->d_bbsize = BBSIZE; /* * Add in defaults for superblock size, interleave, and rpms */ dl->d_sbsize = SBSIZE; dl->d_interleave = DEF_INTERLEAVE; dl->d_rpm = DEF_RPM; strcpy(dl->d_typename,c1->name); dl->d_secsize = 512; dl->d_secperunit = new->chunks->size; dl->d_ncylinders = new->bios_cyl; dl->d_ntracks = new->bios_hd; dl->d_nsectors = new->bios_sect; dl->d_secpercyl = dl->d_ntracks * dl->d_nsectors; dl->d_npartitions = MAXPARTITIONS; dl->d_type = new->name[0] == 's' || new->name[0] == 'd' || new->name[0] == 'o' ? DTYPE_SCSI : DTYPE_ESDI; dl->d_partitions[RAW_PART].p_size = c1->size; dl->d_partitions[RAW_PART].p_offset = c1->offset; if(new->flags & DISK_ON_TRACK) for(i=0;id_partitions[i].p_size) dl->d_partitions[i].p_offset += 63; dl->d_magic = DISKMAGIC; dl->d_magic2 = DISKMAGIC; dl->d_checksum = dkcksum(dl); #ifdef __alpha__ /* * Generate the bootblock checksum for the SRM console. */ for (lp = (u_long *)buf, i = 0, sum = 0; i < 63; i++) sum += lp[i]; lp[63] = sum; #endif for(i=0;ioffset,new),buf+512*i); } return 0; } int Write_Extended(int fd, struct disk *new, struct disk *old, struct chunk *c1) { return 0; } static void Write_Int32(u_int32_t *p, u_int32_t v) { u_int8_t *bp = (u_int8_t *)p; bp[0] = (v >> 0) & 0xff; bp[1] = (v >> 8) & 0xff; bp[2] = (v >> 16) & 0xff; bp[3] = (v >> 24) & 0xff; } int Write_Disk(struct disk *d1) { int fd,i,j; struct disk *old = 0; struct chunk *c1; int ret = 0; char device[64]; u_char *mbr; struct dos_partition *dp,work[NDOSPART]; int s[4]; strcpy(device,"/dev/r"); strcat(device,d1->name); fd = open(device,O_RDWR); if (fd < 0) { #ifdef DEBUG warn("open(%s) failed",device); #endif return 1; } memset(s,0,sizeof s); mbr = read_block(fd,WHERE(0,d1)); dp = (struct dos_partition*) (mbr + DOSPARTOFF); memcpy(work,dp,sizeof work); dp = work; free(mbr); for (c1=d1->chunks->part; c1 ; c1 = c1->next) { if (c1->type == unused) continue; if (!strcmp(c1->name,"X")) continue; j = c1->name[4] - '1'; + j = c1->name[strlen(d1->name) + 1] - '1'; if (j < 0 || j > 3) continue; s[j]++; if (c1->type == extended) ret += Write_Extended(fd, d1,old,c1); if (c1->type == freebsd) ret += Write_FreeBSD(fd, d1,old,c1); Write_Int32(&dp[j].dp_start, c1->offset); Write_Int32(&dp[j].dp_size, c1->size); i = c1->offset; if (i >= 1024*d1->bios_sect*d1->bios_hd) { dp[j].dp_ssect = 0xff; dp[j].dp_shd = 0xff; dp[j].dp_scyl = 0xff; } else { dp[j].dp_ssect = i % d1->bios_sect; i -= dp[j].dp_ssect++; i /= d1->bios_sect; dp[j].dp_shd = i % d1->bios_hd; i -= dp[j].dp_shd; i /= d1->bios_hd; dp[j].dp_scyl = i; i -= dp[j].dp_scyl; dp[j].dp_ssect |= i >> 2; } #ifdef DEBUG printf("S:%lu = (%x/%x/%x)", c1->offset,dp[j].dp_scyl,dp[j].dp_shd,dp[j].dp_ssect); #endif i = c1->end; dp[j].dp_esect = i % d1->bios_sect; i -= dp[j].dp_esect++; i /= d1->bios_sect; dp[j].dp_ehd = i % d1->bios_hd; i -= dp[j].dp_ehd; i /= d1->bios_hd; if (i>1023) i = 1023; dp[j].dp_ecyl = i; i -= dp[j].dp_ecyl; dp[j].dp_esect |= i >> 2; #ifdef DEBUG printf(" E:%lu = (%x/%x/%x)\n", c1->end,dp[j].dp_ecyl,dp[j].dp_ehd,dp[j].dp_esect); #endif dp[j].dp_typ = c1->subtype; if (c1->flags & CHUNK_ACTIVE) dp[j].dp_flag = 0x80; else dp[j].dp_flag = 0; } j = 0; for(i=0;ibootmgr) memcpy(mbr,d1->bootmgr,DOSPARTOFF); memcpy(mbr+DOSPARTOFF,dp,sizeof *dp * NDOSPART); mbr[512-2] = 0x55; mbr[512-1] = 0xaa; write_block(fd,WHERE(0,d1),mbr); i = 1; i = ioctl(fd,DIOCSYNCSLICEINFO,&i); #ifdef DEBUG if (i != 0) warn("ioctl(DIOCSYNCSLICEINFO)"); #endif close(fd); return 0; }