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. 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 (strncmp(disk_device, "/dev/sd", 7) == 0)
  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
  795. 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 */