fdisk_osf.c 29 KB

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