3
0

fdisk_osf.c 29 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048
  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. }
  339. if (t == 4) {
  340. printf("There is no *BSD partition on %s\n", disk_device);
  341. return;
  342. }
  343. #elif defined(__alpha__)
  344. if (xbsd_readlabel(NULL) == 0)
  345. if (xbsd_create_disklabel() == 0)
  346. exit(EXIT_SUCCESS);
  347. #endif
  348. while (1) {
  349. bb_putchar('\n');
  350. switch (tolower(read_nonempty("BSD disklabel command (m for help): "))) {
  351. case 'd':
  352. xbsd_delete_part();
  353. break;
  354. case 'e':
  355. xbsd_edit_disklabel();
  356. break;
  357. case 'i':
  358. xbsd_write_bootstrap();
  359. break;
  360. case 'l':
  361. xbsd_list_types();
  362. break;
  363. case 'n':
  364. xbsd_new_part();
  365. break;
  366. case 'p':
  367. xbsd_print_disklabel(0);
  368. break;
  369. case 'q':
  370. if (ENABLE_FEATURE_CLEAN_UP)
  371. close_dev_fd();
  372. exit(EXIT_SUCCESS);
  373. case 'r':
  374. return;
  375. case 's':
  376. xbsd_print_disklabel(1);
  377. break;
  378. case 't':
  379. xbsd_change_fstype();
  380. break;
  381. case 'u':
  382. change_units();
  383. break;
  384. case 'w':
  385. xbsd_write_disklabel();
  386. break;
  387. #if !defined(__alpha__)
  388. case 'x':
  389. xbsd_link_part();
  390. break;
  391. #endif
  392. default:
  393. bsd_menu();
  394. break;
  395. }
  396. }
  397. }
  398. static void
  399. xbsd_delete_part(void)
  400. {
  401. int i;
  402. i = xbsd_get_part_index(xbsd_dlabel.d_npartitions);
  403. xbsd_dlabel.d_partitions[i].p_size = 0;
  404. xbsd_dlabel.d_partitions[i].p_offset = 0;
  405. xbsd_dlabel.d_partitions[i].p_fstype = BSD_FS_UNUSED;
  406. if (xbsd_dlabel.d_npartitions == i + 1)
  407. while (xbsd_dlabel.d_partitions[xbsd_dlabel.d_npartitions-1].p_size == 0)
  408. xbsd_dlabel.d_npartitions--;
  409. }
  410. static void
  411. xbsd_new_part(void)
  412. {
  413. off_t begin, end;
  414. char mesg[256];
  415. int i;
  416. if (!xbsd_check_new_partition(&i))
  417. return;
  418. #if !defined(__alpha__) && !defined(__powerpc__) && !defined(__hppa__)
  419. begin = get_start_sect(xbsd_part);
  420. end = begin + get_nr_sects(xbsd_part) - 1;
  421. #else
  422. begin = 0;
  423. end = xbsd_dlabel.d_secperunit - 1;
  424. #endif
  425. snprintf(mesg, sizeof(mesg), "First %s", str_units(SINGULAR));
  426. begin = read_int(bsd_cround(begin), bsd_cround(begin), bsd_cround(end),
  427. 0, mesg);
  428. if (display_in_cyl_units)
  429. begin = (begin - 1) * xbsd_dlabel.d_secpercyl;
  430. snprintf(mesg, sizeof(mesg), "Last %s or +size or +sizeM or +sizeK",
  431. str_units(SINGULAR));
  432. end = read_int(bsd_cround(begin), bsd_cround(end), bsd_cround(end),
  433. bsd_cround(begin), mesg);
  434. if (display_in_cyl_units)
  435. end = end * xbsd_dlabel.d_secpercyl - 1;
  436. xbsd_dlabel.d_partitions[i].p_size = end - begin + 1;
  437. xbsd_dlabel.d_partitions[i].p_offset = begin;
  438. xbsd_dlabel.d_partitions[i].p_fstype = BSD_FS_UNUSED;
  439. }
  440. static void
  441. xbsd_print_disklabel(int show_all)
  442. {
  443. struct xbsd_disklabel *lp = &xbsd_dlabel;
  444. struct xbsd_partition *pp;
  445. int i, j;
  446. if (show_all) {
  447. static const int d_masks[] = { BSD_D_REMOVABLE, BSD_D_ECC, BSD_D_BADSECT };
  448. #if defined(__alpha__)
  449. printf("# %s:\n", disk_device);
  450. #else
  451. printf("# %s:\n", partname(disk_device, xbsd_part_index+1, 0));
  452. #endif
  453. if ((unsigned) lp->d_type < ARRAY_SIZE(xbsd_dktypenames)-1)
  454. printf("type: %s\n", xbsd_dktypenames[lp->d_type]);
  455. else
  456. printf("type: %u\n", lp->d_type);
  457. printf("disk: %.*s\n", (int) sizeof(lp->d_typename), lp->d_typename);
  458. printf("label: %.*s\n", (int) sizeof(lp->d_packname), lp->d_packname);
  459. printf("flags: ");
  460. print_flags_separated(d_masks, "removable\0""ecc\0""badsect\0", lp->d_flags, " ");
  461. bb_putchar('\n');
  462. /* On various machines the fields of *lp are short/int/long */
  463. /* In order to avoid problems, we cast them all to long. */
  464. printf("bytes/sector: %lu\n", (long) lp->d_secsize);
  465. printf("sectors/track: %lu\n", (long) lp->d_nsectors);
  466. printf("tracks/cylinder: %lu\n", (long) lp->d_ntracks);
  467. printf("sectors/cylinder: %lu\n", (long) lp->d_secpercyl);
  468. printf("cylinders: %lu\n", (long) lp->d_ncylinders);
  469. printf("rpm: %u\n", lp->d_rpm);
  470. printf("interleave: %u\n", lp->d_interleave);
  471. printf("trackskew: %u\n", lp->d_trackskew);
  472. printf("cylinderskew: %u\n", lp->d_cylskew);
  473. printf("headswitch: %lu\t\t# milliseconds\n",
  474. (long) lp->d_headswitch);
  475. printf("track-to-track seek: %lu\t# milliseconds\n",
  476. (long) lp->d_trkseek);
  477. printf("drivedata: ");
  478. for (i = NDDATA - 1; i >= 0; i--)
  479. if (lp->d_drivedata[i])
  480. break;
  481. if (i < 0)
  482. i = 0;
  483. for (j = 0; j <= i; j++)
  484. printf("%lu ", (long) lp->d_drivedata[j]);
  485. }
  486. printf("\n%u partitions:\n", lp->d_npartitions);
  487. printf("# start end size fstype [fsize bsize cpg]\n");
  488. pp = lp->d_partitions;
  489. for (i = 0; i < lp->d_npartitions; i++, pp++) {
  490. if (pp->p_size) {
  491. if (display_in_cyl_units && lp->d_secpercyl) {
  492. printf(" %c: %8lu%c %8lu%c %8lu%c ",
  493. 'a' + i,
  494. (unsigned long) pp->p_offset / lp->d_secpercyl + 1,
  495. (pp->p_offset % lp->d_secpercyl) ? '*' : ' ',
  496. (unsigned long) (pp->p_offset + pp->p_size + lp->d_secpercyl - 1) / lp->d_secpercyl,
  497. ((pp->p_offset + pp->p_size) % lp->d_secpercyl) ? '*' : ' ',
  498. (long) pp->p_size / lp->d_secpercyl,
  499. (pp->p_size % lp->d_secpercyl) ? '*' : ' '
  500. );
  501. } else {
  502. printf(" %c: %8lu %8lu %8lu ",
  503. 'a' + i,
  504. (long) pp->p_offset,
  505. (long) pp->p_offset + pp->p_size - 1,
  506. (long) pp->p_size
  507. );
  508. }
  509. if ((unsigned) pp->p_fstype < ARRAY_SIZE(xbsd_fstypes)-1)
  510. printf("%8.8s", xbsd_fstypes[pp->p_fstype]);
  511. else
  512. printf("%8x", pp->p_fstype);
  513. switch (pp->p_fstype) {
  514. case BSD_FS_UNUSED:
  515. printf(" %5lu %5lu %5.5s ",
  516. (long) pp->p_fsize, (long) pp->p_fsize * pp->p_frag, "");
  517. break;
  518. case BSD_FS_BSDFFS:
  519. printf(" %5lu %5lu %5u ",
  520. (long) pp->p_fsize, (long) pp->p_fsize * pp->p_frag, pp->p_cpg);
  521. break;
  522. default:
  523. printf("%22.22s", "");
  524. break;
  525. }
  526. bb_putchar('\n');
  527. }
  528. }
  529. }
  530. static void
  531. xbsd_write_disklabel(void)
  532. {
  533. #if defined(__alpha__)
  534. printf("Writing disklabel to %s\n", disk_device);
  535. xbsd_writelabel(NULL);
  536. #else
  537. printf("Writing disklabel to %s\n",
  538. partname(disk_device, xbsd_part_index + 1, 0));
  539. xbsd_writelabel(xbsd_part);
  540. #endif
  541. reread_partition_table(0); /* no exit yet */
  542. }
  543. static int
  544. xbsd_create_disklabel(void)
  545. {
  546. char c;
  547. #if defined(__alpha__)
  548. printf("%s contains no disklabel\n", disk_device);
  549. #else
  550. printf("%s contains no disklabel\n",
  551. partname(disk_device, xbsd_part_index + 1, 0));
  552. #endif
  553. while (1) {
  554. c = read_nonempty("Do you want to create a disklabel? (y/n) ");
  555. if ((c|0x20) == 'y') {
  556. if (xbsd_initlabel(
  557. #if defined(__alpha__) || defined(__powerpc__) || defined(__hppa__) || \
  558. defined(__s390__) || defined(__s390x__)
  559. NULL
  560. #else
  561. xbsd_part
  562. #endif
  563. ) == 1) {
  564. xbsd_print_disklabel(1);
  565. return 1;
  566. }
  567. return 0;
  568. }
  569. if ((c|0x20) == 'n')
  570. return 0;
  571. }
  572. }
  573. static int
  574. edit_int(int def, const char *mesg)
  575. {
  576. mesg = xasprintf("%s (%u): ", mesg, def);
  577. do {
  578. if (!read_line(mesg))
  579. goto ret;
  580. } while (!isdigit(*line_ptr));
  581. def = atoi(line_ptr);
  582. ret:
  583. free((char*)mesg);
  584. return def;
  585. }
  586. static void
  587. xbsd_edit_disklabel(void)
  588. {
  589. struct xbsd_disklabel *d;
  590. d = &xbsd_dlabel;
  591. #if defined(__alpha__) || defined(__ia64__)
  592. d->d_secsize = edit_int(d->d_secsize , "bytes/sector");
  593. d->d_nsectors = edit_int(d->d_nsectors , "sectors/track");
  594. d->d_ntracks = edit_int(d->d_ntracks , "tracks/cylinder");
  595. d->d_ncylinders = edit_int(d->d_ncylinders , "cylinders");
  596. #endif
  597. /* d->d_secpercyl can be != d->d_nsectors * d->d_ntracks */
  598. while (1) {
  599. d->d_secpercyl = edit_int(d->d_nsectors * d->d_ntracks,
  600. "sectors/cylinder");
  601. if (d->d_secpercyl <= d->d_nsectors * d->d_ntracks)
  602. break;
  603. printf("Must be <= sectors/track * tracks/cylinder (default)\n");
  604. }
  605. d->d_rpm = edit_int(d->d_rpm , "rpm");
  606. d->d_interleave = edit_int(d->d_interleave, "interleave");
  607. d->d_trackskew = edit_int(d->d_trackskew , "trackskew");
  608. d->d_cylskew = edit_int(d->d_cylskew , "cylinderskew");
  609. d->d_headswitch = edit_int(d->d_headswitch, "headswitch");
  610. d->d_trkseek = edit_int(d->d_trkseek , "track-to-track seek");
  611. d->d_secperunit = d->d_secpercyl * d->d_ncylinders;
  612. }
  613. static int
  614. xbsd_get_bootstrap(char *path, void *ptr, int size)
  615. {
  616. int fdb;
  617. fdb = open_or_warn(path, O_RDONLY);
  618. if (fdb < 0) {
  619. return 0;
  620. }
  621. if (full_read(fdb, ptr, size) < 0) {
  622. bb_simple_perror_msg(path);
  623. close(fdb);
  624. return 0;
  625. }
  626. printf(" ... %s\n", path);
  627. close(fdb);
  628. return 1;
  629. }
  630. static void
  631. sync_disks(void)
  632. {
  633. printf("Syncing disks\n");
  634. sync();
  635. /* sleep(4); What? */
  636. }
  637. static void
  638. xbsd_write_bootstrap(void)
  639. {
  640. char path[MAXPATHLEN];
  641. const char *bootdir = BSD_LINUX_BOOTDIR;
  642. const char *dkbasename;
  643. struct xbsd_disklabel dl;
  644. char *d, *p, *e;
  645. int sector;
  646. if (xbsd_dlabel.d_type == BSD_DTYPE_SCSI)
  647. dkbasename = "sd";
  648. else
  649. dkbasename = "wd";
  650. snprintf(path, sizeof(path), "Bootstrap: %sboot -> boot%s (%s): ",
  651. dkbasename, dkbasename, dkbasename);
  652. if (read_line(path)) {
  653. dkbasename = line_ptr;
  654. }
  655. snprintf(path, sizeof(path), "%s/%sboot", bootdir, dkbasename);
  656. if (!xbsd_get_bootstrap(path, disklabelbuffer, (int) xbsd_dlabel.d_secsize))
  657. return;
  658. /* We need a backup of the disklabel (xbsd_dlabel might have changed). */
  659. d = &disklabelbuffer[BSD_LABELSECTOR * SECTOR_SIZE];
  660. memmove(&dl, d, sizeof(struct xbsd_disklabel));
  661. /* The disklabel will be overwritten by 0's from bootxx anyway */
  662. memset(d, 0, sizeof(struct xbsd_disklabel));
  663. snprintf(path, sizeof(path), "%s/boot%s", bootdir, dkbasename);
  664. if (!xbsd_get_bootstrap(path, &disklabelbuffer[xbsd_dlabel.d_secsize],
  665. (int) xbsd_dlabel.d_bbsize - xbsd_dlabel.d_secsize))
  666. return;
  667. e = d + sizeof(struct xbsd_disklabel);
  668. for (p = d; p < e; p++)
  669. if (*p) {
  670. printf("Bootstrap overlaps with disk label!\n");
  671. exit(EXIT_FAILURE);
  672. }
  673. memmove(d, &dl, sizeof(struct xbsd_disklabel));
  674. #if defined(__powerpc__) || defined(__hppa__)
  675. sector = 0;
  676. #elif defined(__alpha__)
  677. sector = 0;
  678. alpha_bootblock_checksum(disklabelbuffer);
  679. #else
  680. sector = get_start_sect(xbsd_part);
  681. #endif
  682. seek_sector(sector);
  683. xwrite(dev_fd, disklabelbuffer, BSD_BBSIZE);
  684. #if defined(__alpha__)
  685. printf("Bootstrap installed on %s\n", disk_device);
  686. #else
  687. printf("Bootstrap installed on %s\n",
  688. partname(disk_device, xbsd_part_index+1, 0));
  689. #endif
  690. sync_disks();
  691. }
  692. static void
  693. xbsd_change_fstype(void)
  694. {
  695. int i;
  696. i = xbsd_get_part_index(xbsd_dlabel.d_npartitions);
  697. xbsd_dlabel.d_partitions[i].p_fstype = read_hex(xbsd_fstypes);
  698. }
  699. static int
  700. xbsd_get_part_index(int max)
  701. {
  702. char prompt[sizeof("Partition (a-%c): ") + 16];
  703. char l;
  704. snprintf(prompt, sizeof(prompt), "Partition (a-%c): ", 'a' + max - 1);
  705. do
  706. l = tolower(read_nonempty(prompt));
  707. while (l < 'a' || l > 'a' + max - 1);
  708. return l - 'a';
  709. }
  710. static int
  711. xbsd_check_new_partition(int *i)
  712. {
  713. /* room for more? various BSD flavours have different maxima */
  714. if (xbsd_dlabel.d_npartitions == BSD_MAXPARTITIONS) {
  715. int t;
  716. for (t = 0; t < BSD_MAXPARTITIONS; t++)
  717. if (xbsd_dlabel.d_partitions[t].p_size == 0)
  718. break;
  719. if (t == BSD_MAXPARTITIONS) {
  720. printf("The maximum number of partitions has been created\n");
  721. return 0;
  722. }
  723. }
  724. *i = xbsd_get_part_index(BSD_MAXPARTITIONS);
  725. if (*i >= xbsd_dlabel.d_npartitions)
  726. xbsd_dlabel.d_npartitions = (*i) + 1;
  727. if (xbsd_dlabel.d_partitions[*i].p_size != 0) {
  728. printf("This partition already exists\n");
  729. return 0;
  730. }
  731. return 1;
  732. }
  733. static void
  734. xbsd_list_types(void)
  735. {
  736. list_types(xbsd_fstypes);
  737. }
  738. static uint16_t
  739. xbsd_dkcksum(struct xbsd_disklabel *lp)
  740. {
  741. uint16_t *start, *end;
  742. uint16_t sum = 0;
  743. start = (uint16_t *) lp;
  744. end = (uint16_t *) &lp->d_partitions[lp->d_npartitions];
  745. while (start < end)
  746. sum ^= *start++;
  747. return sum;
  748. }
  749. static int
  750. xbsd_initlabel(struct partition *p)
  751. {
  752. struct xbsd_disklabel *d = &xbsd_dlabel;
  753. struct xbsd_partition *pp;
  754. get_geometry();
  755. memset(d, 0, sizeof(struct xbsd_disklabel));
  756. d->d_magic = BSD_DISKMAGIC;
  757. if (is_prefixed_with(disk_device, "/dev/sd"))
  758. d->d_type = BSD_DTYPE_SCSI;
  759. else
  760. d->d_type = BSD_DTYPE_ST506;
  761. #if !defined(__alpha__)
  762. d->d_flags = BSD_D_DOSPART;
  763. #else
  764. d->d_flags = 0;
  765. #endif
  766. d->d_secsize = SECTOR_SIZE; /* bytes/sector */
  767. d->d_nsectors = g_sectors; /* sectors/track */
  768. d->d_ntracks = g_heads; /* tracks/cylinder (heads) */
  769. d->d_ncylinders = g_cylinders;
  770. d->d_secpercyl = g_sectors * g_heads;/* sectors/cylinder */
  771. if (d->d_secpercyl == 0)
  772. d->d_secpercyl = 1; /* avoid segfaults */
  773. d->d_secperunit = d->d_secpercyl * d->d_ncylinders;
  774. d->d_rpm = 3600;
  775. d->d_interleave = 1;
  776. d->d_trackskew = 0;
  777. d->d_cylskew = 0;
  778. d->d_headswitch = 0;
  779. d->d_trkseek = 0;
  780. d->d_magic2 = BSD_DISKMAGIC;
  781. d->d_bbsize = BSD_BBSIZE;
  782. d->d_sbsize = BSD_SBSIZE;
  783. #if !defined(__alpha__)
  784. d->d_npartitions = 4;
  785. pp = &d->d_partitions[2]; /* Partition C should be NetBSD partition */
  786. pp->p_offset = get_start_sect(p);
  787. pp->p_size = get_nr_sects(p);
  788. pp->p_fstype = BSD_FS_UNUSED;
  789. pp = &d->d_partitions[3]; /* Partition D should be whole disk */
  790. pp->p_offset = 0;
  791. pp->p_size = d->d_secperunit;
  792. pp->p_fstype = BSD_FS_UNUSED;
  793. #else
  794. d->d_npartitions = 3;
  795. pp = &d->d_partitions[2]; /* Partition C should be the whole disk */
  796. pp->p_offset = 0;
  797. pp->p_size = d->d_secperunit;
  798. pp->p_fstype = BSD_FS_UNUSED;
  799. #endif
  800. return 1;
  801. }
  802. /*
  803. * Read a xbsd_disklabel from sector 0 or from the starting sector of p.
  804. * If it has the right magic, return 1.
  805. */
  806. static int
  807. xbsd_readlabel(struct partition *p)
  808. {
  809. struct xbsd_disklabel *d;
  810. int t, sector;
  811. if (!bsd_globals_ptr)
  812. bsd_globals_ptr = xzalloc(sizeof(*bsd_globals_ptr));
  813. d = &xbsd_dlabel;
  814. /* p is used only to get the starting sector */
  815. #if !defined(__alpha__)
  816. sector = (p ? get_start_sect(p) : 0);
  817. #else
  818. sector = 0;
  819. #endif
  820. seek_sector(sector);
  821. if (BSD_BBSIZE != full_read(dev_fd, disklabelbuffer, BSD_BBSIZE))
  822. fdisk_fatal(unable_to_read);
  823. memmove(d, &disklabelbuffer[BSD_LABELSECTOR * SECTOR_SIZE + BSD_LABELOFFSET],
  824. sizeof(struct xbsd_disklabel));
  825. if (d->d_magic != BSD_DISKMAGIC || d->d_magic2 != BSD_DISKMAGIC)
  826. return 0;
  827. for (t = d->d_npartitions; t < BSD_MAXPARTITIONS; t++) {
  828. d->d_partitions[t].p_size = 0;
  829. d->d_partitions[t].p_offset = 0;
  830. d->d_partitions[t].p_fstype = BSD_FS_UNUSED;
  831. }
  832. if (d->d_npartitions > BSD_MAXPARTITIONS)
  833. printf("Warning: too many partitions (%u, maximum is %u)\n",
  834. d->d_npartitions, BSD_MAXPARTITIONS);
  835. return 1;
  836. }
  837. static int
  838. xbsd_writelabel(struct partition *p)
  839. {
  840. struct xbsd_disklabel *d = &xbsd_dlabel;
  841. unsigned int sector;
  842. #if !defined(__alpha__) && !defined(__powerpc__) && !defined(__hppa__)
  843. sector = get_start_sect(p) + BSD_LABELSECTOR;
  844. #else
  845. (void)p; /* silence warning */
  846. sector = BSD_LABELSECTOR;
  847. #endif
  848. d->d_checksum = 0;
  849. d->d_checksum = xbsd_dkcksum(d);
  850. /* This is necessary if we want to write the bootstrap later,
  851. otherwise we'd write the old disklabel with the bootstrap.
  852. */
  853. memmove(&disklabelbuffer[BSD_LABELSECTOR * SECTOR_SIZE + BSD_LABELOFFSET],
  854. d, sizeof(struct xbsd_disklabel));
  855. #if defined(__alpha__) && BSD_LABELSECTOR == 0
  856. alpha_bootblock_checksum(disklabelbuffer);
  857. seek_sector(0);
  858. xwrite(dev_fd, disklabelbuffer, BSD_BBSIZE);
  859. #else
  860. seek_sector(sector);
  861. lseek(dev_fd, BSD_LABELOFFSET, SEEK_CUR);
  862. xwrite(dev_fd, d, sizeof(*d));
  863. #endif
  864. sync_disks();
  865. return 1;
  866. }
  867. #if !defined(__alpha__)
  868. static int
  869. xbsd_translate_fstype(int linux_type)
  870. {
  871. switch (linux_type) {
  872. case 0x01: /* DOS 12-bit FAT */
  873. case 0x04: /* DOS 16-bit <32M */
  874. case 0x06: /* DOS 16-bit >=32M */
  875. case 0xe1: /* DOS access */
  876. case 0xe3: /* DOS R/O */
  877. case 0xf2: /* DOS secondary */
  878. return BSD_FS_MSDOS;
  879. case 0x07: /* OS/2 HPFS */
  880. return BSD_FS_HPFS;
  881. default:
  882. return BSD_FS_OTHER;
  883. }
  884. }
  885. static void
  886. xbsd_link_part(void)
  887. {
  888. int k, i;
  889. struct partition *p;
  890. k = get_partition(1, g_partitions);
  891. if (!xbsd_check_new_partition(&i))
  892. return;
  893. p = get_part_table(k);
  894. xbsd_dlabel.d_partitions[i].p_size = get_nr_sects(p);
  895. xbsd_dlabel.d_partitions[i].p_offset = get_start_sect(p);
  896. xbsd_dlabel.d_partitions[i].p_fstype = xbsd_translate_fstype(p->sys_ind);
  897. }
  898. #endif
  899. #if defined(__alpha__)
  900. static void
  901. alpha_bootblock_checksum(char *boot)
  902. {
  903. uint64_t *dp, sum;
  904. int i;
  905. dp = (uint64_t *)boot;
  906. sum = 0;
  907. for (i = 0; i < 63; i++)
  908. sum += dp[i];
  909. dp[63] = sum;
  910. }
  911. #endif /* __alpha__ */
  912. /* Undefine 'global' tricks */
  913. #undef disklabelbuffer
  914. #undef xbsd_dlabel
  915. #endif /* OSF_LABEL */