fdisk_osf.c 29 KB

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