/* build a cdboot partition if there is an embedded boot floppy image */ int cdpart(Disk *d) { uchar buf[2048]; ulong a, n; uchar *p; if(readdisk(d, buf, 17*2048, 2048) == -1 || strcmp((char*)buf+1, "CD001\x01EL TORITO SPECIFICATION") != 0) return 0; p = buf + 0x47; a = p[0] | (p[1]<<8) | (p[2]<<16) | (p[3]<<24); if(readdisk(d, buf, a*2048, 2048) == -1 || memcmp((char*)buf, "\x01\x00\x00\x00", 4) != 0 || memcmp((char*)buf+30, "\x55\xAA", 2) != 0 || buf[0x20] != 0x88) return 0; p = buf+0x28; a = p[0]|(p[1]<<8)|(p[2]<<16)|(p[3]<<24); switch(buf[0x21]) { case 1: n = 1200*1024; break; case 2: n = 1440*1024; break; case 3: n = 2880*1024; break; default: return 0; } a = a * (uvlong)2048 / d->secsize; n /= d->secsize; addpart(d, "cdboot", a, a+n); return 1; }
//------------------------------------------------------------------------ void musicxmlfactory::addgroup (int number, const char* name, const char* abbrev, bool groupbarline, vector<Sxmlelement>& parts) { Sxmlelement groupStart = element(k_part_group); groupStart->add (attribute ("number", number)); groupStart->add (attribute ("type", "start")); if (name) groupStart->push (element(k_group_name, name)); if (abbrev) groupStart->push (element(k_group_abbreviation, abbrev)); if (groupbarline) groupStart->push (element(k_group_barline, "yes")); fPartList->push (groupStart); for (vector<Sxmlelement>::const_iterator i = parts.begin(); i != parts.end(); i++) addpart(*i); Sxmlelement groupStop = element(k_part_group); groupStop->add (attribute ("number", number)); groupStop->add (attribute ("type", "stop")); fPartList->push (groupStop); }
int p9part(Disk *d, char *name, ulong pstart) { char partbuf[512]; char *field[4], *line[Npart+1], *name2; ulong start, end; int i, n; name2 = smprint("%s%s", d->prefix, name); d = opendisk(name2, 1, 0); if(!d) { fprint(2, "%s: %r\n", name2); free(name2); return 0; } free(name2); if(readdisk(d, partbuf, 512, sizeof partbuf) == -1) return 0; partbuf[sizeof partbuf - 1] = '\0'; if(strncmp(partbuf, "part ", 5) != 0 || (n = getfields(partbuf, line, Npart+1, 0, "\n")) == 0) return 0; for(i = 0; i < n; i++) { if(strncmp(line[i], "part ", 5) != 0) break; if(getfields(line[i], field, 4, 0, " ") != 4) break; start = strtoul(field[2], 0, 0); end = strtoul(field[3], 0, 0); if(start >= end) break; addpart(d, field[1], pstart+start, pstart+end); } return 0; }
void ctlwrite(Req *r) { int i; Cmdbuf *cb; vlong start, end; r->ofcall.count = r->ifcall.count; cb = parsecmd(r->ifcall.data, r->ifcall.count); if(cb->nf < 1){ respond(r, "empty control message"); free(cb); return; } if(strcmp(cb->f[0], "part") == 0){ if(cb->nf != 4){ respondcmderror(r, cb, "part takes 3 args"); free(cb); return; } start = strtoll(cb->f[2], 0, 0); end = strtoll(cb->f[3], 0, 0); if(addpart(cb->f[1], start, end) < 0){ respondcmderror(r, cb, "%r"); free(cb); return; } } else if(strcmp(cb->f[0], "delpart") == 0){ if(cb->nf != 2){ respondcmderror(r, cb, "delpart takes 1 arg"); free(cb); return; } if(delpart(cb->f[1]) < 0){ respondcmderror(r, cb, "%r"); free(cb); return; } } else if(strcmp(cb->f[0], "inquiry") == 0){ if(cb->nf != 2){ respondcmderror(r, cb, "inquiry takes 1 arg"); free(cb); return; } free(inquiry); inquiry = estrdup9p(cb->f[1]); } else if(strcmp(cb->f[0], "geometry") == 0){ if(cb->nf != 6){ respondcmderror(r, cb, "geometry takes 5 args"); free(cb); return; } nsect = strtoll(cb->f[1], 0, 0); sectsize = strtoll(cb->f[2], 0, 0); c = strtoll(cb->f[3], 0, 0); h = strtoll(cb->f[4], 0, 0); s = strtoll(cb->f[5], 0, 0); if(tab[0].inuse && strcmp(tab[0].name, "data") == 0 && tab[0].vers == 0){ tab[0].offset = 0; tab[0].length = nsect; } for(i=0; i<nelem(tab); i++){ if(tab[i].inuse && tab[i].offset+tab[i].length > nsect){ tab[i].inuse = 0; free(tab[i].name); tab[i].name = 0; } } } else{ respondcmderror(r, cb, "unknown control message"); free(cb); return; } free(cb); respond(r, nil); }
int main(int argc, char **argv){ int i,j,fd; long size; int pagesize, pagesecs; unsigned char *bp; struct ext2_super_block *e2bp; struct fat16_boot_sector *fat16bs; struct fat32_boot_sector *fat32bs; progname = argv[0]; if (argc != 2) { fprintf(stderr, "call: %s device\n", progname); exit(1); } device = argv[1]; fd = open(device, O_RDONLY); if (fd < 0) { perror(device); fprintf(stderr, "%s: could not open %s\n", progname, device); exit(1); } if (ioctl(fd, BLKGETSIZE, &size)) { struct stat s; perror("BLKGETSIZE"); fprintf(stderr, "%s: could not get device size\n", progname); if (stat(device, &s)) { fprintf(stderr, "and also stat fails. Aborting.\n"); exit(1); } size = s.st_size / 512; } pagesize = getpagesize(); if (pagesize <= 0) pagesize = 4096; else if (pagesize > MAXPAGESZ) { fprintf(stderr, "%s: ridiculous pagesize %d\n", progname, pagesize); exit(1); } pagesecs = pagesize/512; printf("# partition table of %s\n", device); printf("# total size %ld sectors\n", size); printf("unit: sectors\n"); for(i=0; i<size; i++) { if (i/BUFSECS != bufstart) { int len, secno; bufstart = i/BUFSECS; secno = bufstart*BUFSECS; len = BUFSECS; if (size - secno < len) len = size - secno; len = (len / 2)*2; /* avoid reading the last (odd) sector */ read_sectors(fd, buf, secno, len); } j = i % BUFSECS; bp = buf + 512 * j; if (bp[510] == 0x55 && bp[511] == 0xAA) { char *cp = bp+512-2-64; int j; if (i==0) continue; /* the MBR is supposed to be broken */ /* Unfortunately one finds extended partition table sectors that look just like a fat boot sector, except that the partition table bytes have been overwritten */ /* typical FAT32 end: "nd then press ...", followed by IO.SYS and MSDOS.SYS and WINBOOT.SYS directory entries. typical extd part tab end: 2 entries, 32 nul bytes */ for(j=0; j<32; j++) if (cp[32+j]) goto nonzero; addepts(i, bp); if (i > 0) { j = create_extended_partition(fd, i, size); if (j && j > i) i = j; /* skip */ } continue; nonzero: fat16bs = (struct fat16_boot_sector *) bp; if (fat16bs->s.media == 0xf8 && fat16bs->m.extd_signature == 0x29 && !strncmp(fat16bs->m.fs_name, "FAT", 3)) { int lth; lth = fat16bs->s.sectors[0] + fat16bs->s.sectors[1]*256; if (lth) { outmsg("small fat partition", i, i+lth, 0x1); addpart(i, lth, 0x1); } else { lth = fat16bs->s.total_sect; outmsg("fat partition", i, i+lth, 0x6); addpart(i, lth, 0x6); } i = i+lth-1; /* skip */ continue; } fat32bs = (struct fat32_boot_sector *) bp; if (fat32bs->s.media == 0xf8 && fat32bs->m.extd_signature == 0x29 && !strncmp(fat32bs->m.fs_name, "FAT32 ", 8)) { int lth = fat32bs->s.total_sect; outmsg("fat32 partition", i, i+lth, 0xb); /* or 0xc */ addpart(i, lth, 0xb); i = i+lth-1; /* skip */ continue; } } if (!strncmp(bp+502, "SWAP-SPACE", 10)) { char *last; int ct; int ss = i-pagesecs+1; int es; char buf2[MAXPAGESZ]; read_sectors(fd, buf2, ss, pagesecs); for (last = buf2+pagesize-10-1; last > buf2; last--) if (*last) break; for (ct = 7; ct >= 0; ct--) if (*last & (1<<ct)) break; es = ((last - buf2)*8 + ct + 1)*pagesecs + ss; if (es <= size) { outmsg("old swap space", ss, es, 0x82); addpart(ss, es-ss, 0x82); i = es-1; /* skip */ continue; } } if (!strncmp(bp+502, "SWAPSPACE2", 10)) { int ss = i-pagesecs+1; int es, lth; char buf2[MAXPAGESZ]; struct swap_header_v1 *p; read_sectors(fd, buf2, ss, pagesecs); p = (struct swap_header_v1 *) buf2; lth = (p->last_page + 1)* pagesecs; es = ss + lth; if (es <= size) { outmsg("new swap space", ss, es, 0x82); addpart(ss, lth, 0x82); i = es-1; /* skip */ continue; } } e2bp = (struct ext2_super_block *) bp; if (e2bp->s_magic == EXT2_SUPER_MAGIC && is_time(e2bp->s_mtime) && is_time(e2bp->s_wtime) && is_ztime(e2bp->s_lastcheck) && e2bp->s_log_block_size <= 10 /* at most 1 MB blocks */) { char buf[512]; struct ext2_super_block *bp2; int ss, sz, es, gsz, j; ss = i-2; sz = (e2bp->s_blocks_count << (e2bp->s_log_block_size + 1)); gsz = (e2bp->s_blocks_per_group << (e2bp->s_log_block_size + 1)); if (e2bp->s_block_group_nr > 0) ss -= gsz * e2bp->s_block_group_nr; es = ss + sz; if (ss > 0 && es > i && es <= size) { if (e2bp->s_block_group_nr == 0) { outmsg("ext2 partition", ss, es, 0x83); addpart(ss, es-ss, 0x83); i = es-1; /* skip */ continue; } /* maybe we jumped into the middle of a partially obliterated ext2 partition? */ printf("# sector %d looks like an ext2 superblock copy #%d;\n" "# in a partition covering sectors %d-%d\n", i, e2bp->s_block_group_nr, ss, es-1); for (j=1; j<=e2bp->s_block_group_nr; j++) { read_sectors(fd, buf, i-j*gsz, 1); bp2 = (struct ext2_super_block *) buf; if (bp2->s_magic != EXT2_SUPER_MAGIC || bp2->s_block_group_nr != e2bp->s_block_group_nr - j) break; } if (j == 1) printf("# however, sector %d doesnt look like a sb.\n", i-gsz); else if (j <= e2bp->s_block_group_nr) printf("# also the preceding %d block groups seem OK\n" "# but before that things seem to be wrong.\n", j-1); else { printf("# found all preceding superblocks OK\n" "# Warning: overlapping partitions?\n"); outmsg("ext2 partition", ss, es, 0x83); addpart(ss, es-ss, 0x83); i = es-1; /* skip */ continue; } } } if (bp[4] == 0x0d && bp[5] == 0x60 && bp[6] == 0x5e && bp[7] == 0xca && /* CA5E600D */ bp[156] == 0xee && bp[157] == 0xde && bp[158] == 0x0d && bp[159] == 0x60) /* 600DDEEE */ { int ss, es; struct unixware_slice *u; printf("# Unixware partition seen\n"); u = (struct unixware_slice *)(bp + 216); if (u->slice_type == 5 /* entire disk */ && (u->slice_flags & 0x200)) /* valid */ { ss = u->start; es = u->start + u->size; outmsg("Unixware ptn", ss, es, 0x63); addpart(ss, es-ss, 0x63); i = es-1; continue; } else printf("# Unrecognized details\n"); } /* bsd disk magic 0x82564557UL */ if (bp[0] == 0x57 && bp[1] == 0x45 && bp[2] == 0x56 && bp[3] == 0x82) { int ss, es, npts, j; struct bsd_disklabel *l; struct bsd_partition *p; printf("# BSD magic seen in sector %d\n", i); l = (struct bsd_disklabel *) bp; if (l->d_magic2[0] != 0x57 || l->d_magic2[1] != 0x45 || l->d_magic2[2] != 0x56 || l->d_magic2[3] != 0x82) printf("# 2nd magic bad - ignored this sector\n"); else if ((npts = l->d_npartitions) > 16) printf("# strange number (%d) of subpartitions - " "ignored this sector\n", npts); else { for (j=0; j<npts; j++) { p = &(l->d_partitions[j]); if (p->p_size) printf("# part %c: size %9d, start %9d\n", 'a'+j, p->p_size, p->p_offset); } ss = l->d_partitions[2].p_offset; es = ss + l->d_partitions[2].p_size; if (ss != i-1) printf("# strange start of whole disk - " "ignored this sector\n"); else { /* FreeBSD 0xa5, OpenBSD 0xa6, NetBSD 0xa9, BSDI 0xb7 */ /* How to distinguish? */ outmsg("BSD partition", ss, es, 0xa5); addpart(ss, es-ss, 0xa5); i = es-1; continue; } } } } outparts(); exit(0); }
int create_extended_partition(int fd, int secno, int size) { int sec = secno; int cursec = secno; int pno = partno; /* number of extd partition */ int ei = eptsct-1; unsigned char type = 0x5; int lastseen = secno; int ok = 0; if (epts[ei].secno != secno) { fprintf(stderr, "%s: program bug\n", progname); exit(1); } outmsg("candidate ext pt", secno, secno+1, type); addpart(secno, 1, type); /* size to be filled in later */ while(1) { char buf[512]; struct partition *p1, *p2, *pr, *pe; p1 = (struct partition *)(& epts[ei].pt4[0]); p2 = (struct partition *)(& epts[ei].pt4[16]); /* for the time being we just ignore the rest */ if (is_extended(p1->sys_type)) { pr = p2; pe = p1; } else if (is_extended(p2->sys_type)) { pr = p1; pe = p2; } else if (p1->sys_type == 0) { pr = p2; pe = 0; } else if (p2->sys_type == 0) { pr = p1; pe = 0; } else break; /* first handle the real partition, if any */ if (pr->sys_type != 0) { int ss = cursec + pr->start_sect; int es = ss + pr->nr_sects; outmsg("found in ept", ss, es, pr->sys_type); addpart(ss, pr->nr_sects, pr->sys_type); if (lastseen < es - 1) lastseen = es - 1; if (lastseen >= size) break; } /* then the extended link */ if (!pe) { ok = 1; break; } type = pe->sys_type; cursec = sec + pe->start_sect; if (cursec >= size) break; read_sectors(fd, buf, cursec, 1); addepts(cursec, buf); ei = eptsct-1; } if (!ok || lastseen == secno) { printf("# retracted\n"); partno = pno; return 0; } pts[pno].type = type; pts[pno].size = lastseen+1-secno; outmsg("extended part ok", secno, lastseen+1, type); return lastseen; }
static void ctlwrite0(Req *r, char *msg, Cmdbuf *cb) { vlong start, end; Part *p; r->ofcall.count = r->ifcall.count; if(cb->nf < 1){ respond(r, "empty control message"); return; } if(strcmp(cb->f[0], "part") == 0){ if(cb->nf != 4){ respondcmderror(r, cb, "part takes 3 args"); return; } start = strtoll(cb->f[2], 0, 0); end = strtoll(cb->f[3], 0, 0); if(addpart(cb->f[1], start, end) < 0){ respondcmderror(r, cb, "%r"); return; } } else if(strcmp(cb->f[0], "delpart") == 0){ if(cb->nf != 2){ respondcmderror(r, cb, "delpart takes 1 arg"); return; } if(delpart(cb->f[1]) < 0){ respondcmderror(r, cb, "%r"); return; } } else if(strcmp(cb->f[0], "inquiry") == 0){ if(cb->nf != 2){ respondcmderror(r, cb, "inquiry takes 1 arg"); return; } free(inquiry); inquiry = estrdup9p(cb->f[1]); } else if(strcmp(cb->f[0], "geometry") == 0){ if(cb->nf != 3){ respondcmderror(r, cb, "geometry takes 2 args"); return; } nsect = strtoll(cb->f[1], 0, 0); sectsize = strtoll(cb->f[2], 0, 0); if(tab[0].inuse && strcmp(tab[0].name, "data") == 0 && tab[0].vers == 0){ tab[0].offset = 0; tab[0].length = nsect; } for(p = tab; p < tab + nelem(tab); p++) if(p->inuse && p->offset + p->length > nsect){ p->inuse = 0; free(p->name); p->name = nil; } } else /* pass through to underlying ctl file, if any */ if (write(ctlfd, msg, r->ifcall.count) != r->ifcall.count) { respondcmderror(r, cb, "%r"); return; } respond(r, nil); }
int mbrpart(Disk *d) { uchar mbrbuf[512]; char name[10]; Dospart *dp; ulong taboffset, start, end; ulong firstxpart, nxtxpart; int i, nplan9, havedos; #define readmbr() \ if(readdisk(d, mbrbuf, (uvlong)taboffset*512, sizeof mbrbuf) == -1 \ || mbrbuf[0x1FE] != 0x55 || mbrbuf[0x1FF] != 0xAA) \ return 0 if(d->secsize > 512) return 0; dp = (Dospart*)&mbrbuf[0x1BE]; taboffset = 0; if(1) { /* get the MBR (allowing for DMDDO) */ readmbr(); for(i = 0; i < 4; i++) { if(dp[i].type == DMDDO) { taboffset = 63; readmbr(); i = -1; /* start over */ } } } /* * Read the partitions, first from the MBR and then * from successive extended partition tables. */ nplan9 = 0; havedos = 0; firstxpart = 0; for(;;) { readmbr(); nxtxpart = 0; for(i = 0; i < 4; i++) { /* partition offsets are relative to taboffset */ start = taboffset+GLONG(dp[i].start); end = start+GLONG(dp[i].len); if(dp[i].type == PLAN9) { if(nplan9 == 0) strcpy(name, "plan9"); else sprint(name, "plan9.%d", nplan9); addpart(d, name, start, end); p9part(d, name, start); nplan9++; } if(!havedos && isdos(dp[i].type)) { havedos = 1; addpart(d, "dos", start, end); } /* nxtxpart is relative to firstxpart (or 0), not taboffset */ if(isextend(dp[i].type)) nxtxpart = start-taboffset+firstxpart; } if(!nxtxpart) break; if(!firstxpart) firstxpart = nxtxpart; taboffset = nxtxpart; } return nplan9 + havedos; }
int main(int argc, char *argv[]) { cout << "Enter dimension" << endl; cin >> dim; //dim = 4; cout << "Enter number to generate partitions" << endl; cin >> num; //num = 20; //Initializing the array p[0] = node(dim); p[1] = node(dim); p[2] = node(dim); possible[0] = node(dim); possible[1] = node(dim); //Initializing possible with initial possible nodes int l=0; //Iterator for(int j=dim-1;j>=0;j--) { int *x = node(dim); x[j] = 1; delete [] possible[l]; possible[l] = x; l++; } //Creating List object shared_ptr<List> initialList(new List()); //Inserting 0 node initialList->insert_tail(node(dim)); //pointer initialization d = NULL; a = NULL; //Initilization of results res[0] = 1; res[1] = 1; sum[0] = 0; sum[1] = 1; int start_s = clock(); //The main call addpart(1,0,dim-1,initialList); int stop_s = clock(); //Printing time cout << "Time : " << (stop_s - start_s)/(double)(CLOCKS_PER_SEC)*1000 << endl; //Deleting pointer a if(a!=NULL) { delete [] a; } //Deleting pointer d if(d!=NULL) { delete [] d; } //Result /*cout << "Result : " << endl; for(int j=0;j<=num;j++) { cout << "res["<<j<<"]" << ": " << res[j] << endl; }*/ //Sum cout << "Sum : " << endl; for(int j=0;j<2000;j++) { if(j<=num) { cout << fixed << setprecision(0) << "sum["<<j<<"]" << ": " << sum[j] << endl; } //Deleting objects from the arrays if(p[j]!=NULL) { delete [] p[j]; } if(possible[j]!=NULL) { delete [] possible[j]; } } //Deleting List instance //delete initialList; }
void addpart(int n, int f, int t, shared_ptr<List> nodeList) { //Contianer for deleted nodes vector<int *> dNodes(100); int i,j,it; int s=0; int ns =0; for(i=f;i<=t;i++) { a = copyNode(possible[i]); delete [] p[n+1]; p[n+1] = a; //Transfering possible[i] to p[n+1] a = NULL; ns = 0; for(int l=dim-1;l>=0;l--)//Decrementing each coordinate { d = copyNode(possible[i]); d[l]--; if(nodeList->delete_node(d)) { dNodes[ns] = d; ns++; } else { delete [] d; } } nodeList->insert_tail(copyNode(possible[i])); res[n+1]++; sum[n+1] += nodeList->listSize(); if(n==num-1) { //Partition has been generated for(it=0;it<ns;it++) { nodeList->insert_tail(copyNode(dNodes[it])); } nodeList->delete_node(possible[i]); continue; } for(j=dim-1;j>=0;j--)//Increment each coordinate { a = copyNode(possible[i]); a[j]++; if(ispossible(p, a, n+1))//Check if node is possible { s++; delete [] possible[s+t]; possible[s+t] = a;//Add to possible a = NULL; } else { delete []a; } } addpart(n+1, i+1, s+t, nodeList);//Recursive call s = 0; for(it=0;it<ns;it++) { nodeList->insert_tail(copyNode(dNodes[it])); } nodeList->delete_node(possible[i]); dNodes.clear(); } }