fdisk_sgi.c 25 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892
  1. #if ENABLE_FEATURE_SGI_LABEL
  2. #define SGI_DEBUG 0
  3. /*
  4. * Copyright (C) Andreas Neuper, Sep 1998.
  5. * This file may be modified and redistributed under
  6. * the terms of the GNU Public License.
  7. */
  8. #define SGI_VOLHDR 0x00
  9. /* 1 and 2 were used for drive types no longer supported by SGI */
  10. #define SGI_SWAP 0x03
  11. /* 4 and 5 were for filesystem types SGI haven't ever supported on MIPS CPUs */
  12. #define SGI_VOLUME 0x06
  13. #define SGI_EFS 0x07
  14. #define SGI_LVOL 0x08
  15. #define SGI_RLVOL 0x09
  16. #define SGI_XFS 0x0a
  17. #define SGI_XFSLOG 0x0b
  18. #define SGI_XLV 0x0c
  19. #define SGI_XVM 0x0d
  20. #define SGI_ENTIRE_DISK SGI_VOLUME
  21. struct device_parameter { /* 48 bytes */
  22. unsigned char skew;
  23. unsigned char gap1;
  24. unsigned char gap2;
  25. unsigned char sparecyl;
  26. unsigned short pcylcount;
  27. unsigned short head_vol0;
  28. unsigned short ntrks; /* tracks in cyl 0 or vol 0 */
  29. unsigned char cmd_tag_queue_depth;
  30. unsigned char unused0;
  31. unsigned short unused1;
  32. unsigned short nsect; /* sectors/tracks in cyl 0 or vol 0 */
  33. unsigned short bytes;
  34. unsigned short ilfact;
  35. unsigned int flags; /* controller flags */
  36. unsigned int datarate;
  37. unsigned int retries_on_error;
  38. unsigned int ms_per_word;
  39. unsigned short xylogics_gap1;
  40. unsigned short xylogics_syncdelay;
  41. unsigned short xylogics_readdelay;
  42. unsigned short xylogics_gap2;
  43. unsigned short xylogics_readgate;
  44. unsigned short xylogics_writecont;
  45. };
  46. /*
  47. * controller flags
  48. */
  49. #define SECTOR_SLIP 0x01
  50. #define SECTOR_FWD 0x02
  51. #define TRACK_FWD 0x04
  52. #define TRACK_MULTIVOL 0x08
  53. #define IGNORE_ERRORS 0x10
  54. #define RESEEK 0x20
  55. #define ENABLE_CMDTAGQ 0x40
  56. typedef struct {
  57. unsigned int magic; /* expect SGI_LABEL_MAGIC */
  58. unsigned short boot_part; /* active boot partition */
  59. unsigned short swap_part; /* active swap partition */
  60. unsigned char boot_file[16]; /* name of the bootfile */
  61. struct device_parameter devparam; /* 1 * 48 bytes */
  62. struct volume_directory { /* 15 * 16 bytes */
  63. unsigned char vol_file_name[8]; /* a character array */
  64. unsigned int vol_file_start; /* number of logical block */
  65. unsigned int vol_file_size; /* number of bytes */
  66. } directory[15];
  67. struct sgi_partinfo { /* 16 * 12 bytes */
  68. unsigned int num_sectors; /* number of blocks */
  69. unsigned int start_sector; /* must be cylinder aligned */
  70. unsigned int id;
  71. } partitions[16];
  72. unsigned int csum;
  73. unsigned int fillbytes;
  74. } sgi_partition;
  75. typedef struct {
  76. unsigned int magic; /* looks like a magic number */
  77. unsigned int a2;
  78. unsigned int a3;
  79. unsigned int a4;
  80. unsigned int b1;
  81. unsigned short b2;
  82. unsigned short b3;
  83. unsigned int c[16];
  84. unsigned short d[3];
  85. unsigned char scsi_string[50];
  86. unsigned char serial[137];
  87. unsigned short check1816;
  88. unsigned char installer[225];
  89. } sgiinfo;
  90. #define SGI_LABEL_MAGIC 0x0be5a941
  91. #define SGI_LABEL_MAGIC_SWAPPED 0x41a9e50b
  92. #define SGI_INFO_MAGIC 0x00072959
  93. #define SGI_INFO_MAGIC_SWAPPED 0x59290700
  94. #define SGI_SSWAP16(x) (sgi_other_endian ? fdisk_swap16(x) : (uint16_t)(x))
  95. #define SGI_SSWAP32(x) (sgi_other_endian ? fdisk_swap32(x) : (uint32_t)(x))
  96. #define sgilabel ((sgi_partition *)MBRbuffer)
  97. #define sgiparam (sgilabel->devparam)
  98. /*
  99. *
  100. * fdisksgilabel.c
  101. *
  102. * Copyright (C) Andreas Neuper, Sep 1998.
  103. * This file may be modified and redistributed under
  104. * the terms of the GNU Public License.
  105. *
  106. * Sat Mar 20 EST 1999 Arnaldo Carvalho de Melo <acme@conectiva.com.br>
  107. * Internationalization
  108. */
  109. static smallint sgi_other_endian; /* bool */
  110. static smallint sgi_volumes = 1; /* max 15 */
  111. /*
  112. * only dealing with free blocks here
  113. */
  114. typedef struct {
  115. unsigned int first;
  116. unsigned int last;
  117. } freeblocks;
  118. static freeblocks freelist[17]; /* 16 partitions can produce 17 vacant slots */
  119. static void
  120. setfreelist(int i, unsigned int f, unsigned int l)
  121. {
  122. freelist[i].first = f;
  123. freelist[i].last = l;
  124. }
  125. static void
  126. add2freelist(unsigned int f, unsigned int l)
  127. {
  128. int i;
  129. for (i = 0; i < 17; i++)
  130. if (freelist[i].last == 0)
  131. break;
  132. setfreelist(i, f, l);
  133. }
  134. static void
  135. clearfreelist(void)
  136. {
  137. int i;
  138. for (i = 0; i < 17; i++)
  139. setfreelist(i, 0, 0);
  140. }
  141. static unsigned int
  142. isinfreelist(unsigned int b)
  143. {
  144. int i;
  145. for (i = 0; i < 17; i++)
  146. if (freelist[i].first <= b && freelist[i].last >= b)
  147. return freelist[i].last;
  148. return 0;
  149. }
  150. /* return last vacant block of this stride (never 0). */
  151. /* the '>=' is not quite correct, but simplifies the code */
  152. /*
  153. * end of free blocks section
  154. */
  155. static const char *const sgi_sys_types[] = {
  156. /* SGI_VOLHDR */ "\x00" "SGI volhdr" ,
  157. /* 0x01 */ "\x01" "SGI trkrepl" ,
  158. /* 0x02 */ "\x02" "SGI secrepl" ,
  159. /* SGI_SWAP */ "\x03" "SGI raw" ,
  160. /* 0x04 */ "\x04" "SGI bsd" ,
  161. /* 0x05 */ "\x05" "SGI sysv" ,
  162. /* SGI_ENTIRE_DISK */ "\x06" "SGI volume" ,
  163. /* SGI_EFS */ "\x07" "SGI efs" ,
  164. /* 0x08 */ "\x08" "SGI lvol" ,
  165. /* 0x09 */ "\x09" "SGI rlvol" ,
  166. /* SGI_XFS */ "\x0a" "SGI xfs" ,
  167. /* SGI_XFSLOG */ "\x0b" "SGI xfslog" ,
  168. /* SGI_XLV */ "\x0c" "SGI xlv" ,
  169. /* SGI_XVM */ "\x0d" "SGI xvm" ,
  170. /* LINUX_SWAP */ "\x82" "Linux swap" ,
  171. /* LINUX_NATIVE */ "\x83" "Linux native",
  172. /* LINUX_LVM */ "\x8d" "Linux LVM" ,
  173. /* LINUX_RAID */ "\xfd" "Linux RAID" ,
  174. NULL
  175. };
  176. static int
  177. sgi_get_nsect(void)
  178. {
  179. return SGI_SSWAP16(sgilabel->devparam.nsect);
  180. }
  181. static int
  182. sgi_get_ntrks(void)
  183. {
  184. return SGI_SSWAP16(sgilabel->devparam.ntrks);
  185. }
  186. static unsigned int
  187. two_s_complement_32bit_sum(unsigned int* base, int size /* in bytes */)
  188. {
  189. int i = 0;
  190. unsigned int sum = 0;
  191. size /= sizeof(unsigned int);
  192. for (i = 0; i < size; i++)
  193. sum -= SGI_SSWAP32(base[i]);
  194. return sum;
  195. }
  196. void BUG_bad_sgi_partition_size(void);
  197. static int
  198. check_sgi_label(void)
  199. {
  200. if (sizeof(sgi_partition) > 512) {
  201. /* According to MIPS Computer Systems, Inc the label
  202. * must not contain more than 512 bytes */
  203. BUG_bad_sgi_partition_size();
  204. }
  205. if (sgilabel->magic != SGI_LABEL_MAGIC
  206. && sgilabel->magic != SGI_LABEL_MAGIC_SWAPPED
  207. ) {
  208. current_label_type = LABEL_DOS;
  209. return 0;
  210. }
  211. sgi_other_endian = (sgilabel->magic == SGI_LABEL_MAGIC_SWAPPED);
  212. /*
  213. * test for correct checksum
  214. */
  215. if (two_s_complement_32bit_sum((unsigned int*)sgilabel,
  216. sizeof(*sgilabel))) {
  217. printf("Detected sgi disklabel with wrong checksum\n");
  218. }
  219. update_units();
  220. current_label_type = LABEL_SGI;
  221. g_partitions = 16;
  222. sgi_volumes = 15;
  223. return 1;
  224. }
  225. static unsigned int
  226. sgi_get_start_sector(int i)
  227. {
  228. return SGI_SSWAP32(sgilabel->partitions[i].start_sector);
  229. }
  230. static unsigned int
  231. sgi_get_num_sectors(int i)
  232. {
  233. return SGI_SSWAP32(sgilabel->partitions[i].num_sectors);
  234. }
  235. static int
  236. sgi_get_sysid(int i)
  237. {
  238. return SGI_SSWAP32(sgilabel->partitions[i].id);
  239. }
  240. static int
  241. sgi_get_bootpartition(void)
  242. {
  243. return SGI_SSWAP16(sgilabel->boot_part);
  244. }
  245. static int
  246. sgi_get_swappartition(void)
  247. {
  248. return SGI_SSWAP16(sgilabel->swap_part);
  249. }
  250. static void
  251. sgi_list_table(int xtra)
  252. {
  253. int i, w, wd;
  254. int kpi = 0; /* kernel partition ID */
  255. if (xtra) {
  256. printf("\nDisk %s (SGI disk label): %d heads, %d sectors\n"
  257. "%d cylinders, %d physical cylinders\n"
  258. "%d extra sects/cyl, interleave %d:1\n"
  259. "%s\n"
  260. "Units = %s of %d * 512 bytes\n\n",
  261. disk_device, g_heads, g_sectors, g_cylinders,
  262. SGI_SSWAP16(sgiparam.pcylcount),
  263. SGI_SSWAP16(sgiparam.sparecyl),
  264. SGI_SSWAP16(sgiparam.ilfact),
  265. (char *)sgilabel,
  266. str_units(PLURAL), units_per_sector);
  267. } else {
  268. printf("\nDisk %s (SGI disk label): "
  269. "%d heads, %d sectors, %d cylinders\n"
  270. "Units = %s of %d * 512 bytes\n\n",
  271. disk_device, g_heads, g_sectors, g_cylinders,
  272. str_units(PLURAL), units_per_sector );
  273. }
  274. w = strlen(disk_device);
  275. wd = sizeof("Device") - 1;
  276. if (w < wd)
  277. w = wd;
  278. printf("----- partitions -----\n"
  279. "Pt# %*s Info Start End Sectors Id System\n",
  280. w + 2, "Device");
  281. for (i = 0; i < g_partitions; i++) {
  282. if (sgi_get_num_sectors(i) || SGI_DEBUG) {
  283. uint32_t start = sgi_get_start_sector(i);
  284. uint32_t len = sgi_get_num_sectors(i);
  285. kpi++; /* only count nonempty partitions */
  286. printf(
  287. "%2d: %s %4s %9ld %9ld %9ld %2x %s\n",
  288. /* fdisk part number */ i+1,
  289. /* device */ partname(disk_device, kpi, w+3),
  290. /* flags */ (sgi_get_swappartition() == i) ? "swap" :
  291. /* flags */ (sgi_get_bootpartition() == i) ? "boot" : " ",
  292. /* start */ (long) scround(start),
  293. /* end */ (long) scround(start+len)-1,
  294. /* no odd flag on end */(long) len,
  295. /* type id */ sgi_get_sysid(i),
  296. /* type name */ partition_type(sgi_get_sysid(i)));
  297. }
  298. }
  299. printf("----- Bootinfo -----\nBootfile: %s\n"
  300. "----- Directory Entries -----\n",
  301. sgilabel->boot_file);
  302. for (i = 0; i < sgi_volumes; i++) {
  303. if (sgilabel->directory[i].vol_file_size) {
  304. uint32_t start = SGI_SSWAP32(sgilabel->directory[i].vol_file_start);
  305. uint32_t len = SGI_SSWAP32(sgilabel->directory[i].vol_file_size);
  306. unsigned char *name = sgilabel->directory[i].vol_file_name;
  307. printf("%2d: %-10s sector%5u size%8u\n",
  308. i, (char*)name, (unsigned int) start, (unsigned int) len);
  309. }
  310. }
  311. }
  312. static void
  313. sgi_set_bootpartition(int i)
  314. {
  315. sgilabel->boot_part = SGI_SSWAP16(((short)i));
  316. }
  317. static unsigned int
  318. sgi_get_lastblock(void)
  319. {
  320. return g_heads * g_sectors * g_cylinders;
  321. }
  322. static void
  323. sgi_set_swappartition(int i)
  324. {
  325. sgilabel->swap_part = SGI_SSWAP16(((short)i));
  326. }
  327. static int
  328. sgi_check_bootfile(const char* aFile)
  329. {
  330. if (strlen(aFile) < 3) /* "/a\n" is minimum */ {
  331. printf("\nInvalid Bootfile!\n"
  332. "\tThe bootfile must be an absolute non-zero pathname,\n"
  333. "\te.g. \"/unix\" or \"/unix.save\".\n");
  334. return 0;
  335. }
  336. if (strlen(aFile) > 16) {
  337. printf("\nName of Bootfile too long (>16 bytes)\n");
  338. return 0;
  339. }
  340. if (aFile[0] != '/') {
  341. printf("\nBootfile must have a fully qualified pathname\n");
  342. return 0;
  343. }
  344. if (strncmp(aFile, (char*)sgilabel->boot_file, 16)) {
  345. printf("\nBe aware, that the bootfile is not checked for existence.\n"
  346. "\tSGI's default is \"/unix\" and for backup \"/unix.save\".\n");
  347. /* filename is correct and did change */
  348. return 1;
  349. }
  350. return 0; /* filename did not change */
  351. }
  352. static const char *
  353. sgi_get_bootfile(void)
  354. {
  355. return (char*)sgilabel->boot_file;
  356. }
  357. static void
  358. sgi_set_bootfile(const char* aFile)
  359. {
  360. int i = 0;
  361. if (sgi_check_bootfile(aFile)) {
  362. while (i < 16) {
  363. if ((aFile[i] != '\n') /* in principle caught again by next line */
  364. && (strlen(aFile) > i))
  365. sgilabel->boot_file[i] = aFile[i];
  366. else
  367. sgilabel->boot_file[i] = 0;
  368. i++;
  369. }
  370. printf("\n\tBootfile is changed to \"%s\"\n", sgilabel->boot_file);
  371. }
  372. }
  373. static void
  374. create_sgiinfo(void)
  375. {
  376. /* I keep SGI's habit to write the sgilabel to the second block */
  377. sgilabel->directory[0].vol_file_start = SGI_SSWAP32(2);
  378. sgilabel->directory[0].vol_file_size = SGI_SSWAP32(sizeof(sgiinfo));
  379. strncpy((char*)sgilabel->directory[0].vol_file_name, "sgilabel", 8);
  380. }
  381. static sgiinfo *fill_sgiinfo(void);
  382. static void
  383. sgi_write_table(void)
  384. {
  385. sgilabel->csum = 0;
  386. sgilabel->csum = SGI_SSWAP32(two_s_complement_32bit_sum(
  387. (unsigned int*)sgilabel, sizeof(*sgilabel)));
  388. assert(two_s_complement_32bit_sum(
  389. (unsigned int*)sgilabel, sizeof(*sgilabel)) == 0);
  390. if (lseek(dev_fd, 0, SEEK_SET) < 0)
  391. fdisk_fatal(unable_to_seek);
  392. if (write(dev_fd, sgilabel, SECTOR_SIZE) != SECTOR_SIZE)
  393. fdisk_fatal(unable_to_write);
  394. if (!strncmp((char*)sgilabel->directory[0].vol_file_name, "sgilabel", 8)) {
  395. /*
  396. * keep this habit of first writing the "sgilabel".
  397. * I never tested whether it works without (AN 981002).
  398. */
  399. sgiinfo *info = fill_sgiinfo();
  400. int infostartblock = SGI_SSWAP32(sgilabel->directory[0].vol_file_start);
  401. if (lseek(dev_fd, infostartblock*SECTOR_SIZE, SEEK_SET) < 0)
  402. fdisk_fatal(unable_to_seek);
  403. if (write(dev_fd, info, SECTOR_SIZE) != SECTOR_SIZE)
  404. fdisk_fatal(unable_to_write);
  405. free(info);
  406. }
  407. }
  408. static int
  409. compare_start(int *x, int *y)
  410. {
  411. /*
  412. * sort according to start sectors
  413. * and prefers largest partition:
  414. * entry zero is entire disk entry
  415. */
  416. unsigned int i = *x;
  417. unsigned int j = *y;
  418. unsigned int a = sgi_get_start_sector(i);
  419. unsigned int b = sgi_get_start_sector(j);
  420. unsigned int c = sgi_get_num_sectors(i);
  421. unsigned int d = sgi_get_num_sectors(j);
  422. if (a == b)
  423. return (d > c) ? 1 : (d == c) ? 0 : -1;
  424. return (a > b) ? 1 : -1;
  425. }
  426. static int
  427. verify_sgi(int verbose)
  428. {
  429. int Index[16]; /* list of valid partitions */
  430. int sortcount = 0; /* number of used partitions, i.e. non-zero lengths */
  431. int entire = 0, i = 0;
  432. unsigned int start = 0;
  433. long long gap = 0; /* count unused blocks */
  434. unsigned int lastblock = sgi_get_lastblock();
  435. clearfreelist();
  436. for (i = 0; i < 16; i++) {
  437. if (sgi_get_num_sectors(i) != 0) {
  438. Index[sortcount++] = i;
  439. if (sgi_get_sysid(i) == SGI_ENTIRE_DISK) {
  440. if (entire++ == 1) {
  441. if (verbose)
  442. printf("More than one entire disk entry present\n");
  443. }
  444. }
  445. }
  446. }
  447. if (sortcount == 0) {
  448. if (verbose)
  449. printf("No partitions defined\n");
  450. return (lastblock > 0) ? 1 : (lastblock == 0) ? 0 : -1;
  451. }
  452. qsort(Index, sortcount, sizeof(Index[0]), (void*)compare_start);
  453. if (sgi_get_sysid(Index[0]) == SGI_ENTIRE_DISK) {
  454. if ((Index[0] != 10) && verbose)
  455. printf("IRIX likes when Partition 11 covers the entire disk\n");
  456. if ((sgi_get_start_sector(Index[0]) != 0) && verbose)
  457. printf("The entire disk partition should start "
  458. "at block 0,\n"
  459. "not at diskblock %d\n",
  460. sgi_get_start_sector(Index[0]));
  461. if (SGI_DEBUG) /* I do not understand how some disks fulfil it */
  462. if ((sgi_get_num_sectors(Index[0]) != lastblock) && verbose)
  463. printf("The entire disk partition is only %d diskblock large,\n"
  464. "but the disk is %d diskblocks long\n",
  465. sgi_get_num_sectors(Index[0]), lastblock);
  466. lastblock = sgi_get_num_sectors(Index[0]);
  467. } else {
  468. if (verbose)
  469. printf("One Partition (#11) should cover the entire disk\n");
  470. if (SGI_DEBUG > 2)
  471. printf("sysid=%d\tpartition=%d\n",
  472. sgi_get_sysid(Index[0]), Index[0]+1);
  473. }
  474. for (i = 1, start = 0; i < sortcount; i++) {
  475. int cylsize = sgi_get_nsect() * sgi_get_ntrks();
  476. if ((sgi_get_start_sector(Index[i]) % cylsize) != 0) {
  477. if (SGI_DEBUG) /* I do not understand how some disks fulfil it */
  478. if (verbose)
  479. printf("Partition %d does not start on cylinder boundary\n",
  480. Index[i]+1);
  481. }
  482. if (sgi_get_num_sectors(Index[i]) % cylsize != 0) {
  483. if (SGI_DEBUG) /* I do not understand how some disks fulfil it */
  484. if (verbose)
  485. printf("Partition %d does not end on cylinder boundary\n",
  486. Index[i]+1);
  487. }
  488. /* We cannot handle several "entire disk" entries. */
  489. if (sgi_get_sysid(Index[i]) == SGI_ENTIRE_DISK) continue;
  490. if (start > sgi_get_start_sector(Index[i])) {
  491. if (verbose)
  492. printf("Partitions %d and %d overlap by %d sectors\n",
  493. Index[i-1]+1, Index[i]+1,
  494. start - sgi_get_start_sector(Index[i]));
  495. if (gap > 0) gap = -gap;
  496. if (gap == 0) gap = -1;
  497. }
  498. if (start < sgi_get_start_sector(Index[i])) {
  499. if (verbose)
  500. printf("Unused gap of %8u sectors - sectors %8u-%8u\n",
  501. sgi_get_start_sector(Index[i]) - start,
  502. start, sgi_get_start_sector(Index[i])-1);
  503. gap += sgi_get_start_sector(Index[i]) - start;
  504. add2freelist(start, sgi_get_start_sector(Index[i]));
  505. }
  506. start = sgi_get_start_sector(Index[i])
  507. + sgi_get_num_sectors(Index[i]);
  508. if (SGI_DEBUG > 1) {
  509. if (verbose)
  510. printf("%2d:%12d\t%12d\t%12d\n", Index[i],
  511. sgi_get_start_sector(Index[i]),
  512. sgi_get_num_sectors(Index[i]),
  513. sgi_get_sysid(Index[i]));
  514. }
  515. }
  516. if (start < lastblock) {
  517. if (verbose)
  518. printf("Unused gap of %8u sectors - sectors %8u-%8u\n",
  519. lastblock - start, start, lastblock-1);
  520. gap += lastblock - start;
  521. add2freelist(start, lastblock);
  522. }
  523. /*
  524. * Done with arithmetics
  525. * Go for details now
  526. */
  527. if (verbose) {
  528. if (!sgi_get_num_sectors(sgi_get_bootpartition())) {
  529. printf("\nThe boot partition does not exist\n");
  530. }
  531. if (!sgi_get_num_sectors(sgi_get_swappartition())) {
  532. printf("\nThe swap partition does not exist\n");
  533. } else {
  534. if ((sgi_get_sysid(sgi_get_swappartition()) != SGI_SWAP)
  535. && (sgi_get_sysid(sgi_get_swappartition()) != LINUX_SWAP))
  536. printf("\nThe swap partition has no swap type\n");
  537. }
  538. if (sgi_check_bootfile("/unix"))
  539. printf("\tYou have chosen an unusual boot file name\n");
  540. }
  541. return (gap > 0) ? 1 : (gap == 0) ? 0 : -1;
  542. }
  543. static int
  544. sgi_gaps(void)
  545. {
  546. /*
  547. * returned value is:
  548. * = 0 : disk is properly filled to the rim
  549. * < 0 : there is an overlap
  550. * > 0 : there is still some vacant space
  551. */
  552. return verify_sgi(0);
  553. }
  554. static void
  555. sgi_change_sysid(int i, int sys)
  556. {
  557. if (sgi_get_num_sectors(i) == 0) { /* caught already before, ... */
  558. printf("Sorry you may change the Tag of non-empty partitions\n");
  559. return;
  560. }
  561. if ((sys != SGI_ENTIRE_DISK) && (sys != SGI_VOLHDR)
  562. && (sgi_get_start_sector(i) < 1)
  563. ) {
  564. read_maybe_empty(
  565. "It is highly recommended that the partition at offset 0\n"
  566. "is of type \"SGI volhdr\", the IRIX system will rely on it to\n"
  567. "retrieve from its directory standalone tools like sash and fx.\n"
  568. "Only the \"SGI volume\" entire disk section may violate this.\n"
  569. "Type YES if you are sure about tagging this partition differently.\n");
  570. if (strcmp(line_ptr, "YES\n") != 0)
  571. return;
  572. }
  573. sgilabel->partitions[i].id = SGI_SSWAP32(sys);
  574. }
  575. /* returns partition index of first entry marked as entire disk */
  576. static int
  577. sgi_entire(void)
  578. {
  579. int i;
  580. for (i = 0; i < 16; i++)
  581. if (sgi_get_sysid(i) == SGI_VOLUME)
  582. return i;
  583. return -1;
  584. }
  585. static void
  586. sgi_set_partition(int i, unsigned int start, unsigned int length, int sys)
  587. {
  588. sgilabel->partitions[i].id = SGI_SSWAP32(sys);
  589. sgilabel->partitions[i].num_sectors = SGI_SSWAP32(length);
  590. sgilabel->partitions[i].start_sector = SGI_SSWAP32(start);
  591. set_changed(i);
  592. if (sgi_gaps() < 0) /* rebuild freelist */
  593. printf("Partition overlap detected\n");
  594. }
  595. static void
  596. sgi_set_entire(void)
  597. {
  598. int n;
  599. for (n = 10; n < g_partitions; n++) {
  600. if (!sgi_get_num_sectors(n) ) {
  601. sgi_set_partition(n, 0, sgi_get_lastblock(), SGI_VOLUME);
  602. break;
  603. }
  604. }
  605. }
  606. static void
  607. sgi_set_volhdr(void)
  608. {
  609. int n;
  610. for (n = 8; n < g_partitions; n++) {
  611. if (!sgi_get_num_sectors(n)) {
  612. /*
  613. * 5 cylinders is an arbitrary value I like
  614. * IRIX 5.3 stored files in the volume header
  615. * (like sash, symmon, fx, ide) with ca. 3200
  616. * sectors.
  617. */
  618. if (g_heads * g_sectors * 5 < sgi_get_lastblock())
  619. sgi_set_partition(n, 0, g_heads * g_sectors * 5, SGI_VOLHDR);
  620. break;
  621. }
  622. }
  623. }
  624. static void
  625. sgi_delete_partition(int i)
  626. {
  627. sgi_set_partition(i, 0, 0, 0);
  628. }
  629. static void
  630. sgi_add_partition(int n, int sys)
  631. {
  632. char mesg[256];
  633. unsigned int first = 0, last = 0;
  634. if (n == 10) {
  635. sys = SGI_VOLUME;
  636. } else if (n == 8) {
  637. sys = 0;
  638. }
  639. if (sgi_get_num_sectors(n)) {
  640. printf(msg_part_already_defined, n + 1);
  641. return;
  642. }
  643. if ((sgi_entire() == -1) && (sys != SGI_VOLUME)) {
  644. printf("Attempting to generate entire disk entry automatically\n");
  645. sgi_set_entire();
  646. sgi_set_volhdr();
  647. }
  648. if ((sgi_gaps() == 0) && (sys != SGI_VOLUME)) {
  649. printf("The entire disk is already covered with partitions\n");
  650. return;
  651. }
  652. if (sgi_gaps() < 0) {
  653. printf("You got a partition overlap on the disk. Fix it first!\n");
  654. return;
  655. }
  656. snprintf(mesg, sizeof(mesg), "First %s", str_units(SINGULAR));
  657. while (1) {
  658. if (sys == SGI_VOLUME) {
  659. last = sgi_get_lastblock();
  660. first = read_int(0, 0, last-1, 0, mesg);
  661. if (first != 0) {
  662. printf("It is highly recommended that eleventh partition\n"
  663. "covers the entire disk and is of type 'SGI volume'\n");
  664. }
  665. } else {
  666. first = freelist[0].first;
  667. last = freelist[0].last;
  668. first = read_int(scround(first), scround(first), scround(last)-1,
  669. 0, mesg);
  670. }
  671. if (display_in_cyl_units)
  672. first *= units_per_sector;
  673. else
  674. first = first; /* align to cylinder if you know how ... */
  675. if (!last )
  676. last = isinfreelist(first);
  677. if (last != 0)
  678. break;
  679. printf("You will get a partition overlap on the disk. "
  680. "Fix it first!\n");
  681. }
  682. snprintf(mesg, sizeof(mesg), " Last %s", str_units(SINGULAR));
  683. last = read_int(scround(first), scround(last)-1, scround(last)-1,
  684. scround(first), mesg)+1;
  685. if (display_in_cyl_units)
  686. last *= units_per_sector;
  687. else
  688. last = last; /* align to cylinder if You know how ... */
  689. if ( (sys == SGI_VOLUME) && (first != 0 || last != sgi_get_lastblock() ) )
  690. printf("It is highly recommended that eleventh partition\n"
  691. "covers the entire disk and is of type 'SGI volume'\n");
  692. sgi_set_partition(n, first, last-first, sys);
  693. }
  694. #if ENABLE_FEATURE_FDISK_ADVANCED
  695. static void
  696. create_sgilabel(void)
  697. {
  698. struct hd_geometry geometry;
  699. struct {
  700. unsigned int start;
  701. unsigned int nsect;
  702. int sysid;
  703. } old[4];
  704. int i = 0;
  705. long longsectors; /* the number of sectors on the device */
  706. int res; /* the result from the ioctl */
  707. int sec_fac; /* the sector factor */
  708. sec_fac = sector_size / 512; /* determine the sector factor */
  709. printf(msg_building_new_label, "SGI disklabel");
  710. sgi_other_endian = BB_LITTLE_ENDIAN;
  711. res = ioctl(dev_fd, BLKGETSIZE, &longsectors);
  712. if (!ioctl(dev_fd, HDIO_GETGEO, &geometry)) {
  713. g_heads = geometry.heads;
  714. g_sectors = geometry.sectors;
  715. if (res == 0) {
  716. /* the get device size ioctl was successful */
  717. g_cylinders = longsectors / (g_heads * g_sectors);
  718. g_cylinders /= sec_fac;
  719. } else {
  720. /* otherwise print error and use truncated version */
  721. g_cylinders = geometry.cylinders;
  722. printf(
  723. "Warning: BLKGETSIZE ioctl failed on %s. Using geometry cylinder value of %d.\n"
  724. "This value may be truncated for devices > 33.8 GB.\n", disk_device, g_cylinders);
  725. }
  726. }
  727. for (i = 0; i < 4; i++) {
  728. old[i].sysid = 0;
  729. if (valid_part_table_flag(MBRbuffer)) {
  730. if (get_part_table(i)->sys_ind) {
  731. old[i].sysid = get_part_table(i)->sys_ind;
  732. old[i].start = get_start_sect(get_part_table(i));
  733. old[i].nsect = get_nr_sects(get_part_table(i));
  734. printf("Trying to keep parameters of partition %d\n", i);
  735. if (SGI_DEBUG)
  736. printf("ID=%02x\tSTART=%d\tLENGTH=%d\n",
  737. old[i].sysid, old[i].start, old[i].nsect);
  738. }
  739. }
  740. }
  741. memset(MBRbuffer, 0, sizeof(MBRbuffer));
  742. /* fields with '//' are already zeroed out by memset above */
  743. sgilabel->magic = SGI_SSWAP32(SGI_LABEL_MAGIC);
  744. //sgilabel->boot_part = SGI_SSWAP16(0);
  745. sgilabel->swap_part = SGI_SSWAP16(1);
  746. //memset(sgilabel->boot_file, 0, 16);
  747. strcpy((char*)sgilabel->boot_file, "/unix"); /* sizeof(sgilabel->boot_file) == 16 > 6 */
  748. //sgilabel->devparam.skew = (0);
  749. //sgilabel->devparam.gap1 = (0);
  750. //sgilabel->devparam.gap2 = (0);
  751. //sgilabel->devparam.sparecyl = (0);
  752. sgilabel->devparam.pcylcount = SGI_SSWAP16(geometry.cylinders);
  753. //sgilabel->devparam.head_vol0 = SGI_SSWAP16(0);
  754. /* tracks/cylinder (heads) */
  755. sgilabel->devparam.ntrks = SGI_SSWAP16(geometry.heads);
  756. //sgilabel->devparam.cmd_tag_queue_depth = (0);
  757. //sgilabel->devparam.unused0 = (0);
  758. //sgilabel->devparam.unused1 = SGI_SSWAP16(0);
  759. /* sectors/track */
  760. sgilabel->devparam.nsect = SGI_SSWAP16(geometry.sectors);
  761. sgilabel->devparam.bytes = SGI_SSWAP16(512);
  762. sgilabel->devparam.ilfact = SGI_SSWAP16(1);
  763. sgilabel->devparam.flags = SGI_SSWAP32(TRACK_FWD|
  764. IGNORE_ERRORS|RESEEK);
  765. //sgilabel->devparam.datarate = SGI_SSWAP32(0);
  766. sgilabel->devparam.retries_on_error = SGI_SSWAP32(1);
  767. //sgilabel->devparam.ms_per_word = SGI_SSWAP32(0);
  768. //sgilabel->devparam.xylogics_gap1 = SGI_SSWAP16(0);
  769. //sgilabel->devparam.xylogics_syncdelay = SGI_SSWAP16(0);
  770. //sgilabel->devparam.xylogics_readdelay = SGI_SSWAP16(0);
  771. //sgilabel->devparam.xylogics_gap2 = SGI_SSWAP16(0);
  772. //sgilabel->devparam.xylogics_readgate = SGI_SSWAP16(0);
  773. //sgilabel->devparam.xylogics_writecont = SGI_SSWAP16(0);
  774. //memset( &(sgilabel->directory), 0, sizeof(struct volume_directory)*15 );
  775. //memset( &(sgilabel->partitions), 0, sizeof(struct sgi_partinfo)*16 );
  776. current_label_type = LABEL_SGI;
  777. g_partitions = 16;
  778. sgi_volumes = 15;
  779. sgi_set_entire();
  780. sgi_set_volhdr();
  781. for (i = 0; i < 4; i++) {
  782. if (old[i].sysid) {
  783. sgi_set_partition(i, old[i].start, old[i].nsect, old[i].sysid);
  784. }
  785. }
  786. }
  787. static void
  788. sgi_set_xcyl(void)
  789. {
  790. /* do nothing in the beginning */
  791. }
  792. #endif /* FEATURE_FDISK_ADVANCED */
  793. /* _____________________________________________________________
  794. */
  795. static sgiinfo *
  796. fill_sgiinfo(void)
  797. {
  798. sgiinfo *info = xzalloc(sizeof(sgiinfo));
  799. info->magic = SGI_SSWAP32(SGI_INFO_MAGIC);
  800. info->b1 = SGI_SSWAP32(-1);
  801. info->b2 = SGI_SSWAP16(-1);
  802. info->b3 = SGI_SSWAP16(1);
  803. /* You may want to replace this string !!!!!!! */
  804. strcpy( (char*)info->scsi_string, "IBM OEM 0662S12 3 30" );
  805. strcpy( (char*)info->serial, "0000" );
  806. info->check1816 = SGI_SSWAP16(18*256 +16 );
  807. strcpy( (char*)info->installer, "Sfx version 5.3, Oct 18, 1994" );
  808. return info;
  809. }
  810. #endif /* SGI_LABEL */