fdisk.c 23 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138
  1. /*
  2. * This file is part of the UCB release of Plan 9. It is subject to the license
  3. * terms in the LICENSE file found in the top-level directory of this
  4. * distribution and at http://akaros.cs.berkeley.edu/files/Plan9License. No
  5. * part of the UCB release of Plan 9, including this file, may be copied,
  6. * modified, propagated, or distributed except according to the terms contained
  7. * in the LICENSE file.
  8. */
  9. /*
  10. * fdisk - edit dos disk partition table
  11. */
  12. #include <u.h>
  13. #include <libc.h>
  14. #include <bio.h>
  15. #include <ctype.h>
  16. #include <disk.h>
  17. #include "edit.h"
  18. typedef struct Dospart Dospart;
  19. enum {
  20. NTentry = 4,
  21. Mpart = 64,
  22. };
  23. static void rdpart(Edit*, uint64_t, uint64_t);
  24. static void findmbr(Edit*);
  25. static void autopart(Edit*);
  26. static void wrpart(Edit*);
  27. static void blankpart(Edit*);
  28. static void recover(Edit*);
  29. static int Dfmt(Fmt*);
  30. static int blank;
  31. static int dowrite;
  32. static int file;
  33. static int rdonly;
  34. static int doauto;
  35. static int64_t mbroffset;
  36. static int printflag;
  37. static int printchs;
  38. static int sec2cyl;
  39. static int written;
  40. static void cmdsum(Edit*, Part*, int64_t, int64_t);
  41. static char *cmdadd(Edit*, char*, int64_t, int64_t);
  42. static char *cmddel(Edit*, Part*);
  43. static char *cmdext(Edit*, int, char**);
  44. static char *cmdhelp(Edit*);
  45. static char *cmdokname(Edit*, char*);
  46. static char *cmdwrite(Edit*);
  47. static void cmdprintctl(Edit*, int);
  48. #pragma varargck type "D" uchar*
  49. Edit edit = {
  50. .add= cmdadd,
  51. .del= cmddel,
  52. .ext= cmdext,
  53. .help= cmdhelp,
  54. .okname= cmdokname,
  55. .sum= cmdsum,
  56. .write= cmdwrite,
  57. .printctl= cmdprintctl,
  58. .unit= "cylinder",
  59. };
  60. /*
  61. * Catch the obvious error routines to fix up the disk.
  62. */
  63. void
  64. sysfatal(char *fmt, ...)
  65. {
  66. char buf[1024];
  67. va_list arg;
  68. va_start(arg, fmt);
  69. vseprint(buf, buf+sizeof(buf), fmt, arg);
  70. va_end(arg);
  71. if(argv0)
  72. fprint(2, "%s: %s\n", argv0, buf);
  73. else
  74. fprint(2, "%s\n", buf);
  75. if(written)
  76. recover(&edit);
  77. exits(buf);
  78. }
  79. void
  80. abort(void)
  81. {
  82. fprint(2, "abort\n");
  83. recover(&edit);
  84. }
  85. void
  86. usage(void)
  87. {
  88. fprint(2, "usage: disk/fdisk [-abfprvw] [-s sectorsize] /dev/sdC0/data\n");
  89. exits("usage");
  90. }
  91. void
  92. main(int argc, char **argv)
  93. {
  94. int64_t secsize;
  95. secsize = 0;
  96. ARGBEGIN{
  97. case 'a':
  98. doauto++;
  99. break;
  100. case 'b':
  101. blank++;
  102. break;
  103. case 'f':
  104. file++;
  105. break;
  106. case 'p':
  107. printflag++;
  108. break;
  109. case 'r':
  110. rdonly++;
  111. break;
  112. case 's':
  113. secsize = atoi(ARGF());
  114. break;
  115. case 'v':
  116. printchs++;
  117. break;
  118. case 'w':
  119. dowrite++;
  120. break;
  121. }ARGEND;
  122. fmtinstall('D', Dfmt);
  123. if(argc != 1)
  124. usage();
  125. edit.disk = opendisk(argv[0], rdonly, file);
  126. if(edit.disk == nil) {
  127. fprint(2, "cannot open disk: %r\n");
  128. exits("opendisk");
  129. }
  130. if(secsize != 0) {
  131. edit.disk->secsize = secsize;
  132. edit.disk->secs = edit.disk->size / secsize;
  133. }
  134. sec2cyl = edit.disk->h * edit.disk->s;
  135. edit.end = edit.disk->secs / sec2cyl;
  136. findmbr(&edit);
  137. if(blank)
  138. blankpart(&edit);
  139. else
  140. rdpart(&edit, 0, 0);
  141. if(doauto)
  142. autopart(&edit);
  143. if(dowrite)
  144. runcmd(&edit, (char[]){"w"});
  145. if(printflag)
  146. runcmd(&edit, (char[]){"P"});
  147. if(dowrite || printflag)
  148. exits(0);
  149. fprint(2, "cylinder = %lld bytes\n", sec2cyl*edit.disk->secsize);
  150. runcmd(&edit, (char[]){"p"});
  151. for(;;) {
  152. fprint(2, ">>> ");
  153. runcmd(&edit, getline(&edit));
  154. }
  155. }
  156. typedef struct Tentry Tentry;
  157. typedef struct Table Table;
  158. typedef struct Type Type;
  159. typedef struct Tab Tab;
  160. typedef struct Recover Recover;
  161. struct Tentry {
  162. uint8_t active; /* active flag */
  163. uint8_t starth; /* starting head */
  164. uint8_t starts; /* starting sector */
  165. uint8_t startc; /* starting cylinder */
  166. uint8_t type; /* partition type */
  167. uint8_t endh; /* ending head */
  168. uint8_t ends; /* ending sector */
  169. uint8_t endc; /* ending cylinder */
  170. uint8_t xlba[4]; /* starting LBA from beginning of disc or ext. partition */
  171. uint8_t xsize[4]; /* size in sectors */
  172. };
  173. struct Table {
  174. Tentry entry[NTentry];
  175. uint8_t magic[2];
  176. uint8_t size[];
  177. };
  178. enum {
  179. Active = 0x80, /* partition is active */
  180. Primary = 0x01, /* internal flag */
  181. TypeBB = 0xFF,
  182. TypeEMPTY = 0x00,
  183. TypeFAT12 = 0x01,
  184. TypeXENIX = 0x02, /* root */
  185. TypeXENIXUSR = 0x03, /* usr */
  186. TypeFAT16 = 0x04,
  187. TypeEXTENDED = 0x05,
  188. TypeFATHUGE = 0x06,
  189. TypeHPFS = 0x07,
  190. TypeAIXBOOT = 0x08,
  191. TypeAIXDATA = 0x09,
  192. TypeOS2BOOT = 0x0A, /* OS/2 Boot Manager */
  193. TypeFAT32 = 0x0B, /* FAT 32 */
  194. TypeFAT32LBA = 0x0C, /* FAT 32 needing LBA support */
  195. TypeFAT16X = 0x0E, /* FAT 16 needing LBA support */
  196. TypeEXTHUGE = 0x0F, /* FAT 32 extended partition */
  197. TypeUNFORMATTED = 0x16, /* unformatted primary partition (OS/2 FDISK)? */
  198. TypeHPFS2 = 0x17,
  199. TypeIBMRecovery = 0x1C, /* really hidden fat */
  200. TypeCPM0 = 0x52,
  201. TypeDMDDO = 0x54, /* Disk Manager Dynamic Disk Overlay */
  202. TypeGB = 0x56, /* ???? */
  203. TypeSPEEDSTOR = 0x61,
  204. TypeSYSV386 = 0x63, /* also HURD? */
  205. TypeNETWARE = 0x64,
  206. TypePCIX = 0x75,
  207. TypeMINIX13 = 0x80, /* Minix v1.3 and below */
  208. TypeMINIX = 0x81, /* Minix v1.5+ */
  209. TypeLINUXSWAP = 0x82,
  210. TypeLINUX = 0x83,
  211. TypeLINUXEXT = 0x85,
  212. TypeLINUXLVM = 0x8E, /* logical volume manager */
  213. TypeAMOEBA = 0x93,
  214. TypeAMOEBABB = 0x94,
  215. TypeBSD386 = 0xA5,
  216. TypeNETBSD = 0xA9,
  217. TypeBSDI = 0xB7,
  218. TypeBSDISWAP = 0xB8,
  219. TypeOTHER = 0xDA,
  220. TypeCPM = 0xDB,
  221. TypeDellRecovery= 0xDE,
  222. TypeSPEEDSTOR12 = 0xE1,
  223. TypeSPEEDSTOR16 = 0xE4,
  224. TypeEFIProtect = 0xEE,
  225. TypeEFI = 0xEF,
  226. TypeLANSTEP = 0xFE,
  227. Type9 = 0x39,
  228. Toffset = 446, /* offset of partition table in sector */
  229. Magic0 = 0x55,
  230. Magic1 = 0xAA,
  231. Tablesz = offsetof(Table, size[0]),
  232. };
  233. struct Type {
  234. char *desc;
  235. char *name;
  236. };
  237. struct Dospart {
  238. Part;
  239. Tentry;
  240. uint32_t lba;
  241. uint32_t size;
  242. int primary;
  243. };
  244. struct Recover {
  245. Table table;
  246. uint32_t lba;
  247. };
  248. static Type types[256] = {
  249. [TypeEMPTY] { "EMPTY", "" },
  250. [TypeFAT12] { "FAT12", "dos" },
  251. [TypeFAT16] { "FAT16", "dos" },
  252. [TypeFAT32] { "FAT32", "dos" },
  253. [TypeFAT32LBA] { "FAT32LBA", "dos" },
  254. [TypeFAT16X] { "FAT16X", "dos" },
  255. [TypeEXTHUGE] { "EXTHUGE", "" },
  256. [TypeIBMRecovery] { "IBMRECOVERY", "ibm" },
  257. [TypeEXTENDED] { "EXTENDED", "" },
  258. [TypeFATHUGE] { "FATHUGE", "dos" },
  259. [TypeBB] { "BB", "bb" },
  260. [TypeXENIX] { "XENIX", "xenix" },
  261. [TypeXENIXUSR] { "XENIX USR", "xenixusr" },
  262. [TypeHPFS] { "HPFS", "ntfs" },
  263. [TypeAIXBOOT] { "AIXBOOT", "aixboot" },
  264. [TypeAIXDATA] { "AIXDATA", "aixdata" },
  265. [TypeOS2BOOT] { "OS/2BOOT", "os2boot" },
  266. [TypeUNFORMATTED] { "UNFORMATTED", "" },
  267. [TypeHPFS2] { "HPFS2", "hpfs2" },
  268. [TypeCPM0] { "CPM0", "cpm0" },
  269. [TypeDMDDO] { "DMDDO", "dmdd0" },
  270. [TypeGB] { "GB", "gb" },
  271. [TypeSPEEDSTOR] { "SPEEDSTOR", "speedstor" },
  272. [TypeSYSV386] { "SYSV386", "sysv386" },
  273. [TypeNETWARE] { "NETWARE", "netware" },
  274. [TypePCIX] { "PCIX", "pcix" },
  275. [TypeMINIX13] { "MINIXV1.3", "minix13" },
  276. [TypeMINIX] { "MINIXV1.5", "minix15" },
  277. [TypeLINUXSWAP] { "LINUXSWAP", "linuxswap" },
  278. [TypeLINUX] { "LINUX", "linux" },
  279. [TypeLINUXEXT] { "LINUXEXTENDED", "" },
  280. [TypeLINUXLVM] { "LINUXLVM", "linuxlvm" },
  281. [TypeAMOEBA] { "AMOEBA", "amoeba" },
  282. [TypeAMOEBABB] { "AMOEBABB", "amoebaboot" },
  283. [TypeBSD386] { "BSD386", "bsd386" },
  284. [TypeNETBSD] { "NETBSD", "netbsd" },
  285. [TypeBSDI] { "BSDI", "bsdi" },
  286. [TypeBSDISWAP] { "BSDISWAP", "bsdiswap" },
  287. [TypeOTHER] { "OTHER", "other" },
  288. [TypeCPM] { "CPM", "cpm" },
  289. [TypeDellRecovery] { "DELLRECOVERY", "dell" },
  290. [TypeSPEEDSTOR12] { "SPEEDSTOR12", "speedstor" },
  291. [TypeSPEEDSTOR16] { "SPEEDSTOR16", "speedstor" },
  292. [TypeEFIProtect] { "EFIPROTECT", "efiprotect" },
  293. [TypeEFI] { "EFI", "efi" },
  294. [TypeLANSTEP] { "LANSTEP", "lanstep" },
  295. [Type9] { "PLAN9", "plan9" },
  296. };
  297. static Dospart part[Mpart];
  298. static int npart;
  299. static char*
  300. typestr0(int type)
  301. {
  302. static char buf[100];
  303. sprint(buf, "type %d", type);
  304. if(type < 0 || type >= 256)
  305. return buf;
  306. if(types[type].desc == nil)
  307. return buf;
  308. return types[type].desc;
  309. }
  310. static uint32_t
  311. getle32(void* v)
  312. {
  313. uint8_t *p;
  314. p = v;
  315. return (p[3]<<24)|(p[2]<<16)|(p[1]<<8)|p[0];
  316. }
  317. static void
  318. putle32(void* v, uint32_t i)
  319. {
  320. uint8_t *p;
  321. p = v;
  322. p[0] = i;
  323. p[1] = i>>8;
  324. p[2] = i>>16;
  325. p[3] = i>>24;
  326. }
  327. static void
  328. diskread(Disk *disk, void *data, int ndata, uint32_t sec, uint32_t off)
  329. {
  330. if(seek(disk->fd, (int64_t)sec*disk->secsize+off, 0) != (int64_t)sec*disk->secsize+off)
  331. sysfatal("diskread seek %lud.%lud: %r", (uint32_t)sec,
  332. (uint32_t)off);
  333. if(readn(disk->fd, data, ndata) != ndata)
  334. sysfatal("diskread %lud at %lud.%lud: %r", (uint32_t)ndata,
  335. (uint32_t)sec, (uint32_t)off);
  336. }
  337. static int
  338. diskwrite(Disk *disk, void *data, int ndata, uint32_t sec, uint32_t off)
  339. {
  340. written = 1;
  341. if(seek(disk->wfd, (int64_t)sec*disk->secsize+off, 0) != (int64_t)sec*disk->secsize+off)
  342. goto Error;
  343. if(write(disk->wfd, data, ndata) != ndata)
  344. goto Error;
  345. return 0;
  346. Error:
  347. fprint(2, "write %d bytes at %lud.%lud failed: %r\n", ndata,
  348. (uint32_t)sec, (uint32_t)off);
  349. return -1;
  350. }
  351. static Dospart*
  352. mkpart(char *name, int primary, int64_t lba, int64_t size, Tentry *t)
  353. {
  354. static int n;
  355. Dospart *p;
  356. p = emalloc(sizeof(*p));
  357. if(name)
  358. p->name = estrdup(name);
  359. else{
  360. p->name = emalloc(20);
  361. sprint(p->name, "%c%d", primary ? 'p' : 's', ++n);
  362. }
  363. if(t)
  364. p->Tentry = *t;
  365. else
  366. memset(&p->Tentry, 0, sizeof(Tentry));
  367. p->changed = 0;
  368. p->start = lba/sec2cyl;
  369. p->end = (lba+size)/sec2cyl;
  370. p->ctlstart = lba;
  371. p->ctlend = lba+size;
  372. p->lba = lba;
  373. if (p->lba != lba)
  374. fprint(2, "%s: start of partition (%lld) won't fit in MBR table\n", argv0, lba);
  375. p->size = size;
  376. if (p->size != size)
  377. fprint(2, "%s: size of partition (%lld) won't fit in MBR table\n", argv0, size);
  378. p->primary = primary;
  379. return p;
  380. }
  381. /*
  382. * Recovery takes care of remembering what the various tables
  383. * looked like when we started, attempting to restore them when
  384. * we are finished.
  385. */
  386. static Recover *rtab;
  387. static int nrtab;
  388. static void
  389. addrecover(Table t, uint32_t lba)
  390. {
  391. if((nrtab%8) == 0) {
  392. rtab = realloc(rtab, (nrtab+8)*sizeof(rtab[0]));
  393. if(rtab == nil)
  394. sysfatal("out of memory");
  395. }
  396. rtab[nrtab] = (Recover){t, lba};
  397. nrtab++;
  398. }
  399. static void
  400. recover(Edit *edit)
  401. {
  402. int err, i, ctlfd;
  403. int64_t offset;
  404. err = 0;
  405. for(i=0; i<nrtab; i++)
  406. if(diskwrite(edit->disk, &rtab[i].table, Tablesz, rtab[i].lba, Toffset) < 0)
  407. err = 1;
  408. if(err) {
  409. fprint(2, "warning: some writes failed during restoration of old partition tables\n");
  410. exits("inconsistent");
  411. } else {
  412. fprint(2, "restored old partition tables\n");
  413. }
  414. ctlfd = edit->disk->ctlfd;
  415. offset = edit->disk->offset;
  416. if(ctlfd >= 0){
  417. for(i=0; i<edit->npart; i++)
  418. if(edit->part[i]->ctlname && fprint(ctlfd, "delpart %s", edit->part[i]->ctlname)<0)
  419. fprint(2, "delpart failed: %s: %r", edit->part[i]->ctlname);
  420. for(i=0; i<edit->nctlpart; i++)
  421. if(edit->part[i]->name && fprint(ctlfd, "delpart %s", edit->ctlpart[i]->name)<0)
  422. fprint(2, "delpart failed: %s: %r", edit->ctlpart[i]->name);
  423. for(i=0; i<edit->nctlpart; i++){
  424. if(fprint(ctlfd, "part %s %lld %lld", edit->ctlpart[i]->name,
  425. edit->ctlpart[i]->start+offset, edit->ctlpart[i]->end+offset) < 0){
  426. fprint(2, "restored disk partition table but not kernel; reboot\n");
  427. exits("inconsistent");
  428. }
  429. }
  430. }
  431. exits("restored");
  432. }
  433. /*
  434. * Read the partition table (including extended partition tables)
  435. * from the disk into the part array.
  436. */
  437. static void
  438. rdpart(Edit *edit, uint64_t lba, uint64_t xbase)
  439. {
  440. char *err;
  441. Table table;
  442. Tentry *tp, *ep;
  443. Dospart *p;
  444. if(xbase == 0)
  445. xbase = lba;
  446. diskread(edit->disk, &table, Tablesz, mbroffset+lba, Toffset);
  447. addrecover(table, mbroffset+lba);
  448. if(table.magic[0] != Magic0 || table.magic[1] != Magic1) {
  449. assert(lba != 0);
  450. return;
  451. }
  452. for(tp=table.entry, ep=tp+NTentry; tp<ep && npart < Mpart; tp++) {
  453. switch(tp->type) {
  454. case TypeEMPTY:
  455. break;
  456. case TypeEXTENDED:
  457. case TypeEXTHUGE:
  458. case TypeLINUXEXT:
  459. rdpart(edit, xbase+getle32(tp->xlba), xbase);
  460. break;
  461. default:
  462. p = mkpart(nil, lba==0, lba+getle32(tp->xlba), getle32(tp->xsize), tp);
  463. if(err = addpart(edit, p))
  464. fprint(2, "adding partition: %s\n", err);
  465. break;
  466. }
  467. }
  468. }
  469. static void
  470. blankpart(Edit *edit)
  471. {
  472. edit->changed = 1;
  473. }
  474. static void
  475. findmbr(Edit *edit)
  476. {
  477. Table table;
  478. Tentry *tp;
  479. diskread(edit->disk, &table, Tablesz, 0, Toffset);
  480. if(table.magic[0] != Magic0 || table.magic[1] != Magic1)
  481. sysfatal("did not find master boot record");
  482. for(tp = table.entry; tp < &table.entry[NTentry]; tp++)
  483. if(tp->type == TypeDMDDO)
  484. mbroffset = edit->disk->s;
  485. }
  486. static int
  487. haveroom(Edit *edit, int primary, int64_t start)
  488. {
  489. int i, lastsec, n;
  490. Dospart *p, *q;
  491. uint32_t pend, qstart;
  492. if(primary) {
  493. /*
  494. * must be open primary slot.
  495. * primary slots are taken by primary partitions
  496. * and runs of secondary partitions.
  497. */
  498. n = 0;
  499. lastsec = 0;
  500. for(i=0; i<edit->npart; i++) {
  501. p = (Dospart*)edit->part[i];
  502. if(p->primary)
  503. n++, lastsec=0;
  504. else if(!lastsec)
  505. n++, lastsec=1;
  506. }
  507. return n<4;
  508. }
  509. /*
  510. * secondary partitions can be inserted between two primary
  511. * partitions only if there is an empty primary slot.
  512. * otherwise, we can put a new secondary partition next
  513. * to a secondary partition no problem.
  514. */
  515. n = 0;
  516. for(i=0; i<edit->npart; i++){
  517. p = (Dospart*)edit->part[i];
  518. if(p->primary)
  519. n++;
  520. pend = p->end;
  521. if(i+1<edit->npart){
  522. q = (Dospart*)edit->part[i+1];
  523. qstart = q->start;
  524. }else{
  525. qstart = edit->end;
  526. q = nil;
  527. }
  528. if(start < pend || start >= qstart)
  529. continue;
  530. /* we go between these two */
  531. if(p->primary==0 || (q && q->primary==0))
  532. return 1;
  533. }
  534. /* not next to a secondary, need a new primary */
  535. return n<4;
  536. }
  537. static void
  538. autopart(Edit *edit)
  539. {
  540. char *err;
  541. int active, i;
  542. int64_t bigstart, bigsize, start;
  543. Dospart *p;
  544. for(i=0; i<edit->npart; i++)
  545. if(((Dospart*)edit->part[i])->type == Type9)
  546. return;
  547. /* look for the biggest gap in which we can put a primary partition */
  548. start = 0;
  549. bigsize = 0;
  550. SET(bigstart);
  551. for(i=0; i<edit->npart; i++) {
  552. p = (Dospart*)edit->part[i];
  553. if(p->start > start && p->start - start > bigsize && haveroom(edit, 1, start)) {
  554. bigsize = p->start - start;
  555. bigstart = start;
  556. }
  557. start = p->end;
  558. }
  559. if(edit->end - start > bigsize && haveroom(edit, 1, start)) {
  560. bigsize = edit->end - start;
  561. bigstart = start;
  562. }
  563. if(bigsize < 1) {
  564. fprint(2, "couldn't find space or partition slot for plan 9 partition\n");
  565. return;
  566. }
  567. /* set new partition active only if no others are */
  568. active = Active;
  569. for(i=0; i<edit->npart; i++)
  570. if(((Dospart*)edit->part[i])->primary && (((Dospart*)edit->part[i])->active & Active))
  571. active = 0;
  572. /* add new plan 9 partition */
  573. bigsize *= sec2cyl;
  574. bigstart *= sec2cyl;
  575. if(bigstart == 0) {
  576. bigstart += edit->disk->s;
  577. bigsize -= edit->disk->s;
  578. }
  579. p = mkpart(nil, 1, bigstart, bigsize, nil);
  580. p->active = active;
  581. p->changed = 1;
  582. p->type = Type9;
  583. edit->changed = 1;
  584. if(err = addpart(edit, p)) {
  585. fprint(2, "error adding plan9 partition: %s\n", err);
  586. return;
  587. }
  588. }
  589. typedef struct Name Name;
  590. struct Name {
  591. char *name;
  592. Name *link;
  593. };
  594. Name *namelist;
  595. static void
  596. plan9print(Dospart *part, int fd)
  597. {
  598. int i, ok;
  599. char *name, *vname;
  600. Name *n;
  601. int64_t start, end;
  602. char *sep;
  603. vname = types[part->type].name;
  604. if(vname==nil || strcmp(vname, "")==0) {
  605. part->ctlname = "";
  606. return;
  607. }
  608. start = mbroffset+part->lba;
  609. end = start+part->size;
  610. /* avoid names like plan90 */
  611. i = strlen(vname) - 1;
  612. if(vname[i] >= '0' && vname[i] <= '9')
  613. sep = ".";
  614. else
  615. sep = "";
  616. i = 0;
  617. name = emalloc(strlen(vname)+10);
  618. sprint(name, "%s", vname);
  619. do {
  620. ok = 1;
  621. for(n=namelist; n; n=n->link) {
  622. if(strcmp(name, n->name) == 0) {
  623. i++;
  624. sprint(name, "%s%s%d", vname, sep, i);
  625. ok = 0;
  626. }
  627. }
  628. } while(ok == 0);
  629. n = emalloc(sizeof(*n));
  630. n->name = name;
  631. n->link = namelist;
  632. namelist = n;
  633. part->ctlname = name;
  634. if(fd >= 0)
  635. print("part %s %lld %lld\n", name, start, end);
  636. }
  637. static void
  638. freenamelist(void)
  639. {
  640. Name *n, *next;
  641. for(n=namelist; n; n=next) {
  642. next = n->link;
  643. free(n);
  644. }
  645. namelist = nil;
  646. }
  647. static void
  648. cmdprintctl(Edit *edit, int ctlfd)
  649. {
  650. int i;
  651. freenamelist();
  652. for(i=0; i<edit->npart; i++)
  653. plan9print((Dospart*)edit->part[i], -1);
  654. ctldiff(edit, ctlfd);
  655. }
  656. static char*
  657. cmdokname(Edit *e, char *name)
  658. {
  659. char *q;
  660. if(name[0] != 'p' && name[0] != 's')
  661. return "name must be pN or sN";
  662. strtol(name+1, &q, 10);
  663. if(*q != '\0')
  664. return "name must be pN or sN";
  665. return nil;
  666. }
  667. #define TB (1024LL*GB)
  668. #define GB (1024*1024*1024)
  669. #define MB (1024*1024)
  670. #define KB (1024)
  671. static void
  672. cmdsum(Edit *edit, Part *vp, int64_t a, int64_t b)
  673. {
  674. char *name, *ty;
  675. char buf[3];
  676. char *suf;
  677. Dospart *p;
  678. int64_t sz, div;
  679. p = (Dospart*)vp;
  680. buf[0] = p && p->changed ? '\'' : ' ';
  681. buf[1] = p && (p->active & Active) ? '*' : ' ';
  682. buf[2] = '\0';
  683. name = p ? p->name : "empty";
  684. ty = p ? typestr0(p->type) : "";
  685. sz = (b-a)*edit->disk->secsize*sec2cyl;
  686. if(sz >= 1*TB){
  687. suf = "TB";
  688. div = TB;
  689. }else if(sz >= 1*GB){
  690. suf = "GB";
  691. div = GB;
  692. }else if(sz >= 1*MB){
  693. suf = "MB";
  694. div = MB;
  695. }else if(sz >= 1*KB){
  696. suf = "KB";
  697. div = KB;
  698. }else{
  699. suf = "B ";
  700. div = 1;
  701. }
  702. if(div == 1)
  703. print("%s %-12s %*lld %-*lld (%lld cylinders, %lld %s) %s\n", buf, name,
  704. edit->disk->width, a, edit->disk->width, b, b-a, sz, suf, ty);
  705. else
  706. print("%s %-12s %*lld %-*lld (%lld cylinders, %lld.%.2d %s) %s\n", buf, name,
  707. edit->disk->width, a, edit->disk->width, b, b-a,
  708. sz/div, (int)(((sz%div)*100)/div), suf, ty);
  709. }
  710. static char*
  711. cmdadd(Edit *edit, char *name, int64_t start, int64_t end)
  712. {
  713. Dospart *p;
  714. if(!haveroom(edit, name[0]=='p', start))
  715. return "no room for partition";
  716. start *= sec2cyl;
  717. end *= sec2cyl;
  718. if(start == 0 || name[0] != 'p')
  719. start += edit->disk->s;
  720. p = mkpart(name, name[0]=='p', start, end-start, nil);
  721. p->changed = 1;
  722. p->type = Type9;
  723. return addpart(edit, p);
  724. }
  725. static char*
  726. cmddel(Edit *edit, Part *p)
  727. {
  728. return delpart(edit, p);
  729. }
  730. static char*
  731. cmdwrite(Edit *edit)
  732. {
  733. wrpart(edit);
  734. return nil;
  735. }
  736. static char *help =
  737. "A name - set partition active\n"
  738. "P - print table in ctl format\n"
  739. "R - restore disk back to initial configuration and exit\n"
  740. "e - show empty dos partitions\n"
  741. "t name [type] - set partition type\n";
  742. static char*
  743. cmdhelp(Edit *e)
  744. {
  745. print("%s\n", help);
  746. return nil;
  747. }
  748. static char*
  749. cmdactive(Edit *edit, int nf, char **f)
  750. {
  751. int i;
  752. Dospart *p, *ip;
  753. if(nf != 2)
  754. return "args";
  755. if(f[1][0] != 'p')
  756. return "cannot set secondary partition active";
  757. if((p = (Dospart*)findpart(edit, f[1])) == nil)
  758. return "unknown partition";
  759. for(i=0; i<edit->npart; i++) {
  760. ip = (Dospart*)edit->part[i];
  761. if(ip->active & Active) {
  762. ip->active &= ~Active;
  763. ip->changed = 1;
  764. edit->changed = 1;
  765. }
  766. }
  767. if((p->active & Active) == 0) {
  768. p->active |= Active;
  769. p->changed = 1;
  770. edit->changed = 1;
  771. }
  772. return nil;
  773. }
  774. static char*
  775. strupr(char *s)
  776. {
  777. char *p;
  778. for(p=s; *p; p++)
  779. *p = toupper(*p);
  780. return s;
  781. }
  782. static void
  783. dumplist(void)
  784. {
  785. int i, n;
  786. n = 0;
  787. for(i=0; i<256; i++) {
  788. if(types[i].desc) {
  789. print("%-16s", types[i].desc);
  790. if(n++%4 == 3)
  791. print("\n");
  792. }
  793. }
  794. if(n%4)
  795. print("\n");
  796. }
  797. static char*
  798. cmdtype(Edit *edit, int nf, char **f)
  799. {
  800. char *q;
  801. Dospart *p;
  802. int i;
  803. if(nf < 2)
  804. return "args";
  805. if((p = (Dospart*)findpart(edit, f[1])) == nil)
  806. return "unknown partition";
  807. if(nf == 2) {
  808. for(;;) {
  809. fprint(2, "new partition type [? for list]: ");
  810. q = getline(edit);
  811. if(q[0] == '?')
  812. dumplist();
  813. else
  814. break;
  815. }
  816. } else
  817. q = f[2];
  818. strupr(q);
  819. for(i=0; i<256; i++)
  820. if(types[i].desc && strcmp(types[i].desc, q) == 0)
  821. break;
  822. if(i < 256 && p->type != i) {
  823. p->type = i;
  824. p->changed = 1;
  825. edit->changed = 1;
  826. }
  827. return nil;
  828. }
  829. static char*
  830. cmdext(Edit *edit, int nf, char **f)
  831. {
  832. switch(f[0][0]) {
  833. case 'A':
  834. return cmdactive(edit, nf, f);
  835. case 't':
  836. return cmdtype(edit, nf, f);
  837. case 'R':
  838. recover(edit);
  839. return nil;
  840. default:
  841. return "unknown command";
  842. }
  843. }
  844. static int
  845. Dfmt(Fmt *f)
  846. {
  847. char buf[60];
  848. uint8_t *p;
  849. int c, h, s;
  850. p = va_arg(f->args, uint8_t*);
  851. h = p[0];
  852. c = p[2];
  853. c |= (p[1]&0xC0)<<2;
  854. s = (p[1] & 0x3F);
  855. sprint(buf, "%d/%d/%d", c, h, s);
  856. return fmtstrcpy(f, buf);
  857. }
  858. static void
  859. writechs(Disk *disk, uint8_t *p, int64_t lba)
  860. {
  861. int c, h, s;
  862. s = lba % disk->s;
  863. h = (lba / disk->s) % disk->h;
  864. c = lba / (disk->s * disk->h);
  865. if(c >= 1024) {
  866. c = 1023;
  867. h = disk->h - 1;
  868. s = disk->s - 1;
  869. }
  870. p[0] = h;
  871. p[1] = ((s+1) & 0x3F) | ((c>>2) & 0xC0);
  872. p[2] = c;
  873. }
  874. static void
  875. wrtentry(Disk *disk, Tentry *tp, int type, uint32_t xbase, uint32_t lba,
  876. uint32_t end)
  877. {
  878. tp->type = type;
  879. writechs(disk, &tp->starth, lba);
  880. writechs(disk, &tp->endh, end-1);
  881. putle32(tp->xlba, lba-xbase);
  882. putle32(tp->xsize, end-lba);
  883. }
  884. static int
  885. wrextend(Edit *edit, int i, int64_t xbase, int64_t startlba,
  886. int64_t *endlba)
  887. {
  888. int ni;
  889. Table table;
  890. Tentry *tp, *ep;
  891. Dospart *p;
  892. Disk *disk;
  893. if(i == edit->npart){
  894. *endlba = edit->disk->secs;
  895. Finish:
  896. if(startlba < *endlba){
  897. disk = edit->disk;
  898. diskread(disk, &table, Tablesz, mbroffset+startlba, Toffset);
  899. tp = table.entry;
  900. ep = tp+NTentry;
  901. for(; tp<ep; tp++)
  902. memset(tp, 0, sizeof *tp);
  903. table.magic[0] = Magic0;
  904. table.magic[1] = Magic1;
  905. if(diskwrite(edit->disk, &table, Tablesz, mbroffset+startlba, Toffset) < 0)
  906. recover(edit);
  907. }
  908. return i;
  909. }
  910. p = (Dospart*)edit->part[i];
  911. if(p->primary){
  912. *endlba = (int64_t)p->start*sec2cyl;
  913. goto Finish;
  914. }
  915. disk = edit->disk;
  916. diskread(disk, &table, Tablesz, mbroffset+startlba, Toffset);
  917. tp = table.entry;
  918. ep = tp+NTentry;
  919. ni = wrextend(edit, i+1, xbase, p->end*sec2cyl, endlba);
  920. *tp = p->Tentry;
  921. wrtentry(disk, tp, p->type, startlba, startlba+disk->s, p->end*sec2cyl);
  922. tp++;
  923. if(p->end*sec2cyl != *endlba){
  924. memset(tp, 0, sizeof *tp);
  925. wrtentry(disk, tp, TypeEXTENDED, xbase, p->end*sec2cyl, *endlba);
  926. tp++;
  927. }
  928. for(; tp<ep; tp++)
  929. memset(tp, 0, sizeof *tp);
  930. table.magic[0] = Magic0;
  931. table.magic[1] = Magic1;
  932. if(diskwrite(edit->disk, &table, Tablesz, mbroffset+startlba, Toffset) < 0)
  933. recover(edit);
  934. return ni;
  935. }
  936. static void
  937. wrpart(Edit *edit)
  938. {
  939. int i, ni, t;
  940. Table table;
  941. Tentry *tp, *ep;
  942. Disk *disk;
  943. int64_t s, endlba;
  944. Dospart *p;
  945. disk = edit->disk;
  946. diskread(disk, &table, Tablesz, mbroffset, Toffset);
  947. tp = table.entry;
  948. ep = tp+NTentry;
  949. for(i=0; i<edit->npart && tp<ep; ) {
  950. p = (Dospart*)edit->part[i];
  951. if(p->start == 0)
  952. s = disk->s;
  953. else
  954. s = p->start*sec2cyl;
  955. if(p->primary) {
  956. *tp = p->Tentry;
  957. wrtentry(disk, tp, p->type, 0, s, p->end*sec2cyl);
  958. tp++;
  959. i++;
  960. } else {
  961. ni = wrextend(edit, i, p->start*sec2cyl, p->start*sec2cyl, &endlba);
  962. memset(tp, 0, sizeof *tp);
  963. if(endlba >= 1024*sec2cyl)
  964. t = TypeEXTHUGE;
  965. else
  966. t = TypeEXTENDED;
  967. wrtentry(disk, tp, t, 0, s, endlba);
  968. tp++;
  969. i = ni;
  970. }
  971. }
  972. for(; tp<ep; tp++)
  973. memset(tp, 0, sizeof(*tp));
  974. if(i != edit->npart)
  975. sysfatal("cannot happen #1");
  976. if(diskwrite(disk, &table, Tablesz, mbroffset, Toffset) < 0)
  977. recover(edit);
  978. /* bring parts up to date */
  979. freenamelist();
  980. for(i=0; i<edit->npart; i++)
  981. plan9print((Dospart*)edit->part[i], -1);
  982. if(ctldiff(edit, disk->ctlfd) < 0)
  983. fprint(2, "?warning: partitions could not be updated in devsd\n");
  984. }