fdisk_osf.c 29 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047
  1. /*
  2. * Copyright (c) 1987, 1988 Regents of the University of California.
  3. * All rights reserved.
  4. *
  5. * Redistribution and use in source and binary forms, with or without
  6. * modification, are permitted provided that the following conditions
  7. * are met:
  8. * 1. Redistributions of source code must retain the above copyright
  9. * notice, this list of conditions and the following disclaimer.
  10. * 2. Redistributions in binary form must reproduce the above copyright
  11. * notice, this list of conditions and the following disclaimer in the
  12. * documentation and/or other materials provided with the distribution.
  13. * 3. All advertising materials mentioning features or use of this software
  14. * must display the following acknowledgment:
  15. * This product includes software developed by the University of
  16. * California, Berkeley and its contributors.
  17. * 4. Neither the name of the University nor the names of its contributors
  18. * may be used to endorse or promote products derived from this software
  19. * without specific prior written permission.
  20. *
  21. * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
  22. * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
  23. * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
  24. * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
  25. * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
  26. * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
  27. * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
  28. * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
  29. * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
  30. * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
  31. * SUCH DAMAGE.
  32. */
  33. #if ENABLE_FEATURE_OSF_LABEL
  34. #ifndef BSD_DISKMAGIC
  35. #define BSD_DISKMAGIC ((uint32_t) 0x82564557)
  36. #endif
  37. #ifndef BSD_MAXPARTITIONS
  38. #define BSD_MAXPARTITIONS 16
  39. #endif
  40. #define BSD_LINUX_BOOTDIR "/usr/ucb/mdec"
  41. #if defined(__alpha__) \
  42. || defined(__powerpc__) \
  43. || defined(__ia64__) \
  44. || defined(__hppa__)
  45. # define BSD_LABELSECTOR 0
  46. # define BSD_LABELOFFSET 64
  47. #else
  48. # define BSD_LABELSECTOR 1
  49. # define BSD_LABELOFFSET 0
  50. #endif
  51. #define BSD_BBSIZE 8192 /* size of boot area, with label */
  52. #define BSD_SBSIZE 8192 /* max size of fs superblock */
  53. struct xbsd_disklabel {
  54. uint32_t d_magic; /* the magic number */
  55. int16_t d_type; /* drive type */
  56. int16_t d_subtype; /* controller/d_type specific */
  57. char d_typename[16]; /* type name, e.g. "eagle" */
  58. char d_packname[16]; /* pack identifier */
  59. /* disk geometry: */
  60. uint32_t d_secsize; /* # of bytes per sector */
  61. uint32_t d_nsectors; /* # of data sectors per track */
  62. uint32_t d_ntracks; /* # of tracks per cylinder */
  63. uint32_t d_ncylinders; /* # of data cylinders per unit */
  64. uint32_t d_secpercyl; /* # of data sectors per cylinder */
  65. uint32_t d_secperunit; /* # of data sectors per unit */
  66. /*
  67. * Spares (bad sector replacements) below
  68. * are not counted in d_nsectors or d_secpercyl.
  69. * Spare sectors are assumed to be physical sectors
  70. * which occupy space at the end of each track and/or cylinder.
  71. */
  72. uint16_t d_sparespertrack; /* # of spare sectors per track */
  73. uint16_t d_sparespercyl; /* # of spare sectors per cylinder */
  74. /*
  75. * Alternate cylinders include maintenance, replacement,
  76. * configuration description areas, etc.
  77. */
  78. uint32_t d_acylinders; /* # of alt. cylinders per unit */
  79. /* hardware characteristics: */
  80. /*
  81. * d_interleave, d_trackskew and d_cylskew describe perturbations
  82. * in the media format used to compensate for a slow controller.
  83. * Interleave is physical sector interleave, set up by the formatter
  84. * or controller when formatting. When interleaving is in use,
  85. * logically adjacent sectors are not physically contiguous,
  86. * but instead are separated by some number of sectors.
  87. * It is specified as the ratio of physical sectors traversed
  88. * per logical sector. Thus an interleave of 1:1 implies contiguous
  89. * layout, while 2:1 implies that logical sector 0 is separated
  90. * by one sector from logical sector 1.
  91. * d_trackskew is the offset of sector 0 on track N
  92. * relative to sector 0 on track N-1 on the same cylinder.
  93. * Finally, d_cylskew is the offset of sector 0 on cylinder N
  94. * relative to sector 0 on cylinder N-1.
  95. */
  96. uint16_t d_rpm; /* rotational speed */
  97. uint16_t d_interleave; /* hardware sector interleave */
  98. uint16_t d_trackskew; /* sector 0 skew, per track */
  99. uint16_t d_cylskew; /* sector 0 skew, per cylinder */
  100. uint32_t d_headswitch; /* head switch time, usec */
  101. uint32_t d_trkseek; /* track-to-track seek, usec */
  102. uint32_t d_flags; /* generic flags */
  103. #define NDDATA 5
  104. uint32_t d_drivedata[NDDATA]; /* drive-type specific information */
  105. #define NSPARE 5
  106. uint32_t d_spare[NSPARE]; /* reserved for future use */
  107. uint32_t d_magic2; /* the magic number (again) */
  108. uint16_t d_checksum; /* xor of data incl. partitions */
  109. /* filesystem and partition information: */
  110. uint16_t d_npartitions; /* number of partitions in following */
  111. uint32_t d_bbsize; /* size of boot area at sn0, bytes */
  112. uint32_t d_sbsize; /* max size of fs superblock, bytes */
  113. struct xbsd_partition { /* the partition table */
  114. uint32_t p_size; /* number of sectors in partition */
  115. uint32_t p_offset; /* starting sector */
  116. uint32_t p_fsize; /* filesystem basic fragment size */
  117. uint8_t p_fstype; /* filesystem type, see below */
  118. uint8_t p_frag; /* filesystem fragments per block */
  119. uint16_t p_cpg; /* filesystem cylinders per group */
  120. } d_partitions[BSD_MAXPARTITIONS]; /* actually may be more */
  121. };
  122. /* d_type values: */
  123. #define BSD_DTYPE_SMD 1 /* SMD, XSMD; VAX hp/up */
  124. #define BSD_DTYPE_MSCP 2 /* MSCP */
  125. #define BSD_DTYPE_DEC 3 /* other DEC (rk, rl) */
  126. #define BSD_DTYPE_SCSI 4 /* SCSI */
  127. #define BSD_DTYPE_ESDI 5 /* ESDI interface */
  128. #define BSD_DTYPE_ST506 6 /* ST506 etc. */
  129. #define BSD_DTYPE_HPIB 7 /* CS/80 on HP-IB */
  130. #define BSD_DTYPE_HPFL 8 /* HP Fiber-link */
  131. #define BSD_DTYPE_FLOPPY 10 /* floppy */
  132. /* d_subtype values: */
  133. #define BSD_DSTYPE_INDOSPART 0x8 /* is inside dos partition */
  134. #define BSD_DSTYPE_DOSPART(s) ((s) & 3) /* dos partition number */
  135. #define BSD_DSTYPE_GEOMETRY 0x10 /* drive params in label */
  136. static const char *const xbsd_dktypenames[] = {
  137. "unknown",
  138. "SMD",
  139. "MSCP",
  140. "old DEC",
  141. "SCSI",
  142. "ESDI",
  143. "ST506",
  144. "HP-IB",
  145. "HP-FL",
  146. "type 9",
  147. "floppy",
  148. 0
  149. };
  150. /*
  151. * Filesystem type and version.
  152. * Used to interpret other filesystem-specific
  153. * per-partition information.
  154. */
  155. #define BSD_FS_UNUSED 0 /* unused */
  156. #define BSD_FS_SWAP 1 /* swap */
  157. #define BSD_FS_V6 2 /* Sixth Edition */
  158. #define BSD_FS_V7 3 /* Seventh Edition */
  159. #define BSD_FS_SYSV 4 /* System V */
  160. #define BSD_FS_V71K 5 /* V7 with 1K blocks (4.1, 2.9) */
  161. #define BSD_FS_V8 6 /* Eighth Edition, 4K blocks */
  162. #define BSD_FS_BSDFFS 7 /* 4.2BSD fast file system */
  163. #define BSD_FS_BSDLFS 9 /* 4.4BSD log-structured file system */
  164. #define BSD_FS_OTHER 10 /* in use, but unknown/unsupported */
  165. #define BSD_FS_HPFS 11 /* OS/2 high-performance file system */
  166. #define BSD_FS_ISO9660 12 /* ISO-9660 filesystem (cdrom) */
  167. #define BSD_FS_ISOFS BSD_FS_ISO9660
  168. #define BSD_FS_BOOT 13 /* partition contains bootstrap */
  169. #define BSD_FS_ADOS 14 /* AmigaDOS fast file system */
  170. #define BSD_FS_HFS 15 /* Macintosh HFS */
  171. #define BSD_FS_ADVFS 16 /* Digital Unix AdvFS */
  172. /* this is annoying, but it's also the way it is :-( */
  173. #ifdef __alpha__
  174. #define BSD_FS_EXT2 8 /* ext2 file system */
  175. #else
  176. #define BSD_FS_MSDOS 8 /* MS-DOS file system */
  177. #endif
  178. static const char *const xbsd_fstypes[] = {
  179. "\x00" "unused", /* BSD_FS_UNUSED */
  180. "\x01" "swap", /* BSD_FS_SWAP */
  181. "\x02" "Version 6", /* BSD_FS_V6 */
  182. "\x03" "Version 7", /* BSD_FS_V7 */
  183. "\x04" "System V", /* BSD_FS_SYSV */
  184. "\x05" "4.1BSD", /* BSD_FS_V71K */
  185. "\x06" "Eighth Edition", /* BSD_FS_V8 */
  186. "\x07" "4.2BSD", /* BSD_FS_BSDFFS */
  187. #ifdef __alpha__
  188. "\x08" "ext2", /* BSD_FS_EXT2 */
  189. #else
  190. "\x08" "MS-DOS", /* BSD_FS_MSDOS */
  191. #endif
  192. "\x09" "4.4LFS", /* BSD_FS_BSDLFS */
  193. "\x0a" "unknown", /* BSD_FS_OTHER */
  194. "\x0b" "HPFS", /* BSD_FS_HPFS */
  195. "\x0c" "ISO-9660", /* BSD_FS_ISO9660 */
  196. "\x0d" "boot", /* BSD_FS_BOOT */
  197. "\x0e" "ADOS", /* BSD_FS_ADOS */
  198. "\x0f" "HFS", /* BSD_FS_HFS */
  199. "\x10" "AdvFS", /* BSD_FS_ADVFS */
  200. NULL
  201. };
  202. /*
  203. * flags shared by various drives:
  204. */
  205. #define BSD_D_REMOVABLE 0x01 /* removable media */
  206. #define BSD_D_ECC 0x02 /* supports ECC */
  207. #define BSD_D_BADSECT 0x04 /* supports bad sector forw. */
  208. #define BSD_D_RAMDISK 0x08 /* disk emulator */
  209. #define BSD_D_CHAIN 0x10 /* can do back-back transfers */
  210. #define BSD_D_DOSPART 0x20 /* within MSDOS partition */
  211. /*
  212. Changes:
  213. 19990319 - Arnaldo Carvalho de Melo <acme@conectiva.com.br> - i18n/nls
  214. 20000101 - David Huggins-Daines <dhuggins@linuxcare.com> - Better
  215. support for OSF/1 disklabels on Alpha.
  216. Also fixed unaligned accesses in alpha_bootblock_checksum()
  217. */
  218. #define FREEBSD_PARTITION 0xa5
  219. #define NETBSD_PARTITION 0xa9
  220. static void xbsd_delete_part(void);
  221. static void xbsd_new_part(void);
  222. static void xbsd_write_disklabel(void);
  223. static int xbsd_create_disklabel(void);
  224. static void xbsd_edit_disklabel(void);
  225. static void xbsd_write_bootstrap(void);
  226. static void xbsd_change_fstype(void);
  227. static int xbsd_get_part_index(int max);
  228. static int xbsd_check_new_partition(int *i);
  229. static void xbsd_list_types(void);
  230. static uint16_t xbsd_dkcksum(struct xbsd_disklabel *lp);
  231. static int xbsd_initlabel(struct partition *p);
  232. static int xbsd_readlabel(struct partition *p);
  233. static int xbsd_writelabel(struct partition *p);
  234. #if defined(__alpha__)
  235. static void alpha_bootblock_checksum(char *boot);
  236. #endif
  237. #if !defined(__alpha__)
  238. static int xbsd_translate_fstype(int linux_type);
  239. static void xbsd_link_part(void);
  240. static struct partition *xbsd_part;
  241. static int xbsd_part_index;
  242. #endif
  243. /* Group big globals data and allocate it in one go */
  244. struct bsd_globals {
  245. /* We access this through a uint64_t * when checksumming */
  246. /* hopefully xmalloc gives us required alignment */
  247. char disklabelbuffer[BSD_BBSIZE];
  248. struct xbsd_disklabel xbsd_dlabel;
  249. };
  250. static struct bsd_globals *bsd_globals_ptr;
  251. #define disklabelbuffer (bsd_globals_ptr->disklabelbuffer)
  252. #define xbsd_dlabel (bsd_globals_ptr->xbsd_dlabel)
  253. /* Code */
  254. #define bsd_cround(n) \
  255. (display_in_cyl_units ? ((n)/xbsd_dlabel.d_secpercyl) + 1 : (n))
  256. /*
  257. * Test whether the whole disk has BSD disk label magic.
  258. *
  259. * Note: often reformatting with DOS-type label leaves the BSD magic,
  260. * so this does not mean that there is a BSD disk label.
  261. */
  262. static int
  263. check_osf_label(void)
  264. {
  265. if (xbsd_readlabel(NULL) == 0)
  266. return 0;
  267. return 1;
  268. }
  269. static int
  270. bsd_trydev(const char * dev)
  271. {
  272. if (xbsd_readlabel(NULL) == 0)
  273. return -1;
  274. printf("\nBSD label for device: %s\n", dev);
  275. xbsd_print_disklabel(0);
  276. return 0;
  277. }
  278. static void
  279. bsd_menu(void)
  280. {
  281. puts("Command Action");
  282. puts("d\tdelete a BSD partition");
  283. puts("e\tedit drive data");
  284. puts("i\tinstall bootstrap");
  285. puts("l\tlist known filesystem types");
  286. puts("n\tadd a new BSD partition");
  287. puts("p\tprint BSD partition table");
  288. puts("q\tquit without saving changes");
  289. puts("r\treturn to main menu");
  290. puts("s\tshow complete disklabel");
  291. puts("t\tchange a partition's filesystem id");
  292. puts("u\tchange units (cylinders/sectors)");
  293. puts("w\twrite disklabel to disk");
  294. #if !defined(__alpha__)
  295. puts("x\tlink BSD partition to non-BSD partition");
  296. #endif
  297. }
  298. #if !defined(__alpha__)
  299. static int
  300. hidden(int type)
  301. {
  302. return type ^ 0x10;
  303. }
  304. static int
  305. is_bsd_partition_type(int type)
  306. {
  307. return (type == FREEBSD_PARTITION ||
  308. type == hidden(FREEBSD_PARTITION) ||
  309. type == NETBSD_PARTITION ||
  310. type == hidden(NETBSD_PARTITION));
  311. }
  312. #endif
  313. static void
  314. bsd_select(void)
  315. {
  316. #if !defined(__alpha__)
  317. int t, ss;
  318. struct partition *p;
  319. for (t = 0; t < 4; t++) {
  320. p = get_part_table(t);
  321. if (p && is_bsd_partition_type(p->sys_ind)) {
  322. xbsd_part = p;
  323. xbsd_part_index = t;
  324. ss = get_start_sect(xbsd_part);
  325. if (ss == 0) {
  326. printf("Partition %s has invalid starting sector 0\n",
  327. partname(disk_device, t+1, 0));
  328. return;
  329. }
  330. printf("Reading disklabel of %s at sector %u\n",
  331. partname(disk_device, t+1, 0), ss + BSD_LABELSECTOR);
  332. if (xbsd_readlabel(xbsd_part) == 0)
  333. if (xbsd_create_disklabel() == 0)
  334. return;
  335. break;
  336. }
  337. }
  338. if (t == 4) {
  339. printf("There is no *BSD partition on %s\n", disk_device);
  340. return;
  341. }
  342. #elif defined(__alpha__)
  343. if (xbsd_readlabel(NULL) == 0)
  344. if (xbsd_create_disklabel() == 0)
  345. exit(EXIT_SUCCESS);
  346. #endif
  347. while (1) {
  348. bb_putchar('\n');
  349. switch (tolower(read_nonempty("BSD disklabel command (m for help): "))) {
  350. case 'd':
  351. xbsd_delete_part();
  352. break;
  353. case 'e':
  354. xbsd_edit_disklabel();
  355. break;
  356. case 'i':
  357. xbsd_write_bootstrap();
  358. break;
  359. case 'l':
  360. xbsd_list_types();
  361. break;
  362. case 'n':
  363. xbsd_new_part();
  364. break;
  365. case 'p':
  366. xbsd_print_disklabel(0);
  367. break;
  368. case 'q':
  369. if (ENABLE_FEATURE_CLEAN_UP)
  370. close_dev_fd();
  371. exit(EXIT_SUCCESS);
  372. case 'r':
  373. return;
  374. case 's':
  375. xbsd_print_disklabel(1);
  376. break;
  377. case 't':
  378. xbsd_change_fstype();
  379. break;
  380. case 'u':
  381. change_units();
  382. break;
  383. case 'w':
  384. xbsd_write_disklabel();
  385. break;
  386. #if !defined(__alpha__)
  387. case 'x':
  388. xbsd_link_part();
  389. break;
  390. #endif
  391. default:
  392. bsd_menu();
  393. break;
  394. }
  395. }
  396. }
  397. static void
  398. xbsd_delete_part(void)
  399. {
  400. int i;
  401. i = xbsd_get_part_index(xbsd_dlabel.d_npartitions);
  402. xbsd_dlabel.d_partitions[i].p_size = 0;
  403. xbsd_dlabel.d_partitions[i].p_offset = 0;
  404. xbsd_dlabel.d_partitions[i].p_fstype = BSD_FS_UNUSED;
  405. if (xbsd_dlabel.d_npartitions == i + 1)
  406. while (xbsd_dlabel.d_partitions[xbsd_dlabel.d_npartitions-1].p_size == 0)
  407. xbsd_dlabel.d_npartitions--;
  408. }
  409. static void
  410. xbsd_new_part(void)
  411. {
  412. off_t begin, end;
  413. char mesg[256];
  414. int i;
  415. if (!xbsd_check_new_partition(&i))
  416. return;
  417. #if !defined(__alpha__) && !defined(__powerpc__) && !defined(__hppa__)
  418. begin = get_start_sect(xbsd_part);
  419. end = begin + get_nr_sects(xbsd_part) - 1;
  420. #else
  421. begin = 0;
  422. end = xbsd_dlabel.d_secperunit - 1;
  423. #endif
  424. snprintf(mesg, sizeof(mesg), "First %s", str_units(SINGULAR));
  425. begin = read_int(bsd_cround(begin), bsd_cround(begin), bsd_cround(end),
  426. 0, mesg);
  427. if (display_in_cyl_units)
  428. begin = (begin - 1) * xbsd_dlabel.d_secpercyl;
  429. snprintf(mesg, sizeof(mesg), "Last %s or +size or +sizeM or +sizeK",
  430. str_units(SINGULAR));
  431. end = read_int(bsd_cround(begin), bsd_cround(end), bsd_cround(end),
  432. bsd_cround(begin), mesg);
  433. if (display_in_cyl_units)
  434. end = end * xbsd_dlabel.d_secpercyl - 1;
  435. xbsd_dlabel.d_partitions[i].p_size = end - begin + 1;
  436. xbsd_dlabel.d_partitions[i].p_offset = begin;
  437. xbsd_dlabel.d_partitions[i].p_fstype = BSD_FS_UNUSED;
  438. }
  439. static void
  440. xbsd_print_disklabel(int show_all)
  441. {
  442. struct xbsd_disklabel *lp = &xbsd_dlabel;
  443. struct xbsd_partition *pp;
  444. int i, j;
  445. if (show_all) {
  446. static const int d_masks[] = { BSD_D_REMOVABLE, BSD_D_ECC, BSD_D_BADSECT };
  447. #if defined(__alpha__)
  448. printf("# %s:\n", disk_device);
  449. #else
  450. printf("# %s:\n", partname(disk_device, xbsd_part_index+1, 0));
  451. #endif
  452. if ((unsigned) lp->d_type < ARRAY_SIZE(xbsd_dktypenames)-1)
  453. printf("type: %s\n", xbsd_dktypenames[lp->d_type]);
  454. else
  455. printf("type: %u\n", lp->d_type);
  456. printf("disk: %.*s\n", (int) sizeof(lp->d_typename), lp->d_typename);
  457. printf("label: %.*s\n", (int) sizeof(lp->d_packname), lp->d_packname);
  458. printf("flags: ");
  459. print_flags_separated(d_masks, "removable\0""ecc\0""badsect\0", lp->d_flags, " ");
  460. bb_putchar('\n');
  461. /* On various machines the fields of *lp are short/int/long */
  462. /* In order to avoid problems, we cast them all to long. */
  463. printf("bytes/sector: %lu\n", (long) lp->d_secsize);
  464. printf("sectors/track: %lu\n", (long) lp->d_nsectors);
  465. printf("tracks/cylinder: %lu\n", (long) lp->d_ntracks);
  466. printf("sectors/cylinder: %lu\n", (long) lp->d_secpercyl);
  467. printf("cylinders: %lu\n", (long) lp->d_ncylinders);
  468. printf("rpm: %u\n", lp->d_rpm);
  469. printf("interleave: %u\n", lp->d_interleave);
  470. printf("trackskew: %u\n", lp->d_trackskew);
  471. printf("cylinderskew: %u\n", lp->d_cylskew);
  472. printf("headswitch: %lu\t\t# milliseconds\n",
  473. (long) lp->d_headswitch);
  474. printf("track-to-track seek: %lu\t# milliseconds\n",
  475. (long) lp->d_trkseek);
  476. printf("drivedata: ");
  477. for (i = NDDATA - 1; i >= 0; i--)
  478. if (lp->d_drivedata[i])
  479. break;
  480. if (i < 0)
  481. i = 0;
  482. for (j = 0; j <= i; j++)
  483. printf("%lu ", (long) lp->d_drivedata[j]);
  484. }
  485. printf("\n%u partitions:\n", lp->d_npartitions);
  486. printf("# start end size fstype [fsize bsize cpg]\n");
  487. pp = lp->d_partitions;
  488. for (i = 0; i < lp->d_npartitions; i++, pp++) {
  489. if (pp->p_size) {
  490. if (display_in_cyl_units && lp->d_secpercyl) {
  491. printf(" %c: %8lu%c %8lu%c %8lu%c ",
  492. 'a' + i,
  493. (unsigned long) pp->p_offset / lp->d_secpercyl + 1,
  494. (pp->p_offset % lp->d_secpercyl) ? '*' : ' ',
  495. (unsigned long) (pp->p_offset + pp->p_size + lp->d_secpercyl - 1) / lp->d_secpercyl,
  496. ((pp->p_offset + pp->p_size) % lp->d_secpercyl) ? '*' : ' ',
  497. (long) pp->p_size / lp->d_secpercyl,
  498. (pp->p_size % lp->d_secpercyl) ? '*' : ' '
  499. );
  500. } else {
  501. printf(" %c: %8lu %8lu %8lu ",
  502. 'a' + i,
  503. (long) pp->p_offset,
  504. (long) pp->p_offset + pp->p_size - 1,
  505. (long) pp->p_size
  506. );
  507. }
  508. if ((unsigned) pp->p_fstype < ARRAY_SIZE(xbsd_fstypes)-1)
  509. printf("%8.8s", xbsd_fstypes[pp->p_fstype]);
  510. else
  511. printf("%8x", pp->p_fstype);
  512. switch (pp->p_fstype) {
  513. case BSD_FS_UNUSED:
  514. printf(" %5lu %5lu %5.5s ",
  515. (long) pp->p_fsize, (long) pp->p_fsize * pp->p_frag, "");
  516. break;
  517. case BSD_FS_BSDFFS:
  518. printf(" %5lu %5lu %5u ",
  519. (long) pp->p_fsize, (long) pp->p_fsize * pp->p_frag, pp->p_cpg);
  520. break;
  521. default:
  522. printf("%22.22s", "");
  523. break;
  524. }
  525. bb_putchar('\n');
  526. }
  527. }
  528. }
  529. static void
  530. xbsd_write_disklabel(void)
  531. {
  532. #if defined(__alpha__)
  533. printf("Writing disklabel to %s\n", disk_device);
  534. xbsd_writelabel(NULL);
  535. #else
  536. printf("Writing disklabel to %s\n",
  537. partname(disk_device, xbsd_part_index + 1, 0));
  538. xbsd_writelabel(xbsd_part);
  539. #endif
  540. reread_partition_table(0); /* no exit yet */
  541. }
  542. static int
  543. xbsd_create_disklabel(void)
  544. {
  545. char c;
  546. #if defined(__alpha__)
  547. printf("%s contains no disklabel\n", disk_device);
  548. #else
  549. printf("%s contains no disklabel\n",
  550. partname(disk_device, xbsd_part_index + 1, 0));
  551. #endif
  552. while (1) {
  553. c = read_nonempty("Do you want to create a disklabel? (y/n) ");
  554. if ((c|0x20) == 'y') {
  555. if (xbsd_initlabel(
  556. #if defined(__alpha__) || defined(__powerpc__) || defined(__hppa__) || \
  557. defined(__s390__) || defined(__s390x__)
  558. NULL
  559. #else
  560. xbsd_part
  561. #endif
  562. ) == 1) {
  563. xbsd_print_disklabel(1);
  564. return 1;
  565. }
  566. return 0;
  567. }
  568. if ((c|0x20) == 'n')
  569. return 0;
  570. }
  571. }
  572. static int
  573. edit_int(int def, const char *mesg)
  574. {
  575. mesg = xasprintf("%s (%u): ", mesg, def);
  576. do {
  577. if (!read_line(mesg))
  578. goto ret;
  579. } while (!isdigit(*line_ptr));
  580. def = atoi(line_ptr);
  581. ret:
  582. free((char*)mesg);
  583. return def;
  584. }
  585. static void
  586. xbsd_edit_disklabel(void)
  587. {
  588. struct xbsd_disklabel *d;
  589. d = &xbsd_dlabel;
  590. #if defined(__alpha__) || defined(__ia64__)
  591. d->d_secsize = edit_int(d->d_secsize , "bytes/sector");
  592. d->d_nsectors = edit_int(d->d_nsectors , "sectors/track");
  593. d->d_ntracks = edit_int(d->d_ntracks , "tracks/cylinder");
  594. d->d_ncylinders = edit_int(d->d_ncylinders , "cylinders");
  595. #endif
  596. /* d->d_secpercyl can be != d->d_nsectors * d->d_ntracks */
  597. while (1) {
  598. d->d_secpercyl = edit_int(d->d_nsectors * d->d_ntracks,
  599. "sectors/cylinder");
  600. if (d->d_secpercyl <= d->d_nsectors * d->d_ntracks)
  601. break;
  602. printf("Must be <= sectors/track * tracks/cylinder (default)\n");
  603. }
  604. d->d_rpm = edit_int(d->d_rpm , "rpm");
  605. d->d_interleave = edit_int(d->d_interleave, "interleave");
  606. d->d_trackskew = edit_int(d->d_trackskew , "trackskew");
  607. d->d_cylskew = edit_int(d->d_cylskew , "cylinderskew");
  608. d->d_headswitch = edit_int(d->d_headswitch, "headswitch");
  609. d->d_trkseek = edit_int(d->d_trkseek , "track-to-track seek");
  610. d->d_secperunit = d->d_secpercyl * d->d_ncylinders;
  611. }
  612. static int
  613. xbsd_get_bootstrap(char *path, void *ptr, int size)
  614. {
  615. int fdb;
  616. fdb = open_or_warn(path, O_RDONLY);
  617. if (fdb < 0) {
  618. return 0;
  619. }
  620. if (full_read(fdb, ptr, size) < 0) {
  621. bb_simple_perror_msg(path);
  622. close(fdb);
  623. return 0;
  624. }
  625. printf(" ... %s\n", path);
  626. close(fdb);
  627. return 1;
  628. }
  629. static void
  630. sync_disks(void)
  631. {
  632. printf("Syncing disks\n");
  633. sync();
  634. /* sleep(4); What? */
  635. }
  636. static void
  637. xbsd_write_bootstrap(void)
  638. {
  639. char path[MAXPATHLEN];
  640. const char *bootdir = BSD_LINUX_BOOTDIR;
  641. const char *dkbasename;
  642. struct xbsd_disklabel dl;
  643. char *d, *p, *e;
  644. int sector;
  645. if (xbsd_dlabel.d_type == BSD_DTYPE_SCSI)
  646. dkbasename = "sd";
  647. else
  648. dkbasename = "wd";
  649. snprintf(path, sizeof(path), "Bootstrap: %sboot -> boot%s (%s): ",
  650. dkbasename, dkbasename, dkbasename);
  651. if (read_line(path)) {
  652. dkbasename = line_ptr;
  653. }
  654. snprintf(path, sizeof(path), "%s/%sboot", bootdir, dkbasename);
  655. if (!xbsd_get_bootstrap(path, disklabelbuffer, (int) xbsd_dlabel.d_secsize))
  656. return;
  657. /* We need a backup of the disklabel (xbsd_dlabel might have changed). */
  658. d = &disklabelbuffer[BSD_LABELSECTOR * SECTOR_SIZE];
  659. memmove(&dl, d, sizeof(struct xbsd_disklabel));
  660. /* The disklabel will be overwritten by 0's from bootxx anyway */
  661. memset(d, 0, sizeof(struct xbsd_disklabel));
  662. snprintf(path, sizeof(path), "%s/boot%s", bootdir, dkbasename);
  663. if (!xbsd_get_bootstrap(path, &disklabelbuffer[xbsd_dlabel.d_secsize],
  664. (int) xbsd_dlabel.d_bbsize - xbsd_dlabel.d_secsize))
  665. return;
  666. e = d + sizeof(struct xbsd_disklabel);
  667. for (p = d; p < e; p++)
  668. if (*p) {
  669. printf("Bootstrap overlaps with disk label!\n");
  670. exit(EXIT_FAILURE);
  671. }
  672. memmove(d, &dl, sizeof(struct xbsd_disklabel));
  673. #if defined(__powerpc__) || defined(__hppa__)
  674. sector = 0;
  675. #elif defined(__alpha__)
  676. sector = 0;
  677. alpha_bootblock_checksum(disklabelbuffer);
  678. #else
  679. sector = get_start_sect(xbsd_part);
  680. #endif
  681. seek_sector(sector);
  682. xwrite(dev_fd, disklabelbuffer, BSD_BBSIZE);
  683. #if defined(__alpha__)
  684. printf("Bootstrap installed on %s\n", disk_device);
  685. #else
  686. printf("Bootstrap installed on %s\n",
  687. partname(disk_device, xbsd_part_index+1, 0));
  688. #endif
  689. sync_disks();
  690. }
  691. static void
  692. xbsd_change_fstype(void)
  693. {
  694. int i;
  695. i = xbsd_get_part_index(xbsd_dlabel.d_npartitions);
  696. xbsd_dlabel.d_partitions[i].p_fstype = read_hex(xbsd_fstypes);
  697. }
  698. static int
  699. xbsd_get_part_index(int max)
  700. {
  701. char prompt[sizeof("Partition (a-%c): ") + 16];
  702. char l;
  703. snprintf(prompt, sizeof(prompt), "Partition (a-%c): ", 'a' + max - 1);
  704. do
  705. l = tolower(read_nonempty(prompt));
  706. while (l < 'a' || l > 'a' + max - 1);
  707. return l - 'a';
  708. }
  709. static int
  710. xbsd_check_new_partition(int *i)
  711. {
  712. /* room for more? various BSD flavours have different maxima */
  713. if (xbsd_dlabel.d_npartitions == BSD_MAXPARTITIONS) {
  714. int t;
  715. for (t = 0; t < BSD_MAXPARTITIONS; t++)
  716. if (xbsd_dlabel.d_partitions[t].p_size == 0)
  717. break;
  718. if (t == BSD_MAXPARTITIONS) {
  719. printf("The maximum number of partitions has been created\n");
  720. return 0;
  721. }
  722. }
  723. *i = xbsd_get_part_index(BSD_MAXPARTITIONS);
  724. if (*i >= xbsd_dlabel.d_npartitions)
  725. xbsd_dlabel.d_npartitions = (*i) + 1;
  726. if (xbsd_dlabel.d_partitions[*i].p_size != 0) {
  727. printf("This partition already exists\n");
  728. return 0;
  729. }
  730. return 1;
  731. }
  732. static void
  733. xbsd_list_types(void)
  734. {
  735. list_types(xbsd_fstypes);
  736. }
  737. static uint16_t
  738. xbsd_dkcksum(struct xbsd_disklabel *lp)
  739. {
  740. uint16_t *start, *end;
  741. uint16_t sum = 0;
  742. start = (uint16_t *) lp;
  743. end = (uint16_t *) &lp->d_partitions[lp->d_npartitions];
  744. while (start < end)
  745. sum ^= *start++;
  746. return sum;
  747. }
  748. static int
  749. xbsd_initlabel(struct partition *p)
  750. {
  751. struct xbsd_disklabel *d = &xbsd_dlabel;
  752. struct xbsd_partition *pp;
  753. get_geometry();
  754. memset(d, 0, sizeof(struct xbsd_disklabel));
  755. d->d_magic = BSD_DISKMAGIC;
  756. if (is_prefixed_with(disk_device, "/dev/sd"))
  757. d->d_type = BSD_DTYPE_SCSI;
  758. else
  759. d->d_type = BSD_DTYPE_ST506;
  760. #if !defined(__alpha__)
  761. d->d_flags = BSD_D_DOSPART;
  762. #else
  763. d->d_flags = 0;
  764. #endif
  765. d->d_secsize = SECTOR_SIZE; /* bytes/sector */
  766. d->d_nsectors = g_sectors; /* sectors/track */
  767. d->d_ntracks = g_heads; /* tracks/cylinder (heads) */
  768. d->d_ncylinders = g_cylinders;
  769. d->d_secpercyl = g_sectors * g_heads;/* sectors/cylinder */
  770. if (d->d_secpercyl == 0)
  771. d->d_secpercyl = 1; /* avoid segfaults */
  772. d->d_secperunit = d->d_secpercyl * d->d_ncylinders;
  773. d->d_rpm = 3600;
  774. d->d_interleave = 1;
  775. d->d_trackskew = 0;
  776. d->d_cylskew = 0;
  777. d->d_headswitch = 0;
  778. d->d_trkseek = 0;
  779. d->d_magic2 = BSD_DISKMAGIC;
  780. d->d_bbsize = BSD_BBSIZE;
  781. d->d_sbsize = BSD_SBSIZE;
  782. #if !defined(__alpha__)
  783. d->d_npartitions = 4;
  784. pp = &d->d_partitions[2]; /* Partition C should be NetBSD partition */
  785. pp->p_offset = get_start_sect(p);
  786. pp->p_size = get_nr_sects(p);
  787. pp->p_fstype = BSD_FS_UNUSED;
  788. pp = &d->d_partitions[3]; /* Partition D should be whole disk */
  789. pp->p_offset = 0;
  790. pp->p_size = d->d_secperunit;
  791. pp->p_fstype = BSD_FS_UNUSED;
  792. #else
  793. d->d_npartitions = 3;
  794. pp = &d->d_partitions[2]; /* Partition C should be the whole disk */
  795. pp->p_offset = 0;
  796. pp->p_size = d->d_secperunit;
  797. pp->p_fstype = BSD_FS_UNUSED;
  798. #endif
  799. return 1;
  800. }
  801. /*
  802. * Read a xbsd_disklabel from sector 0 or from the starting sector of p.
  803. * If it has the right magic, return 1.
  804. */
  805. static int
  806. xbsd_readlabel(struct partition *p)
  807. {
  808. struct xbsd_disklabel *d;
  809. int t, sector;
  810. if (!bsd_globals_ptr)
  811. bsd_globals_ptr = xzalloc(sizeof(*bsd_globals_ptr));
  812. d = &xbsd_dlabel;
  813. /* p is used only to get the starting sector */
  814. #if !defined(__alpha__)
  815. sector = (p ? get_start_sect(p) : 0);
  816. #else
  817. sector = 0;
  818. #endif
  819. seek_sector(sector);
  820. if (BSD_BBSIZE != full_read(dev_fd, disklabelbuffer, BSD_BBSIZE))
  821. fdisk_fatal(unable_to_read);
  822. memmove(d, &disklabelbuffer[BSD_LABELSECTOR * SECTOR_SIZE + BSD_LABELOFFSET],
  823. sizeof(struct xbsd_disklabel));
  824. if (d->d_magic != BSD_DISKMAGIC || d->d_magic2 != BSD_DISKMAGIC)
  825. return 0;
  826. for (t = d->d_npartitions; t < BSD_MAXPARTITIONS; t++) {
  827. d->d_partitions[t].p_size = 0;
  828. d->d_partitions[t].p_offset = 0;
  829. d->d_partitions[t].p_fstype = BSD_FS_UNUSED;
  830. }
  831. if (d->d_npartitions > BSD_MAXPARTITIONS)
  832. printf("Warning: too many partitions (%u, maximum is %u)\n",
  833. d->d_npartitions, BSD_MAXPARTITIONS);
  834. return 1;
  835. }
  836. static int
  837. xbsd_writelabel(struct partition *p)
  838. {
  839. struct xbsd_disklabel *d = &xbsd_dlabel;
  840. unsigned int sector;
  841. #if !defined(__alpha__) && !defined(__powerpc__) && !defined(__hppa__)
  842. sector = get_start_sect(p) + BSD_LABELSECTOR;
  843. #else
  844. (void)p; /* silence warning */
  845. sector = BSD_LABELSECTOR;
  846. #endif
  847. d->d_checksum = 0;
  848. d->d_checksum = xbsd_dkcksum(d);
  849. /* This is necessary if we want to write the bootstrap later,
  850. otherwise we'd write the old disklabel with the bootstrap.
  851. */
  852. memmove(&disklabelbuffer[BSD_LABELSECTOR * SECTOR_SIZE + BSD_LABELOFFSET],
  853. d, sizeof(struct xbsd_disklabel));
  854. #if defined(__alpha__) && BSD_LABELSECTOR == 0
  855. alpha_bootblock_checksum(disklabelbuffer);
  856. seek_sector(0);
  857. xwrite(dev_fd, disklabelbuffer, BSD_BBSIZE);
  858. #else
  859. seek_sector(sector);
  860. lseek(dev_fd, BSD_LABELOFFSET, SEEK_CUR);
  861. xwrite(dev_fd, d, sizeof(*d));
  862. #endif
  863. sync_disks();
  864. return 1;
  865. }
  866. #if !defined(__alpha__)
  867. static int
  868. xbsd_translate_fstype(int linux_type)
  869. {
  870. switch (linux_type) {
  871. case 0x01: /* DOS 12-bit FAT */
  872. case 0x04: /* DOS 16-bit <32M */
  873. case 0x06: /* DOS 16-bit >=32M */
  874. case 0xe1: /* DOS access */
  875. case 0xe3: /* DOS R/O */
  876. case 0xf2: /* DOS secondary */
  877. return BSD_FS_MSDOS;
  878. case 0x07: /* OS/2 HPFS */
  879. return BSD_FS_HPFS;
  880. default:
  881. return BSD_FS_OTHER;
  882. }
  883. }
  884. static void
  885. xbsd_link_part(void)
  886. {
  887. int k, i;
  888. struct partition *p;
  889. k = get_partition(1, g_partitions);
  890. if (!xbsd_check_new_partition(&i))
  891. return;
  892. p = get_part_table(k);
  893. xbsd_dlabel.d_partitions[i].p_size = get_nr_sects(p);
  894. xbsd_dlabel.d_partitions[i].p_offset = get_start_sect(p);
  895. xbsd_dlabel.d_partitions[i].p_fstype = xbsd_translate_fstype(p->sys_ind);
  896. }
  897. #endif
  898. #if defined(__alpha__)
  899. static void
  900. alpha_bootblock_checksum(char *boot)
  901. {
  902. uint64_t *dp, sum;
  903. int i;
  904. dp = (uint64_t *)boot;
  905. sum = 0;
  906. for (i = 0; i < 63; i++)
  907. sum += dp[i];
  908. dp[63] = sum;
  909. }
  910. #endif /* __alpha__ */
  911. /* Undefine 'global' tricks */
  912. #undef disklabelbuffer
  913. #undef xbsd_dlabel
  914. #endif /* OSF_LABEL */