fdisk.c 73 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646164716481649165016511652165316541655165616571658165916601661166216631664166516661667166816691670167116721673167416751676167716781679168016811682168316841685168616871688168916901691169216931694169516961697169816991700170117021703170417051706170717081709171017111712171317141715171617171718171917201721172217231724172517261727172817291730173117321733173417351736173717381739174017411742174317441745174617471748174917501751175217531754175517561757175817591760176117621763176417651766176717681769177017711772177317741775177617771778177917801781178217831784178517861787178817891790179117921793179417951796179717981799180018011802180318041805180618071808180918101811181218131814181518161817181818191820182118221823182418251826182718281829183018311832183318341835183618371838183918401841184218431844184518461847184818491850185118521853185418551856185718581859186018611862186318641865186618671868186918701871187218731874187518761877187818791880188118821883188418851886188718881889189018911892189318941895189618971898189919001901190219031904190519061907190819091910191119121913191419151916191719181919192019211922192319241925192619271928192919301931193219331934193519361937193819391940194119421943194419451946194719481949195019511952195319541955195619571958195919601961196219631964196519661967196819691970197119721973197419751976197719781979198019811982198319841985198619871988198919901991199219931994199519961997199819992000200120022003200420052006200720082009201020112012201320142015201620172018201920202021202220232024202520262027202820292030203120322033203420352036203720382039204020412042204320442045204620472048204920502051205220532054205520562057205820592060206120622063206420652066206720682069207020712072207320742075207620772078207920802081208220832084208520862087208820892090209120922093209420952096209720982099210021012102210321042105210621072108210921102111211221132114211521162117211821192120212121222123212421252126212721282129213021312132213321342135213621372138213921402141214221432144214521462147214821492150215121522153215421552156215721582159216021612162216321642165216621672168216921702171217221732174217521762177217821792180218121822183218421852186218721882189219021912192219321942195219621972198219922002201220222032204220522062207220822092210221122122213221422152216221722182219222022212222222322242225222622272228222922302231223222332234223522362237223822392240224122422243224422452246224722482249225022512252225322542255225622572258225922602261226222632264226522662267226822692270227122722273227422752276227722782279228022812282228322842285228622872288228922902291229222932294229522962297229822992300230123022303230423052306230723082309231023112312231323142315231623172318231923202321232223232324232523262327232823292330233123322333233423352336233723382339234023412342234323442345234623472348234923502351235223532354235523562357235823592360236123622363236423652366236723682369237023712372237323742375237623772378237923802381238223832384238523862387238823892390239123922393239423952396239723982399240024012402240324042405240624072408240924102411241224132414241524162417241824192420242124222423242424252426242724282429243024312432243324342435243624372438243924402441244224432444244524462447244824492450245124522453245424552456245724582459246024612462246324642465246624672468246924702471247224732474247524762477247824792480248124822483248424852486248724882489249024912492249324942495249624972498249925002501250225032504250525062507250825092510251125122513251425152516251725182519252025212522252325242525252625272528252925302531253225332534253525362537253825392540254125422543254425452546254725482549255025512552255325542555255625572558255925602561256225632564256525662567256825692570257125722573257425752576257725782579258025812582258325842585258625872588258925902591259225932594259525962597259825992600260126022603260426052606260726082609261026112612261326142615261626172618261926202621262226232624262526262627262826292630263126322633263426352636263726382639264026412642264326442645264626472648264926502651265226532654265526562657265826592660266126622663266426652666266726682669267026712672267326742675267626772678267926802681268226832684268526862687268826892690269126922693269426952696269726982699270027012702270327042705270627072708270927102711271227132714271527162717271827192720272127222723272427252726272727282729273027312732273327342735273627372738273927402741274227432744274527462747274827492750275127522753275427552756275727582759276027612762276327642765276627672768276927702771277227732774277527762777277827792780278127822783278427852786278727882789279027912792279327942795279627972798279928002801280228032804280528062807280828092810281128122813281428152816281728182819282028212822282328242825282628272828282928302831283228332834283528362837283828392840284128422843284428452846284728482849285028512852285328542855285628572858285928602861286228632864286528662867286828692870287128722873287428752876287728782879288028812882288328842885288628872888288928902891289228932894289528962897289828992900290129022903290429052906290729082909291029112912291329142915291629172918291929202921292229232924292529262927292829292930293129322933293429352936293729382939294029412942294329442945294629472948294929502951295229532954295529562957295829592960296129622963296429652966296729682969297029712972297329742975297629772978297929802981298229832984298529862987298829892990299129922993299429952996299729982999300030013002300330043005300630073008300930103011301230133014301530163017301830193020302130223023302430253026302730283029303030313032303330343035303630373038303930403041
  1. /* vi: set sw=4 ts=4: */
  2. /* fdisk.c -- Partition table manipulator for Linux.
  3. *
  4. * Copyright (C) 1992 A. V. Le Blanc (LeBlanc@mcc.ac.uk)
  5. * Copyright (C) 2001,2002 Vladimir Oleynik <dzo@simtreas.ru> (initial bb port)
  6. *
  7. * Licensed under the GPL v2 or later, see the file LICENSE in this tarball.
  8. */
  9. #include <assert.h> /* assert */
  10. #include "busybox.h"
  11. #define _(x) x
  12. /* Looks like someone forgot to add this to config system */
  13. #ifndef ENABLE_FEATURE_FDISK_BLKSIZE
  14. # define ENABLE_FEATURE_FDISK_BLKSIZE 0
  15. # define USE_FEATURE_FDISK_BLKSIZE(a)
  16. #endif
  17. #define DEFAULT_SECTOR_SIZE 512
  18. #define MAX_SECTOR_SIZE 2048
  19. #define SECTOR_SIZE 512 /* still used in osf/sgi/sun code */
  20. #define MAXIMUM_PARTS 60
  21. #define ACTIVE_FLAG 0x80
  22. #define EXTENDED 0x05
  23. #define WIN98_EXTENDED 0x0f
  24. #define LINUX_PARTITION 0x81
  25. #define LINUX_SWAP 0x82
  26. #define LINUX_NATIVE 0x83
  27. #define LINUX_EXTENDED 0x85
  28. #define LINUX_LVM 0x8e
  29. #define LINUX_RAID 0xfd
  30. #define IS_EXTENDED(i) \
  31. ((i) == EXTENDED || (i) == WIN98_EXTENDED || (i) == LINUX_EXTENDED)
  32. #define SIZE(a) (sizeof(a)/sizeof((a)[0]))
  33. #define cround(n) (display_in_cyl_units ? ((n)/units_per_sector)+1 : (n))
  34. #define scround(x) (((x)+units_per_sector-1)/units_per_sector)
  35. struct hd_geometry {
  36. unsigned char heads;
  37. unsigned char sectors;
  38. unsigned short cylinders;
  39. unsigned long start;
  40. };
  41. #define HDIO_GETGEO 0x0301 /* get device geometry */
  42. struct systypes {
  43. const char *name;
  44. };
  45. static unsigned sector_size = DEFAULT_SECTOR_SIZE;
  46. static unsigned user_set_sector_size;
  47. static unsigned sector_offset = 1;
  48. /*
  49. * Raw disk label. For DOS-type partition tables the MBR,
  50. * with descriptions of the primary partitions.
  51. */
  52. #if (MAX_SECTOR_SIZE) > (BUFSIZ+1)
  53. static char MBRbuffer[MAX_SECTOR_SIZE];
  54. #else
  55. # define MBRbuffer bb_common_bufsiz1
  56. #endif
  57. #if ENABLE_FEATURE_OSF_LABEL
  58. static int possibly_osf_label;
  59. #endif
  60. static unsigned heads, sectors, cylinders;
  61. static void update_units(void);
  62. /*
  63. * return partition name - uses static storage unless buf is supplied
  64. */
  65. static const char *
  66. partname(const char *dev, int pno, int lth)
  67. {
  68. static char buffer[80];
  69. const char *p;
  70. int w, wp;
  71. int bufsiz;
  72. char *bufp;
  73. bufp = buffer;
  74. bufsiz = sizeof(buffer);
  75. w = strlen(dev);
  76. p = "";
  77. if (isdigit(dev[w-1]))
  78. p = "p";
  79. /* devfs kludge - note: fdisk partition names are not supposed
  80. to equal kernel names, so there is no reason to do this */
  81. if (strcmp(dev + w - 4, "disc") == 0) {
  82. w -= 4;
  83. p = "part";
  84. }
  85. wp = strlen(p);
  86. if (lth) {
  87. snprintf(bufp, bufsiz, "%*.*s%s%-2u",
  88. lth-wp-2, w, dev, p, pno);
  89. } else {
  90. snprintf(bufp, bufsiz, "%.*s%s%-2u", w, dev, p, pno);
  91. }
  92. return bufp;
  93. }
  94. struct partition {
  95. unsigned char boot_ind; /* 0x80 - active */
  96. unsigned char head; /* starting head */
  97. unsigned char sector; /* starting sector */
  98. unsigned char cyl; /* starting cylinder */
  99. unsigned char sys_ind; /* What partition type */
  100. unsigned char end_head; /* end head */
  101. unsigned char end_sector; /* end sector */
  102. unsigned char end_cyl; /* end cylinder */
  103. unsigned char start4[4]; /* starting sector counting from 0 */
  104. unsigned char size4[4]; /* nr of sectors in partition */
  105. } ATTRIBUTE_PACKED;
  106. enum failure {
  107. ioctl_error, unable_to_open, unable_to_read, unable_to_seek,
  108. unable_to_write
  109. };
  110. enum label_type {
  111. label_dos, label_sun, label_sgi, label_aix, label_osf
  112. };
  113. #define LABEL_IS_DOS (label_dos == current_label_type)
  114. #if ENABLE_FEATURE_SUN_LABEL
  115. #define LABEL_IS_SUN (label_sun == current_label_type)
  116. #define STATIC_SUN static
  117. #else
  118. #define LABEL_IS_SUN 0
  119. #define STATIC_SUN extern
  120. #endif
  121. #if ENABLE_FEATURE_SGI_LABEL
  122. #define LABEL_IS_SGI (label_sgi == current_label_type)
  123. #define STATIC_SGI static
  124. #else
  125. #define LABEL_IS_SGI 0
  126. #define STATIC_SGI extern
  127. #endif
  128. #if ENABLE_FEATURE_AIX_LABEL
  129. #define LABEL_IS_AIX (label_aix == current_label_type)
  130. #define STATIC_AIX static
  131. #else
  132. #define LABEL_IS_AIX 0
  133. #define STATIC_AIX extern
  134. #endif
  135. #if ENABLE_FEATURE_OSF_LABEL
  136. #define LABEL_IS_OSF (label_osf == current_label_type)
  137. #define STATIC_OSF static
  138. #else
  139. #define LABEL_IS_OSF 0
  140. #define STATIC_OSF extern
  141. #endif
  142. enum action { fdisk, require, try_only, create_empty_dos, create_empty_sun };
  143. static enum label_type current_label_type;
  144. static const char *disk_device;
  145. static int fd; /* the disk */
  146. static int partitions = 4; /* maximum partition + 1 */
  147. static int display_in_cyl_units = 1;
  148. static unsigned units_per_sector = 1;
  149. #if ENABLE_FEATURE_FDISK_WRITABLE
  150. static void change_units(void);
  151. static void reread_partition_table(int leave);
  152. static void delete_partition(int i);
  153. static int get_partition(int warn, int max);
  154. static void list_types(const struct systypes *sys);
  155. static unsigned read_int(unsigned low, unsigned dflt, unsigned high, unsigned base, char *mesg);
  156. #endif
  157. static const char *partition_type(unsigned char type);
  158. static void fdisk_fatal(enum failure why) ATTRIBUTE_NORETURN;
  159. static void get_geometry(void);
  160. static int get_boot(enum action what);
  161. #define PLURAL 0
  162. #define SINGULAR 1
  163. #define hex_val(c) ({ \
  164. char _c = (c); \
  165. isdigit(_c) ? _c - '0' : \
  166. tolower(_c) + 10 - 'a'; \
  167. })
  168. #define LINE_LENGTH 800
  169. #define pt_offset(b, n) ((struct partition *)((b) + 0x1be + \
  170. (n) * sizeof(struct partition)))
  171. #define sector(s) ((s) & 0x3f)
  172. #define cylinder(s, c) ((c) | (((s) & 0xc0) << 2))
  173. #define hsc2sector(h,s,c) (sector(s) - 1 + sectors * \
  174. ((h) + heads * cylinder(s,c)))
  175. #define set_hsc(h,s,c,sector) { \
  176. s = sector % sectors + 1; \
  177. sector /= sectors; \
  178. h = sector % heads; \
  179. sector /= heads; \
  180. c = sector & 0xff; \
  181. s |= (sector >> 2) & 0xc0; \
  182. }
  183. static unsigned get_start_sect(const struct partition *p);
  184. static unsigned get_nr_sects(const struct partition *p);
  185. /*
  186. * per partition table entry data
  187. *
  188. * The four primary partitions have the same sectorbuffer (MBRbuffer)
  189. * and have NULL ext_pointer.
  190. * Each logical partition table entry has two pointers, one for the
  191. * partition and one link to the next one.
  192. */
  193. static struct pte {
  194. struct partition *part_table; /* points into sectorbuffer */
  195. struct partition *ext_pointer; /* points into sectorbuffer */
  196. #if ENABLE_FEATURE_FDISK_WRITABLE
  197. char changed; /* boolean */
  198. #endif
  199. off_t offset; /* disk sector number */
  200. char *sectorbuffer; /* disk sector contents */
  201. } ptes[MAXIMUM_PARTS];
  202. #if ENABLE_FEATURE_FDISK_WRITABLE
  203. static void
  204. set_all_unchanged(void)
  205. {
  206. int i;
  207. for (i = 0; i < MAXIMUM_PARTS; i++)
  208. ptes[i].changed = 0;
  209. }
  210. static ATTRIBUTE_ALWAYS_INLINE void
  211. set_changed(int i)
  212. {
  213. ptes[i].changed = 1;
  214. }
  215. #endif /* FEATURE_FDISK_WRITABLE */
  216. static ATTRIBUTE_ALWAYS_INLINE struct partition *
  217. get_part_table(int i)
  218. {
  219. return ptes[i].part_table;
  220. }
  221. static const char *
  222. str_units(int n)
  223. { /* n==1: use singular */
  224. if (n == 1)
  225. return display_in_cyl_units ? _("cylinder") : _("sector");
  226. else
  227. return display_in_cyl_units ? _("cylinders") : _("sectors");
  228. }
  229. static int
  230. valid_part_table_flag(const char *mbuffer)
  231. {
  232. return (mbuffer[510] == 0x55 && (uint8_t)mbuffer[511] == 0xaa);
  233. }
  234. #if ENABLE_FEATURE_FDISK_WRITABLE
  235. static ATTRIBUTE_ALWAYS_INLINE void
  236. write_part_table_flag(char *b)
  237. {
  238. b[510] = 0x55;
  239. b[511] = 0xaa;
  240. }
  241. static char line_buffer[LINE_LENGTH];
  242. static char *line_ptr;
  243. /* read line; return 0 or first char */
  244. static int
  245. read_line(void)
  246. {
  247. fflush(stdout); /* requested by niles@scyld.com */
  248. line_ptr = line_buffer;
  249. if (!fgets(line_buffer, LINE_LENGTH, stdin)) {
  250. /* error or eof */
  251. bb_error_msg_and_die("\ngot EOF, exiting");
  252. }
  253. while (*line_ptr && !isgraph(*line_ptr))
  254. line_ptr++;
  255. return *line_ptr;
  256. }
  257. static char
  258. read_nonempty(const char *mesg)
  259. {
  260. do {
  261. fputs(mesg, stdout);
  262. } while (!read_line());
  263. return *line_ptr;
  264. }
  265. static char
  266. read_maybe_empty(const char *mesg)
  267. {
  268. fputs(mesg, stdout);
  269. if (!read_line()) {
  270. line_ptr = line_buffer;
  271. *line_ptr = '\n';
  272. line_ptr[1] = 0;
  273. }
  274. return *line_ptr;
  275. }
  276. static int
  277. read_hex(const struct systypes *sys)
  278. {
  279. unsigned long v;
  280. while (1) {
  281. read_nonempty(_("Hex code (type L to list codes): "));
  282. if (*line_ptr == 'l' || *line_ptr == 'L') {
  283. list_types(sys);
  284. continue;
  285. }
  286. v = bb_strtoul(line_ptr, NULL, 16);
  287. if (v > 0xff)
  288. /* Bad input also triggers this */
  289. continue;
  290. return v;
  291. }
  292. }
  293. #endif /* FEATURE_FDISK_WRITABLE */
  294. #include "fdisk_aix.c"
  295. typedef struct {
  296. unsigned char info[128]; /* Informative text string */
  297. unsigned char spare0[14];
  298. struct sun_info {
  299. unsigned char spare1;
  300. unsigned char id;
  301. unsigned char spare2;
  302. unsigned char flags;
  303. } infos[8];
  304. unsigned char spare1[246]; /* Boot information etc. */
  305. unsigned short rspeed; /* Disk rotational speed */
  306. unsigned short pcylcount; /* Physical cylinder count */
  307. unsigned short sparecyl; /* extra sects per cylinder */
  308. unsigned char spare2[4]; /* More magic... */
  309. unsigned short ilfact; /* Interleave factor */
  310. unsigned short ncyl; /* Data cylinder count */
  311. unsigned short nacyl; /* Alt. cylinder count */
  312. unsigned short ntrks; /* Tracks per cylinder */
  313. unsigned short nsect; /* Sectors per track */
  314. unsigned char spare3[4]; /* Even more magic... */
  315. struct sun_partinfo {
  316. uint32_t start_cylinder;
  317. uint32_t num_sectors;
  318. } partitions[8];
  319. unsigned short magic; /* Magic number */
  320. unsigned short csum; /* Label xor'd checksum */
  321. } sun_partition;
  322. #define sunlabel ((sun_partition *)MBRbuffer)
  323. #define SUNOS_SWAP 3
  324. #define SUN_WHOLE_DISK 5
  325. STATIC_OSF void bsd_select(void);
  326. STATIC_OSF void xbsd_print_disklabel(int);
  327. #include "fdisk_osf.c"
  328. #define SGI_VOLHDR 0x00
  329. /* 1 and 2 were used for drive types no longer supported by SGI */
  330. #define SGI_SWAP 0x03
  331. /* 4 and 5 were for filesystem types SGI haven't ever supported on MIPS CPUs */
  332. #define SGI_VOLUME 0x06
  333. #define SGI_EFS 0x07
  334. #define SGI_LVOL 0x08
  335. #define SGI_RLVOL 0x09
  336. #define SGI_XFS 0x0a
  337. #define SGI_XFSLOG 0x0b
  338. #define SGI_XLV 0x0c
  339. #define SGI_XVM 0x0d
  340. #define SGI_ENTIRE_DISK SGI_VOLUME
  341. #if ENABLE_FEATURE_SGI_LABEL || ENABLE_FEATURE_SUN_LABEL
  342. static uint16_t
  343. fdisk_swap16(uint16_t x)
  344. {
  345. return (x << 8) | (x >> 8);
  346. }
  347. static uint32_t
  348. fdisk_swap32(uint32_t x)
  349. {
  350. return (x << 24) |
  351. ((x & 0xFF00) << 8) |
  352. ((x & 0xFF0000) >> 8) |
  353. (x >> 24);
  354. }
  355. #endif
  356. STATIC_SGI const struct systypes sgi_sys_types[];
  357. STATIC_SGI unsigned sgi_get_num_sectors(int i);
  358. STATIC_SGI int sgi_get_sysid(int i);
  359. STATIC_SGI void sgi_delete_partition(int i);
  360. STATIC_SGI void sgi_change_sysid(int i, int sys);
  361. STATIC_SGI void sgi_list_table(int xtra);
  362. #if ENABLE_FEATURE_FDISK_ADVANCED
  363. STATIC_SGI void sgi_set_xcyl(void);
  364. #endif
  365. STATIC_SGI int verify_sgi(int verbose);
  366. STATIC_SGI void sgi_add_partition(int n, int sys);
  367. STATIC_SGI void sgi_set_swappartition(int i);
  368. STATIC_SGI const char *sgi_get_bootfile(void);
  369. STATIC_SGI void sgi_set_bootfile(const char* aFile);
  370. STATIC_SGI void create_sgiinfo(void);
  371. STATIC_SGI void sgi_write_table(void);
  372. STATIC_SGI void sgi_set_bootpartition(int i);
  373. #include "fdisk_sgi.c"
  374. STATIC_SUN const struct systypes sun_sys_types[];
  375. STATIC_SUN void sun_delete_partition(int i);
  376. STATIC_SUN void sun_change_sysid(int i, int sys);
  377. STATIC_SUN void sun_list_table(int xtra);
  378. STATIC_SUN void add_sun_partition(int n, int sys);
  379. #if ENABLE_FEATURE_FDISK_ADVANCED
  380. STATIC_SUN void sun_set_alt_cyl(void);
  381. STATIC_SUN void sun_set_ncyl(int cyl);
  382. STATIC_SUN void sun_set_xcyl(void);
  383. STATIC_SUN void sun_set_ilfact(void);
  384. STATIC_SUN void sun_set_rspeed(void);
  385. STATIC_SUN void sun_set_pcylcount(void);
  386. #endif
  387. STATIC_SUN void toggle_sunflags(int i, unsigned char mask);
  388. STATIC_SUN void verify_sun(void);
  389. STATIC_SUN void sun_write_table(void);
  390. #include "fdisk_sun.c"
  391. /* DOS partition types */
  392. static const struct systypes i386_sys_types[] = {
  393. { "\x00" "Empty" },
  394. { "\x01" "FAT12" },
  395. { "\x04" "FAT16 <32M" },
  396. { "\x05" "Extended" }, /* DOS 3.3+ extended partition */
  397. { "\x06" "FAT16" }, /* DOS 16-bit >=32M */
  398. { "\x07" "HPFS/NTFS" }, /* OS/2 IFS, eg, HPFS or NTFS or QNX */
  399. { "\x0a" "OS/2 Boot Manager" },/* OS/2 Boot Manager */
  400. { "\x0b" "Win95 FAT32" },
  401. { "\x0c" "Win95 FAT32 (LBA)" },/* LBA really is 'Extended Int 13h' */
  402. { "\x0e" "Win95 FAT16 (LBA)" },
  403. { "\x0f" "Win95 Ext'd (LBA)" },
  404. { "\x11" "Hidden FAT12" },
  405. { "\x12" "Compaq diagnostics" },
  406. { "\x14" "Hidden FAT16 <32M" },
  407. { "\x16" "Hidden FAT16" },
  408. { "\x17" "Hidden HPFS/NTFS" },
  409. { "\x1b" "Hidden Win95 FAT32" },
  410. { "\x1c" "Hidden Win95 FAT32 (LBA)" },
  411. { "\x1e" "Hidden Win95 FAT16 (LBA)" },
  412. { "\x3c" "PartitionMagic recovery" },
  413. { "\x41" "PPC PReP Boot" },
  414. { "\x42" "SFS" },
  415. { "\x63" "GNU HURD or SysV" }, /* GNU HURD or Mach or Sys V/386 (such as ISC UNIX) */
  416. { "\x80" "Old Minix" }, /* Minix 1.4a and earlier */
  417. { "\x81" "Minix / old Linux" },/* Minix 1.4b and later */
  418. { "\x82" "Linux swap" }, /* also Solaris */
  419. { "\x83" "Linux" },
  420. { "\x84" "OS/2 hidden C: drive" },
  421. { "\x85" "Linux extended" },
  422. { "\x86" "NTFS volume set" },
  423. { "\x87" "NTFS volume set" },
  424. { "\x8e" "Linux LVM" },
  425. { "\x9f" "BSD/OS" }, /* BSDI */
  426. { "\xa0" "IBM Thinkpad hibernation" },
  427. { "\xa5" "FreeBSD" }, /* various BSD flavours */
  428. { "\xa6" "OpenBSD" },
  429. { "\xa8" "Darwin UFS" },
  430. { "\xa9" "NetBSD" },
  431. { "\xab" "Darwin boot" },
  432. { "\xb7" "BSDI fs" },
  433. { "\xb8" "BSDI swap" },
  434. { "\xbe" "Solaris boot" },
  435. { "\xeb" "BeOS fs" },
  436. { "\xee" "EFI GPT" }, /* Intel EFI GUID Partition Table */
  437. { "\xef" "EFI (FAT-12/16/32)" },/* Intel EFI System Partition */
  438. { "\xf0" "Linux/PA-RISC boot" },/* Linux/PA-RISC boot loader */
  439. { "\xf2" "DOS secondary" }, /* DOS 3.3+ secondary */
  440. { "\xfd" "Linux raid autodetect" },/* New (2.2.x) raid partition with
  441. autodetect using persistent
  442. superblock */
  443. #if 0 /* ENABLE_WEIRD_PARTITION_TYPES */
  444. { "\x02" "XENIX root" },
  445. { "\x03" "XENIX usr" },
  446. { "\x08" "AIX" }, /* AIX boot (AIX -- PS/2 port) or SplitDrive */
  447. { "\x09" "AIX bootable" }, /* AIX data or Coherent */
  448. { "\x10" "OPUS" },
  449. { "\x18" "AST SmartSleep" },
  450. { "\x24" "NEC DOS" },
  451. { "\x39" "Plan 9" },
  452. { "\x40" "Venix 80286" },
  453. { "\x4d" "QNX4.x" },
  454. { "\x4e" "QNX4.x 2nd part" },
  455. { "\x4f" "QNX4.x 3rd part" },
  456. { "\x50" "OnTrack DM" },
  457. { "\x51" "OnTrack DM6 Aux1" }, /* (or Novell) */
  458. { "\x52" "CP/M" }, /* CP/M or Microport SysV/AT */
  459. { "\x53" "OnTrack DM6 Aux3" },
  460. { "\x54" "OnTrackDM6" },
  461. { "\x55" "EZ-Drive" },
  462. { "\x56" "Golden Bow" },
  463. { "\x5c" "Priam Edisk" },
  464. { "\x61" "SpeedStor" },
  465. { "\x64" "Novell Netware 286" },
  466. { "\x65" "Novell Netware 386" },
  467. { "\x70" "DiskSecure Multi-Boot" },
  468. { "\x75" "PC/IX" },
  469. { "\x93" "Amoeba" },
  470. { "\x94" "Amoeba BBT" }, /* (bad block table) */
  471. { "\xa7" "NeXTSTEP" },
  472. { "\xbb" "Boot Wizard hidden" },
  473. { "\xc1" "DRDOS/sec (FAT-12)" },
  474. { "\xc4" "DRDOS/sec (FAT-16 < 32M)" },
  475. { "\xc6" "DRDOS/sec (FAT-16)" },
  476. { "\xc7" "Syrinx" },
  477. { "\xda" "Non-FS data" },
  478. { "\xdb" "CP/M / CTOS / ..." },/* CP/M or Concurrent CP/M or
  479. Concurrent DOS or CTOS */
  480. { "\xde" "Dell Utility" }, /* Dell PowerEdge Server utilities */
  481. { "\xdf" "BootIt" }, /* BootIt EMBRM */
  482. { "\xe1" "DOS access" }, /* DOS access or SpeedStor 12-bit FAT
  483. extended partition */
  484. { "\xe3" "DOS R/O" }, /* DOS R/O or SpeedStor */
  485. { "\xe4" "SpeedStor" }, /* SpeedStor 16-bit FAT extended
  486. partition < 1024 cyl. */
  487. { "\xf1" "SpeedStor" },
  488. { "\xf4" "SpeedStor" }, /* SpeedStor large partition */
  489. { "\xfe" "LANstep" }, /* SpeedStor >1024 cyl. or LANstep */
  490. { "\xff" "BBT" }, /* Xenix Bad Block Table */
  491. #endif
  492. { 0 }
  493. };
  494. #if ENABLE_FEATURE_FDISK_WRITABLE
  495. /* start_sect and nr_sects are stored little endian on all machines */
  496. /* moreover, they are not aligned correctly */
  497. static void
  498. store4_little_endian(unsigned char *cp, unsigned val)
  499. {
  500. cp[0] = val;
  501. cp[1] = val >> 8;
  502. cp[2] = val >> 16;
  503. cp[3] = val >> 24;
  504. }
  505. #endif /* FEATURE_FDISK_WRITABLE */
  506. static unsigned
  507. read4_little_endian(const unsigned char *cp)
  508. {
  509. return cp[0] + (cp[1] << 8) + (cp[2] << 16) + (cp[3] << 24);
  510. }
  511. #if ENABLE_FEATURE_FDISK_WRITABLE
  512. static void
  513. set_start_sect(struct partition *p, unsigned start_sect)
  514. {
  515. store4_little_endian(p->start4, start_sect);
  516. }
  517. #endif
  518. static unsigned
  519. get_start_sect(const struct partition *p)
  520. {
  521. return read4_little_endian(p->start4);
  522. }
  523. #if ENABLE_FEATURE_FDISK_WRITABLE
  524. static void
  525. set_nr_sects(struct partition *p, unsigned nr_sects)
  526. {
  527. store4_little_endian(p->size4, nr_sects);
  528. }
  529. #endif
  530. static unsigned
  531. get_nr_sects(const struct partition *p)
  532. {
  533. return read4_little_endian(p->size4);
  534. }
  535. /* normally O_RDWR, -l option gives O_RDONLY */
  536. static int type_open = O_RDWR;
  537. static int ext_index; /* the prime extended partition */
  538. static int listing; /* no aborts for fdisk -l */
  539. static int dos_compatible_flag = ~0;
  540. #if ENABLE_FEATURE_FDISK_WRITABLE
  541. static int dos_changed;
  542. static int nowarn; /* no warnings for fdisk -l/-s */
  543. #endif
  544. static unsigned user_cylinders, user_heads, user_sectors;
  545. static unsigned pt_heads, pt_sectors;
  546. static unsigned kern_heads, kern_sectors;
  547. static off_t extended_offset; /* offset of link pointers */
  548. static unsigned long long total_number_of_sectors;
  549. static jmp_buf listingbuf;
  550. static void fdisk_fatal(enum failure why)
  551. {
  552. const char *message;
  553. if (listing) {
  554. close(fd);
  555. longjmp(listingbuf, 1);
  556. }
  557. switch (why) {
  558. case unable_to_open:
  559. message = "\nUnable to open %s";
  560. break;
  561. case unable_to_read:
  562. message = "\nUnable to read %s";
  563. break;
  564. case unable_to_seek:
  565. message = "\nUnable to seek on %s";
  566. break;
  567. case unable_to_write:
  568. message = "\nUnable to write %s";
  569. break;
  570. case ioctl_error:
  571. message = "\nBLKGETSIZE ioctl failed on %s";
  572. break;
  573. default:
  574. message = "\nFatal error";
  575. }
  576. bb_error_msg_and_die(message, disk_device);
  577. }
  578. static void
  579. seek_sector(off_t secno)
  580. {
  581. off_t offset = secno * sector_size;
  582. if (lseek(fd, offset, SEEK_SET) == (off_t) -1)
  583. fdisk_fatal(unable_to_seek);
  584. }
  585. #if ENABLE_FEATURE_FDISK_WRITABLE
  586. static void
  587. write_sector(off_t secno, char *buf)
  588. {
  589. seek_sector(secno);
  590. if (write(fd, buf, sector_size) != sector_size)
  591. fdisk_fatal(unable_to_write);
  592. }
  593. #endif
  594. /* Allocate a buffer and read a partition table sector */
  595. static void
  596. read_pte(struct pte *pe, off_t offset)
  597. {
  598. pe->offset = offset;
  599. pe->sectorbuffer = xmalloc(sector_size);
  600. seek_sector(offset);
  601. if (read(fd, pe->sectorbuffer, sector_size) != sector_size)
  602. fdisk_fatal(unable_to_read);
  603. #if ENABLE_FEATURE_FDISK_WRITABLE
  604. pe->changed = 0;
  605. #endif
  606. pe->part_table = pe->ext_pointer = NULL;
  607. }
  608. static unsigned
  609. get_partition_start(const struct pte *pe)
  610. {
  611. return pe->offset + get_start_sect(pe->part_table);
  612. }
  613. #if ENABLE_FEATURE_FDISK_WRITABLE
  614. /*
  615. * Avoid warning about DOS partitions when no DOS partition was changed.
  616. * Here a heuristic "is probably dos partition".
  617. * We might also do the opposite and warn in all cases except
  618. * for "is probably nondos partition".
  619. */
  620. static int
  621. is_dos_partition(int t)
  622. {
  623. return (t == 1 || t == 4 || t == 6 ||
  624. t == 0x0b || t == 0x0c || t == 0x0e ||
  625. t == 0x11 || t == 0x12 || t == 0x14 || t == 0x16 ||
  626. t == 0x1b || t == 0x1c || t == 0x1e || t == 0x24 ||
  627. t == 0xc1 || t == 0xc4 || t == 0xc6);
  628. }
  629. static void
  630. menu(void)
  631. {
  632. if (LABEL_IS_SUN) {
  633. puts(_("Command action"));
  634. puts(_("\ta\ttoggle a read only flag")); /* sun */
  635. puts(_("\tb\tedit bsd disklabel"));
  636. puts(_("\tc\ttoggle the mountable flag")); /* sun */
  637. puts(_("\td\tdelete a partition"));
  638. puts(_("\tl\tlist known partition types"));
  639. puts(_("\tm\tprint this menu"));
  640. puts(_("\tn\tadd a new partition"));
  641. puts(_("\to\tcreate a new empty DOS partition table"));
  642. puts(_("\tp\tprint the partition table"));
  643. puts(_("\tq\tquit without saving changes"));
  644. puts(_("\ts\tcreate a new empty Sun disklabel")); /* sun */
  645. puts(_("\tt\tchange a partition's system id"));
  646. puts(_("\tu\tchange display/entry units"));
  647. puts(_("\tv\tverify the partition table"));
  648. puts(_("\tw\twrite table to disk and exit"));
  649. #if ENABLE_FEATURE_FDISK_ADVANCED
  650. puts(_("\tx\textra functionality (experts only)"));
  651. #endif
  652. } else
  653. if (LABEL_IS_SGI) {
  654. puts(_("Command action"));
  655. puts(_("\ta\tselect bootable partition")); /* sgi flavour */
  656. puts(_("\tb\tedit bootfile entry")); /* sgi */
  657. puts(_("\tc\tselect sgi swap partition")); /* sgi flavour */
  658. puts(_("\td\tdelete a partition"));
  659. puts(_("\tl\tlist known partition types"));
  660. puts(_("\tm\tprint this menu"));
  661. puts(_("\tn\tadd a new partition"));
  662. puts(_("\to\tcreate a new empty DOS partition table"));
  663. puts(_("\tp\tprint the partition table"));
  664. puts(_("\tq\tquit without saving changes"));
  665. puts(_("\ts\tcreate a new empty Sun disklabel")); /* sun */
  666. puts(_("\tt\tchange a partition's system id"));
  667. puts(_("\tu\tchange display/entry units"));
  668. puts(_("\tv\tverify the partition table"));
  669. puts(_("\tw\twrite table to disk and exit"));
  670. } else
  671. if (LABEL_IS_AIX) {
  672. puts(_("Command action"));
  673. puts(_("\tm\tprint this menu"));
  674. puts(_("\to\tcreate a new empty DOS partition table"));
  675. puts(_("\tq\tquit without saving changes"));
  676. puts(_("\ts\tcreate a new empty Sun disklabel")); /* sun */
  677. } else
  678. {
  679. puts(_("Command action"));
  680. puts(_("\ta\ttoggle a bootable flag"));
  681. puts(_("\tb\tedit bsd disklabel"));
  682. puts(_("\tc\ttoggle the dos compatibility flag"));
  683. puts(_("\td\tdelete a partition"));
  684. puts(_("\tl\tlist known partition types"));
  685. puts(_("\tm\tprint this menu"));
  686. puts(_("\tn\tadd a new partition"));
  687. puts(_("\to\tcreate a new empty DOS partition table"));
  688. puts(_("\tp\tprint the partition table"));
  689. puts(_("\tq\tquit without saving changes"));
  690. puts(_("\ts\tcreate a new empty Sun disklabel")); /* sun */
  691. puts(_("\tt\tchange a partition's system id"));
  692. puts(_("\tu\tchange display/entry units"));
  693. puts(_("\tv\tverify the partition table"));
  694. puts(_("\tw\twrite table to disk and exit"));
  695. #if ENABLE_FEATURE_FDISK_ADVANCED
  696. puts(_("\tx\textra functionality (experts only)"));
  697. #endif
  698. }
  699. }
  700. #endif /* FEATURE_FDISK_WRITABLE */
  701. #if ENABLE_FEATURE_FDISK_ADVANCED
  702. static void
  703. xmenu(void)
  704. {
  705. if (LABEL_IS_SUN) {
  706. puts(_("Command action"));
  707. puts(_("\ta\tchange number of alternate cylinders")); /*sun*/
  708. puts(_("\tc\tchange number of cylinders"));
  709. puts(_("\td\tprint the raw data in the partition table"));
  710. puts(_("\te\tchange number of extra sectors per cylinder"));/*sun*/
  711. puts(_("\th\tchange number of heads"));
  712. puts(_("\ti\tchange interleave factor")); /*sun*/
  713. puts(_("\to\tchange rotation speed (rpm)")); /*sun*/
  714. puts(_("\tm\tprint this menu"));
  715. puts(_("\tp\tprint the partition table"));
  716. puts(_("\tq\tquit without saving changes"));
  717. puts(_("\tr\treturn to main menu"));
  718. puts(_("\ts\tchange number of sectors/track"));
  719. puts(_("\tv\tverify the partition table"));
  720. puts(_("\tw\twrite table to disk and exit"));
  721. puts(_("\ty\tchange number of physical cylinders")); /*sun*/
  722. } else
  723. if (LABEL_IS_SGI) {
  724. puts(_("Command action"));
  725. puts(_("\tb\tmove beginning of data in a partition")); /* !sun */
  726. puts(_("\tc\tchange number of cylinders"));
  727. puts(_("\td\tprint the raw data in the partition table"));
  728. puts(_("\te\tlist extended partitions")); /* !sun */
  729. puts(_("\tg\tcreate an IRIX (SGI) partition table"));/* sgi */
  730. puts(_("\th\tchange number of heads"));
  731. puts(_("\tm\tprint this menu"));
  732. puts(_("\tp\tprint the partition table"));
  733. puts(_("\tq\tquit without saving changes"));
  734. puts(_("\tr\treturn to main menu"));
  735. puts(_("\ts\tchange number of sectors/track"));
  736. puts(_("\tv\tverify the partition table"));
  737. puts(_("\tw\twrite table to disk and exit"));
  738. } else
  739. if (LABEL_IS_AIX) {
  740. puts(_("Command action"));
  741. puts(_("\tb\tmove beginning of data in a partition")); /* !sun */
  742. puts(_("\tc\tchange number of cylinders"));
  743. puts(_("\td\tprint the raw data in the partition table"));
  744. puts(_("\te\tlist extended partitions")); /* !sun */
  745. puts(_("\tg\tcreate an IRIX (SGI) partition table"));/* sgi */
  746. puts(_("\th\tchange number of heads"));
  747. puts(_("\tm\tprint this menu"));
  748. puts(_("\tp\tprint the partition table"));
  749. puts(_("\tq\tquit without saving changes"));
  750. puts(_("\tr\treturn to main menu"));
  751. puts(_("\ts\tchange number of sectors/track"));
  752. puts(_("\tv\tverify the partition table"));
  753. puts(_("\tw\twrite table to disk and exit"));
  754. } else {
  755. puts(_("Command action"));
  756. puts(_("\tb\tmove beginning of data in a partition")); /* !sun */
  757. puts(_("\tc\tchange number of cylinders"));
  758. puts(_("\td\tprint the raw data in the partition table"));
  759. puts(_("\te\tlist extended partitions")); /* !sun */
  760. puts(_("\tf\tfix partition order")); /* !sun, !aix, !sgi */
  761. #if ENABLE_FEATURE_SGI_LABEL
  762. puts(_("\tg\tcreate an IRIX (SGI) partition table"));/* sgi */
  763. #endif
  764. puts(_("\th\tchange number of heads"));
  765. puts(_("\tm\tprint this menu"));
  766. puts(_("\tp\tprint the partition table"));
  767. puts(_("\tq\tquit without saving changes"));
  768. puts(_("\tr\treturn to main menu"));
  769. puts(_("\ts\tchange number of sectors/track"));
  770. puts(_("\tv\tverify the partition table"));
  771. puts(_("\tw\twrite table to disk and exit"));
  772. }
  773. }
  774. #endif /* ADVANCED mode */
  775. #if ENABLE_FEATURE_FDISK_WRITABLE
  776. static const struct systypes *
  777. get_sys_types(void)
  778. {
  779. return (
  780. LABEL_IS_SUN ? sun_sys_types :
  781. LABEL_IS_SGI ? sgi_sys_types :
  782. i386_sys_types);
  783. }
  784. #else
  785. #define get_sys_types() i386_sys_types
  786. #endif /* FEATURE_FDISK_WRITABLE */
  787. static const char *partition_type(unsigned char type)
  788. {
  789. int i;
  790. const struct systypes *types = get_sys_types();
  791. for (i = 0; types[i].name; i++)
  792. if ((unsigned char )types[i].name[0] == type)
  793. return types[i].name + 1;
  794. return _("Unknown");
  795. }
  796. #if ENABLE_FEATURE_FDISK_WRITABLE
  797. static int
  798. get_sysid(int i)
  799. {
  800. return LABEL_IS_SUN ? sunlabel->infos[i].id :
  801. (LABEL_IS_SGI ? sgi_get_sysid(i) :
  802. ptes[i].part_table->sys_ind);
  803. }
  804. void list_types(const struct systypes *sys)
  805. {
  806. unsigned last[4], done = 0, next = 0, size;
  807. int i;
  808. for (i = 0; sys[i].name; i++);
  809. size = i;
  810. for (i = 3; i >= 0; i--)
  811. last[3 - i] = done += (size + i - done) / (i + 1);
  812. i = done = 0;
  813. do {
  814. printf("%c%2x %-15.15s", i ? ' ' : '\n',
  815. (unsigned char)sys[next].name[0],
  816. partition_type((unsigned char)sys[next].name[0]));
  817. next = last[i++] + done;
  818. if (i > 3 || next >= last[i]) {
  819. i = 0;
  820. next = ++done;
  821. }
  822. } while (done < last[0]);
  823. putchar('\n');
  824. }
  825. #endif /* FEATURE_FDISK_WRITABLE */
  826. static int
  827. is_cleared_partition(const struct partition *p)
  828. {
  829. return !(!p || p->boot_ind || p->head || p->sector || p->cyl ||
  830. p->sys_ind || p->end_head || p->end_sector || p->end_cyl ||
  831. get_start_sect(p) || get_nr_sects(p));
  832. }
  833. static void
  834. clear_partition(struct partition *p)
  835. {
  836. if (!p)
  837. return;
  838. memset(p, 0, sizeof(struct partition));
  839. }
  840. #if ENABLE_FEATURE_FDISK_WRITABLE
  841. static void
  842. set_partition(int i, int doext, off_t start, off_t stop, int sysid)
  843. {
  844. struct partition *p;
  845. off_t offset;
  846. if (doext) {
  847. p = ptes[i].ext_pointer;
  848. offset = extended_offset;
  849. } else {
  850. p = ptes[i].part_table;
  851. offset = ptes[i].offset;
  852. }
  853. p->boot_ind = 0;
  854. p->sys_ind = sysid;
  855. set_start_sect(p, start - offset);
  856. set_nr_sects(p, stop - start + 1);
  857. if (dos_compatible_flag && (start/(sectors*heads) > 1023))
  858. start = heads*sectors*1024 - 1;
  859. set_hsc(p->head, p->sector, p->cyl, start);
  860. if (dos_compatible_flag && (stop/(sectors*heads) > 1023))
  861. stop = heads*sectors*1024 - 1;
  862. set_hsc(p->end_head, p->end_sector, p->end_cyl, stop);
  863. ptes[i].changed = 1;
  864. }
  865. #endif
  866. static int
  867. test_c(const char **m, const char *mesg)
  868. {
  869. int val = 0;
  870. if (!*m)
  871. printf(_("You must set"));
  872. else {
  873. printf(" %s", *m);
  874. val = 1;
  875. }
  876. *m = mesg;
  877. return val;
  878. }
  879. static int
  880. warn_geometry(void)
  881. {
  882. const char *m = NULL;
  883. int prev = 0;
  884. if (!heads)
  885. prev = test_c(&m, _("heads"));
  886. if (!sectors)
  887. prev = test_c(&m, _("sectors"));
  888. if (!cylinders)
  889. prev = test_c(&m, _("cylinders"));
  890. if (!m)
  891. return 0;
  892. printf("%s%s.\n"
  893. #if ENABLE_FEATURE_FDISK_WRITABLE
  894. "You can do this from the extra functions menu.\n"
  895. #endif
  896. , prev ? _(" and ") : " ", m);
  897. return 1;
  898. }
  899. static void update_units(void)
  900. {
  901. int cyl_units = heads * sectors;
  902. if (display_in_cyl_units && cyl_units)
  903. units_per_sector = cyl_units;
  904. else
  905. units_per_sector = 1; /* in sectors */
  906. }
  907. #if ENABLE_FEATURE_FDISK_WRITABLE
  908. static void
  909. warn_cylinders(void)
  910. {
  911. if (LABEL_IS_DOS && cylinders > 1024 && !nowarn)
  912. printf(_("\n"
  913. "The number of cylinders for this disk is set to %d.\n"
  914. "There is nothing wrong with that, but this is larger than 1024,\n"
  915. "and could in certain setups cause problems with:\n"
  916. "1) software that runs at boot time (e.g., old versions of LILO)\n"
  917. "2) booting and partitioning software from other OSs\n"
  918. " (e.g., DOS FDISK, OS/2 FDISK)\n"),
  919. cylinders);
  920. }
  921. #endif
  922. static void
  923. read_extended(int ext)
  924. {
  925. int i;
  926. struct pte *pex;
  927. struct partition *p, *q;
  928. ext_index = ext;
  929. pex = &ptes[ext];
  930. pex->ext_pointer = pex->part_table;
  931. p = pex->part_table;
  932. if (!get_start_sect(p)) {
  933. printf(_("Bad offset in primary extended partition\n"));
  934. return;
  935. }
  936. while (IS_EXTENDED(p->sys_ind)) {
  937. struct pte *pe = &ptes[partitions];
  938. if (partitions >= MAXIMUM_PARTS) {
  939. /* This is not a Linux restriction, but
  940. this program uses arrays of size MAXIMUM_PARTS.
  941. Do not try to 'improve' this test. */
  942. struct pte *pre = &ptes[partitions-1];
  943. #if ENABLE_FEATURE_FDISK_WRITABLE
  944. printf(_("Warning: deleting partitions after %d\n"),
  945. partitions);
  946. pre->changed = 1;
  947. #endif
  948. clear_partition(pre->ext_pointer);
  949. return;
  950. }
  951. read_pte(pe, extended_offset + get_start_sect(p));
  952. if (!extended_offset)
  953. extended_offset = get_start_sect(p);
  954. q = p = pt_offset(pe->sectorbuffer, 0);
  955. for (i = 0; i < 4; i++, p++) if (get_nr_sects(p)) {
  956. if (IS_EXTENDED(p->sys_ind)) {
  957. if (pe->ext_pointer)
  958. printf(_("Warning: extra link "
  959. "pointer in partition table"
  960. " %d\n"), partitions + 1);
  961. else
  962. pe->ext_pointer = p;
  963. } else if (p->sys_ind) {
  964. if (pe->part_table)
  965. printf(_("Warning: ignoring extra "
  966. "data in partition table"
  967. " %d\n"), partitions + 1);
  968. else
  969. pe->part_table = p;
  970. }
  971. }
  972. /* very strange code here... */
  973. if (!pe->part_table) {
  974. if (q != pe->ext_pointer)
  975. pe->part_table = q;
  976. else
  977. pe->part_table = q + 1;
  978. }
  979. if (!pe->ext_pointer) {
  980. if (q != pe->part_table)
  981. pe->ext_pointer = q;
  982. else
  983. pe->ext_pointer = q + 1;
  984. }
  985. p = pe->ext_pointer;
  986. partitions++;
  987. }
  988. #if ENABLE_FEATURE_FDISK_WRITABLE
  989. /* remove empty links */
  990. remove:
  991. for (i = 4; i < partitions; i++) {
  992. struct pte *pe = &ptes[i];
  993. if (!get_nr_sects(pe->part_table) &&
  994. (partitions > 5 || ptes[4].part_table->sys_ind)) {
  995. printf("omitting empty partition (%d)\n", i+1);
  996. delete_partition(i);
  997. goto remove; /* numbering changed */
  998. }
  999. }
  1000. #endif
  1001. }
  1002. #if ENABLE_FEATURE_FDISK_WRITABLE
  1003. static void
  1004. create_doslabel(void)
  1005. {
  1006. int i;
  1007. printf(
  1008. _("Building a new DOS disklabel. Changes will remain in memory only,\n"
  1009. "until you decide to write them. After that, of course, the previous\n"
  1010. "content won't be recoverable.\n\n"));
  1011. current_label_type = label_dos;
  1012. #if ENABLE_FEATURE_OSF_LABEL
  1013. possibly_osf_label = 0;
  1014. #endif
  1015. partitions = 4;
  1016. for (i = 510-64; i < 510; i++)
  1017. MBRbuffer[i] = 0;
  1018. write_part_table_flag(MBRbuffer);
  1019. extended_offset = 0;
  1020. set_all_unchanged();
  1021. set_changed(0);
  1022. get_boot(create_empty_dos);
  1023. }
  1024. #endif /* FEATURE_FDISK_WRITABLE */
  1025. static void
  1026. get_sectorsize(void)
  1027. {
  1028. if (!user_set_sector_size) {
  1029. int arg;
  1030. if (ioctl(fd, BLKSSZGET, &arg) == 0)
  1031. sector_size = arg;
  1032. if (sector_size != DEFAULT_SECTOR_SIZE)
  1033. printf(_("Note: sector size is %d (not %d)\n"),
  1034. sector_size, DEFAULT_SECTOR_SIZE);
  1035. }
  1036. }
  1037. static void
  1038. get_kernel_geometry(void)
  1039. {
  1040. struct hd_geometry geometry;
  1041. if (!ioctl(fd, HDIO_GETGEO, &geometry)) {
  1042. kern_heads = geometry.heads;
  1043. kern_sectors = geometry.sectors;
  1044. /* never use geometry.cylinders - it is truncated */
  1045. }
  1046. }
  1047. static void
  1048. get_partition_table_geometry(void)
  1049. {
  1050. const unsigned char *bufp = (const unsigned char *)MBRbuffer;
  1051. struct partition *p;
  1052. int i, h, s, hh, ss;
  1053. int first = 1;
  1054. int bad = 0;
  1055. if (!(valid_part_table_flag((char*)bufp)))
  1056. return;
  1057. hh = ss = 0;
  1058. for (i = 0; i < 4; i++) {
  1059. p = pt_offset(bufp, i);
  1060. if (p->sys_ind != 0) {
  1061. h = p->end_head + 1;
  1062. s = (p->end_sector & 077);
  1063. if (first) {
  1064. hh = h;
  1065. ss = s;
  1066. first = 0;
  1067. } else if (hh != h || ss != s)
  1068. bad = 1;
  1069. }
  1070. }
  1071. if (!first && !bad) {
  1072. pt_heads = hh;
  1073. pt_sectors = ss;
  1074. }
  1075. }
  1076. static void
  1077. get_geometry(void)
  1078. {
  1079. int sec_fac;
  1080. unsigned long long bytes; /* really u64 */
  1081. get_sectorsize();
  1082. sec_fac = sector_size / 512;
  1083. #if ENABLE_FEATURE_SUN_LABEL
  1084. guess_device_type();
  1085. #endif
  1086. heads = cylinders = sectors = 0;
  1087. kern_heads = kern_sectors = 0;
  1088. pt_heads = pt_sectors = 0;
  1089. get_kernel_geometry();
  1090. get_partition_table_geometry();
  1091. heads = user_heads ? user_heads :
  1092. pt_heads ? pt_heads :
  1093. kern_heads ? kern_heads : 255;
  1094. sectors = user_sectors ? user_sectors :
  1095. pt_sectors ? pt_sectors :
  1096. kern_sectors ? kern_sectors : 63;
  1097. if (ioctl(fd, BLKGETSIZE64, &bytes) == 0) {
  1098. /* got bytes */
  1099. } else {
  1100. unsigned long longsectors;
  1101. if (ioctl(fd, BLKGETSIZE, &longsectors))
  1102. longsectors = 0;
  1103. bytes = ((unsigned long long) longsectors) << 9;
  1104. }
  1105. total_number_of_sectors = (bytes >> 9);
  1106. sector_offset = 1;
  1107. if (dos_compatible_flag)
  1108. sector_offset = sectors;
  1109. cylinders = total_number_of_sectors / (heads * sectors * sec_fac);
  1110. if (!cylinders)
  1111. cylinders = user_cylinders;
  1112. }
  1113. /*
  1114. * Read MBR. Returns:
  1115. * -1: no 0xaa55 flag present (possibly entire disk BSD)
  1116. * 0: found or created label
  1117. * 1: I/O error
  1118. */
  1119. static int
  1120. get_boot(enum action what)
  1121. {
  1122. int i;
  1123. partitions = 4;
  1124. for (i = 0; i < 4; i++) {
  1125. struct pte *pe = &ptes[i];
  1126. pe->part_table = pt_offset(MBRbuffer, i);
  1127. pe->ext_pointer = NULL;
  1128. pe->offset = 0;
  1129. pe->sectorbuffer = MBRbuffer;
  1130. #if ENABLE_FEATURE_FDISK_WRITABLE
  1131. pe->changed = (what == create_empty_dos);
  1132. #endif
  1133. }
  1134. #if ENABLE_FEATURE_SUN_LABEL
  1135. if (what == create_empty_sun && check_sun_label())
  1136. return 0;
  1137. #endif
  1138. memset(MBRbuffer, 0, 512);
  1139. #if ENABLE_FEATURE_FDISK_WRITABLE
  1140. if (what == create_empty_dos)
  1141. goto got_dos_table; /* skip reading disk */
  1142. if ((fd = open(disk_device, type_open)) < 0) {
  1143. if ((fd = open(disk_device, O_RDONLY)) < 0) {
  1144. if (what == try_only)
  1145. return 1;
  1146. fdisk_fatal(unable_to_open);
  1147. } else
  1148. printf(_("You will not be able to write "
  1149. "the partition table.\n"));
  1150. }
  1151. if (512 != read(fd, MBRbuffer, 512)) {
  1152. if (what == try_only)
  1153. return 1;
  1154. fdisk_fatal(unable_to_read);
  1155. }
  1156. #else
  1157. if ((fd = open(disk_device, O_RDONLY)) < 0)
  1158. return 1;
  1159. if (512 != read(fd, MBRbuffer, 512))
  1160. return 1;
  1161. #endif
  1162. get_geometry();
  1163. update_units();
  1164. #if ENABLE_FEATURE_SUN_LABEL
  1165. if (check_sun_label())
  1166. return 0;
  1167. #endif
  1168. #if ENABLE_FEATURE_SGI_LABEL
  1169. if (check_sgi_label())
  1170. return 0;
  1171. #endif
  1172. #if ENABLE_FEATURE_AIX_LABEL
  1173. if (check_aix_label())
  1174. return 0;
  1175. #endif
  1176. #if ENABLE_FEATURE_OSF_LABEL
  1177. if (check_osf_label()) {
  1178. possibly_osf_label = 1;
  1179. if (!valid_part_table_flag(MBRbuffer)) {
  1180. current_label_type = label_osf;
  1181. return 0;
  1182. }
  1183. printf(_("This disk has both DOS and BSD magic.\n"
  1184. "Give the 'b' command to go to BSD mode.\n"));
  1185. }
  1186. #endif
  1187. #if ENABLE_FEATURE_FDISK_WRITABLE
  1188. got_dos_table:
  1189. #endif
  1190. if (!valid_part_table_flag(MBRbuffer)) {
  1191. #if !ENABLE_FEATURE_FDISK_WRITABLE
  1192. return -1;
  1193. #else
  1194. switch (what) {
  1195. case fdisk:
  1196. printf(_("Device contains neither a valid DOS "
  1197. "partition table, nor Sun, SGI or OSF "
  1198. "disklabel\n"));
  1199. #ifdef __sparc__
  1200. #if ENABLE_FEATURE_SUN_LABEL
  1201. create_sunlabel();
  1202. #endif
  1203. #else
  1204. create_doslabel();
  1205. #endif
  1206. return 0;
  1207. case try_only:
  1208. return -1;
  1209. case create_empty_dos:
  1210. #if ENABLE_FEATURE_SUN_LABEL
  1211. case create_empty_sun:
  1212. #endif
  1213. break;
  1214. default:
  1215. bb_error_msg_and_die(_("internal error"));
  1216. }
  1217. #endif /* FEATURE_FDISK_WRITABLE */
  1218. }
  1219. #if ENABLE_FEATURE_FDISK_WRITABLE
  1220. warn_cylinders();
  1221. #endif
  1222. warn_geometry();
  1223. for (i = 0; i < 4; i++) {
  1224. struct pte *pe = &ptes[i];
  1225. if (IS_EXTENDED(pe->part_table->sys_ind)) {
  1226. if (partitions != 4)
  1227. printf(_("Ignoring extra extended "
  1228. "partition %d\n"), i + 1);
  1229. else
  1230. read_extended(i);
  1231. }
  1232. }
  1233. for (i = 3; i < partitions; i++) {
  1234. struct pte *pe = &ptes[i];
  1235. if (!valid_part_table_flag(pe->sectorbuffer)) {
  1236. printf(_("Warning: invalid flag 0x%02x,0x%02x of partition "
  1237. "table %d will be corrected by w(rite)\n"),
  1238. pe->sectorbuffer[510],
  1239. pe->sectorbuffer[511],
  1240. i + 1);
  1241. #if ENABLE_FEATURE_FDISK_WRITABLE
  1242. pe->changed = 1;
  1243. #endif
  1244. }
  1245. }
  1246. return 0;
  1247. }
  1248. #if ENABLE_FEATURE_FDISK_WRITABLE
  1249. /*
  1250. * Print the message MESG, then read an integer between LOW and HIGH (inclusive).
  1251. * If the user hits Enter, DFLT is returned.
  1252. * Answers like +10 are interpreted as offsets from BASE.
  1253. *
  1254. * There is no default if DFLT is not between LOW and HIGH.
  1255. */
  1256. static unsigned
  1257. read_int(unsigned low, unsigned dflt, unsigned high, unsigned base, char *mesg)
  1258. {
  1259. unsigned i;
  1260. int default_ok = 1;
  1261. const char *fmt = "%s (%u-%u, default %u): ";
  1262. if (dflt < low || dflt > high) {
  1263. fmt = "%s (%u-%u): ";
  1264. default_ok = 0;
  1265. }
  1266. while (1) {
  1267. int use_default = default_ok;
  1268. /* ask question and read answer */
  1269. do {
  1270. printf(fmt, mesg, low, high, dflt);
  1271. read_maybe_empty("");
  1272. } while (*line_ptr != '\n' && !isdigit(*line_ptr)
  1273. && *line_ptr != '-' && *line_ptr != '+');
  1274. if (*line_ptr == '+' || *line_ptr == '-') {
  1275. int minus = (*line_ptr == '-');
  1276. int absolute = 0;
  1277. i = atoi(line_ptr + 1);
  1278. while (isdigit(*++line_ptr))
  1279. use_default = 0;
  1280. switch (*line_ptr) {
  1281. case 'c':
  1282. case 'C':
  1283. if (!display_in_cyl_units)
  1284. i *= heads * sectors;
  1285. break;
  1286. case 'K':
  1287. absolute = 1024;
  1288. break;
  1289. case 'k':
  1290. absolute = 1000;
  1291. break;
  1292. case 'm':
  1293. case 'M':
  1294. absolute = 1000000;
  1295. break;
  1296. case 'g':
  1297. case 'G':
  1298. absolute = 1000000000;
  1299. break;
  1300. default:
  1301. break;
  1302. }
  1303. if (absolute) {
  1304. unsigned long long bytes;
  1305. unsigned long unit;
  1306. bytes = (unsigned long long) i * absolute;
  1307. unit = sector_size * units_per_sector;
  1308. bytes += unit/2; /* round */
  1309. bytes /= unit;
  1310. i = bytes;
  1311. }
  1312. if (minus)
  1313. i = -i;
  1314. i += base;
  1315. } else {
  1316. i = atoi(line_ptr);
  1317. while (isdigit(*line_ptr)) {
  1318. line_ptr++;
  1319. use_default = 0;
  1320. }
  1321. }
  1322. if (use_default)
  1323. printf(_("Using default value %u\n"), i = dflt);
  1324. if (i >= low && i <= high)
  1325. break;
  1326. else
  1327. printf(_("Value is out of range\n"));
  1328. }
  1329. return i;
  1330. }
  1331. static int
  1332. get_partition(int warn, int max)
  1333. {
  1334. struct pte *pe;
  1335. int i;
  1336. i = read_int(1, 0, max, 0, _("Partition number")) - 1;
  1337. pe = &ptes[i];
  1338. if (warn) {
  1339. if ((!LABEL_IS_SUN && !LABEL_IS_SGI && !pe->part_table->sys_ind)
  1340. || (LABEL_IS_SUN && (!sunlabel->partitions[i].num_sectors || !sunlabel->infos[i].id))
  1341. || (LABEL_IS_SGI && !sgi_get_num_sectors(i))
  1342. ) {
  1343. printf(_("Warning: partition %d has empty type\n"), i+1);
  1344. }
  1345. }
  1346. return i;
  1347. }
  1348. static int
  1349. get_existing_partition(int warn, int max)
  1350. {
  1351. int pno = -1;
  1352. int i;
  1353. for (i = 0; i < max; i++) {
  1354. struct pte *pe = &ptes[i];
  1355. struct partition *p = pe->part_table;
  1356. if (p && !is_cleared_partition(p)) {
  1357. if (pno >= 0)
  1358. goto not_unique;
  1359. pno = i;
  1360. }
  1361. }
  1362. if (pno >= 0) {
  1363. printf(_("Selected partition %d\n"), pno+1);
  1364. return pno;
  1365. }
  1366. printf(_("No partition is defined yet!\n"));
  1367. return -1;
  1368. not_unique:
  1369. return get_partition(warn, max);
  1370. }
  1371. static int
  1372. get_nonexisting_partition(int warn, int max)
  1373. {
  1374. int pno = -1;
  1375. int i;
  1376. for (i = 0; i < max; i++) {
  1377. struct pte *pe = &ptes[i];
  1378. struct partition *p = pe->part_table;
  1379. if (p && is_cleared_partition(p)) {
  1380. if (pno >= 0)
  1381. goto not_unique;
  1382. pno = i;
  1383. }
  1384. }
  1385. if (pno >= 0) {
  1386. printf(_("Selected partition %d\n"), pno+1);
  1387. return pno;
  1388. }
  1389. printf(_("All primary partitions have been defined already!\n"));
  1390. return -1;
  1391. not_unique:
  1392. return get_partition(warn, max);
  1393. }
  1394. static void
  1395. change_units(void)
  1396. {
  1397. display_in_cyl_units = !display_in_cyl_units;
  1398. update_units();
  1399. printf(_("Changing display/entry units to %s\n"),
  1400. str_units(PLURAL));
  1401. }
  1402. static void
  1403. toggle_active(int i)
  1404. {
  1405. struct pte *pe = &ptes[i];
  1406. struct partition *p = pe->part_table;
  1407. if (IS_EXTENDED(p->sys_ind) && !p->boot_ind)
  1408. printf(_("WARNING: Partition %d is an extended partition\n"), i + 1);
  1409. p->boot_ind = (p->boot_ind ? 0 : ACTIVE_FLAG);
  1410. pe->changed = 1;
  1411. }
  1412. static void
  1413. toggle_dos_compatibility_flag(void)
  1414. {
  1415. dos_compatible_flag = ~dos_compatible_flag;
  1416. if (dos_compatible_flag) {
  1417. sector_offset = sectors;
  1418. printf(_("DOS Compatibility flag is set\n"));
  1419. }
  1420. else {
  1421. sector_offset = 1;
  1422. printf(_("DOS Compatibility flag is not set\n"));
  1423. }
  1424. }
  1425. static void
  1426. delete_partition(int i)
  1427. {
  1428. struct pte *pe = &ptes[i];
  1429. struct partition *p = pe->part_table;
  1430. struct partition *q = pe->ext_pointer;
  1431. /* Note that for the fifth partition (i == 4) we don't actually
  1432. * decrement partitions.
  1433. */
  1434. if (warn_geometry())
  1435. return; /* C/H/S not set */
  1436. pe->changed = 1;
  1437. if (LABEL_IS_SUN) {
  1438. sun_delete_partition(i);
  1439. return;
  1440. }
  1441. if (LABEL_IS_SGI) {
  1442. sgi_delete_partition(i);
  1443. return;
  1444. }
  1445. if (i < 4) {
  1446. if (IS_EXTENDED(p->sys_ind) && i == ext_index) {
  1447. partitions = 4;
  1448. ptes[ext_index].ext_pointer = NULL;
  1449. extended_offset = 0;
  1450. }
  1451. clear_partition(p);
  1452. return;
  1453. }
  1454. if (!q->sys_ind && i > 4) {
  1455. /* the last one in the chain - just delete */
  1456. --partitions;
  1457. --i;
  1458. clear_partition(ptes[i].ext_pointer);
  1459. ptes[i].changed = 1;
  1460. } else {
  1461. /* not the last one - further ones will be moved down */
  1462. if (i > 4) {
  1463. /* delete this link in the chain */
  1464. p = ptes[i-1].ext_pointer;
  1465. *p = *q;
  1466. set_start_sect(p, get_start_sect(q));
  1467. set_nr_sects(p, get_nr_sects(q));
  1468. ptes[i-1].changed = 1;
  1469. } else if (partitions > 5) { /* 5 will be moved to 4 */
  1470. /* the first logical in a longer chain */
  1471. pe = &ptes[5];
  1472. if (pe->part_table) /* prevent SEGFAULT */
  1473. set_start_sect(pe->part_table,
  1474. get_partition_start(pe) -
  1475. extended_offset);
  1476. pe->offset = extended_offset;
  1477. pe->changed = 1;
  1478. }
  1479. if (partitions > 5) {
  1480. partitions--;
  1481. while (i < partitions) {
  1482. ptes[i] = ptes[i+1];
  1483. i++;
  1484. }
  1485. } else
  1486. /* the only logical: clear only */
  1487. clear_partition(ptes[i].part_table);
  1488. }
  1489. }
  1490. static void
  1491. change_sysid(void)
  1492. {
  1493. int i, sys, origsys;
  1494. struct partition *p;
  1495. /* If sgi_label then don't use get_existing_partition,
  1496. let the user select a partition, since get_existing_partition()
  1497. only works for Linux like partition tables. */
  1498. if (!LABEL_IS_SGI) {
  1499. i = get_existing_partition(0, partitions);
  1500. } else {
  1501. i = get_partition(0, partitions);
  1502. }
  1503. if (i == -1)
  1504. return;
  1505. p = ptes[i].part_table;
  1506. origsys = sys = get_sysid(i);
  1507. /* if changing types T to 0 is allowed, then
  1508. the reverse change must be allowed, too */
  1509. if (!sys && !LABEL_IS_SGI && !LABEL_IS_SUN && !get_nr_sects(p)) {
  1510. printf(_("Partition %d does not exist yet!\n"), i + 1);
  1511. return;
  1512. }
  1513. while (1) {
  1514. sys = read_hex (get_sys_types());
  1515. if (!sys && !LABEL_IS_SGI && !LABEL_IS_SUN) {
  1516. printf(_("Type 0 means free space to many systems\n"
  1517. "(but not to Linux). Having partitions of\n"
  1518. "type 0 is probably unwise. You can delete\n"
  1519. "a partition using the 'd' command.\n"));
  1520. /* break; */
  1521. }
  1522. if (!LABEL_IS_SUN && !LABEL_IS_SGI) {
  1523. if (IS_EXTENDED(sys) != IS_EXTENDED(p->sys_ind)) {
  1524. printf(_("You cannot change a partition into"
  1525. " an extended one or vice versa\n"
  1526. "Delete it first.\n"));
  1527. break;
  1528. }
  1529. }
  1530. if (sys < 256) {
  1531. if (LABEL_IS_SUN && i == 2 && sys != SUN_WHOLE_DISK)
  1532. printf(_("Consider leaving partition 3 "
  1533. "as Whole disk (5),\n"
  1534. "as SunOS/Solaris expects it and "
  1535. "even Linux likes it.\n\n"));
  1536. if (LABEL_IS_SGI &&
  1537. (
  1538. (i == 10 && sys != SGI_ENTIRE_DISK) ||
  1539. (i == 8 && sys != 0)
  1540. )
  1541. ){
  1542. printf(_("Consider leaving partition 9 "
  1543. "as volume header (0),\nand "
  1544. "partition 11 as entire volume (6)"
  1545. "as IRIX expects it.\n\n"));
  1546. }
  1547. if (sys == origsys)
  1548. break;
  1549. if (LABEL_IS_SUN) {
  1550. sun_change_sysid(i, sys);
  1551. } else if (LABEL_IS_SGI) {
  1552. sgi_change_sysid(i, sys);
  1553. } else
  1554. p->sys_ind = sys;
  1555. printf(_("Changed system type of partition %d "
  1556. "to %x (%s)\n"), i + 1, sys,
  1557. partition_type(sys));
  1558. ptes[i].changed = 1;
  1559. if (is_dos_partition(origsys) ||
  1560. is_dos_partition(sys))
  1561. dos_changed = 1;
  1562. break;
  1563. }
  1564. }
  1565. }
  1566. #endif /* FEATURE_FDISK_WRITABLE */
  1567. /* check_consistency() and linear2chs() added Sat Mar 6 12:28:16 1993,
  1568. * faith@cs.unc.edu, based on code fragments from pfdisk by Gordon W. Ross,
  1569. * Jan. 1990 (version 1.2.1 by Gordon W. Ross Aug. 1990; Modified by S.
  1570. * Lubkin Oct. 1991). */
  1571. static void
  1572. linear2chs(unsigned ls, unsigned *c, unsigned *h, unsigned *s)
  1573. {
  1574. int spc = heads * sectors;
  1575. *c = ls / spc;
  1576. ls = ls % spc;
  1577. *h = ls / sectors;
  1578. *s = ls % sectors + 1; /* sectors count from 1 */
  1579. }
  1580. static void
  1581. check_consistency(const struct partition *p, int partition)
  1582. {
  1583. unsigned pbc, pbh, pbs; /* physical beginning c, h, s */
  1584. unsigned pec, peh, pes; /* physical ending c, h, s */
  1585. unsigned lbc, lbh, lbs; /* logical beginning c, h, s */
  1586. unsigned lec, leh, les; /* logical ending c, h, s */
  1587. if (!heads || !sectors || (partition >= 4))
  1588. return; /* do not check extended partitions */
  1589. /* physical beginning c, h, s */
  1590. pbc = (p->cyl & 0xff) | ((p->sector << 2) & 0x300);
  1591. pbh = p->head;
  1592. pbs = p->sector & 0x3f;
  1593. /* physical ending c, h, s */
  1594. pec = (p->end_cyl & 0xff) | ((p->end_sector << 2) & 0x300);
  1595. peh = p->end_head;
  1596. pes = p->end_sector & 0x3f;
  1597. /* compute logical beginning (c, h, s) */
  1598. linear2chs(get_start_sect(p), &lbc, &lbh, &lbs);
  1599. /* compute logical ending (c, h, s) */
  1600. linear2chs(get_start_sect(p) + get_nr_sects(p) - 1, &lec, &leh, &les);
  1601. /* Same physical / logical beginning? */
  1602. if (cylinders <= 1024 && (pbc != lbc || pbh != lbh || pbs != lbs)) {
  1603. printf(_("Partition %d has different physical/logical "
  1604. "beginnings (non-Linux?):\n"), partition + 1);
  1605. printf(_(" phys=(%d, %d, %d) "), pbc, pbh, pbs);
  1606. printf(_("logical=(%d, %d, %d)\n"),lbc, lbh, lbs);
  1607. }
  1608. /* Same physical / logical ending? */
  1609. if (cylinders <= 1024 && (pec != lec || peh != leh || pes != les)) {
  1610. printf(_("Partition %d has different physical/logical "
  1611. "endings:\n"), partition + 1);
  1612. printf(_(" phys=(%d, %d, %d) "), pec, peh, pes);
  1613. printf(_("logical=(%d, %d, %d)\n"),lec, leh, les);
  1614. }
  1615. /* Ending on cylinder boundary? */
  1616. if (peh != (heads - 1) || pes != sectors) {
  1617. printf(_("Partition %i does not end on cylinder boundary.\n"),
  1618. partition + 1);
  1619. }
  1620. }
  1621. static void
  1622. list_disk_geometry(void)
  1623. {
  1624. long long bytes = (total_number_of_sectors << 9);
  1625. long megabytes = bytes/1000000;
  1626. if (megabytes < 10000)
  1627. printf(_("\nDisk %s: %ld MB, %lld bytes\n"),
  1628. disk_device, megabytes, bytes);
  1629. else
  1630. printf(_("\nDisk %s: %ld.%ld GB, %lld bytes\n"),
  1631. disk_device, megabytes/1000, (megabytes/100)%10, bytes);
  1632. printf(_("%d heads, %d sectors/track, %d cylinders"),
  1633. heads, sectors, cylinders);
  1634. if (units_per_sector == 1)
  1635. printf(_(", total %llu sectors"),
  1636. total_number_of_sectors / (sector_size/512));
  1637. printf(_("\nUnits = %s of %d * %d = %d bytes\n\n"),
  1638. str_units(PLURAL),
  1639. units_per_sector, sector_size, units_per_sector * sector_size);
  1640. }
  1641. /*
  1642. * Check whether partition entries are ordered by their starting positions.
  1643. * Return 0 if OK. Return i if partition i should have been earlier.
  1644. * Two separate checks: primary and logical partitions.
  1645. */
  1646. static int
  1647. wrong_p_order(int *prev)
  1648. {
  1649. const struct pte *pe;
  1650. const struct partition *p;
  1651. off_t last_p_start_pos = 0, p_start_pos;
  1652. int i, last_i = 0;
  1653. for (i = 0 ; i < partitions; i++) {
  1654. if (i == 4) {
  1655. last_i = 4;
  1656. last_p_start_pos = 0;
  1657. }
  1658. pe = &ptes[i];
  1659. if ((p = pe->part_table)->sys_ind) {
  1660. p_start_pos = get_partition_start(pe);
  1661. if (last_p_start_pos > p_start_pos) {
  1662. if (prev)
  1663. *prev = last_i;
  1664. return i;
  1665. }
  1666. last_p_start_pos = p_start_pos;
  1667. last_i = i;
  1668. }
  1669. }
  1670. return 0;
  1671. }
  1672. #if ENABLE_FEATURE_FDISK_ADVANCED
  1673. /*
  1674. * Fix the chain of logicals.
  1675. * extended_offset is unchanged, the set of sectors used is unchanged
  1676. * The chain is sorted so that sectors increase, and so that
  1677. * starting sectors increase.
  1678. *
  1679. * After this it may still be that cfdisk doesnt like the table.
  1680. * (This is because cfdisk considers expanded parts, from link to
  1681. * end of partition, and these may still overlap.)
  1682. * Now
  1683. * sfdisk /dev/hda > ohda; sfdisk /dev/hda < ohda
  1684. * may help.
  1685. */
  1686. static void
  1687. fix_chain_of_logicals(void)
  1688. {
  1689. int j, oj, ojj, sj, sjj;
  1690. struct partition *pj,*pjj,tmp;
  1691. /* Stage 1: sort sectors but leave sector of part 4 */
  1692. /* (Its sector is the global extended_offset.) */
  1693. stage1:
  1694. for (j = 5; j < partitions-1; j++) {
  1695. oj = ptes[j].offset;
  1696. ojj = ptes[j+1].offset;
  1697. if (oj > ojj) {
  1698. ptes[j].offset = ojj;
  1699. ptes[j+1].offset = oj;
  1700. pj = ptes[j].part_table;
  1701. set_start_sect(pj, get_start_sect(pj)+oj-ojj);
  1702. pjj = ptes[j+1].part_table;
  1703. set_start_sect(pjj, get_start_sect(pjj)+ojj-oj);
  1704. set_start_sect(ptes[j-1].ext_pointer,
  1705. ojj-extended_offset);
  1706. set_start_sect(ptes[j].ext_pointer,
  1707. oj-extended_offset);
  1708. goto stage1;
  1709. }
  1710. }
  1711. /* Stage 2: sort starting sectors */
  1712. stage2:
  1713. for (j = 4; j < partitions-1; j++) {
  1714. pj = ptes[j].part_table;
  1715. pjj = ptes[j+1].part_table;
  1716. sj = get_start_sect(pj);
  1717. sjj = get_start_sect(pjj);
  1718. oj = ptes[j].offset;
  1719. ojj = ptes[j+1].offset;
  1720. if (oj+sj > ojj+sjj) {
  1721. tmp = *pj;
  1722. *pj = *pjj;
  1723. *pjj = tmp;
  1724. set_start_sect(pj, ojj+sjj-oj);
  1725. set_start_sect(pjj, oj+sj-ojj);
  1726. goto stage2;
  1727. }
  1728. }
  1729. /* Probably something was changed */
  1730. for (j = 4; j < partitions; j++)
  1731. ptes[j].changed = 1;
  1732. }
  1733. static void
  1734. fix_partition_table_order(void)
  1735. {
  1736. struct pte *pei, *pek;
  1737. int i,k;
  1738. if (!wrong_p_order(NULL)) {
  1739. printf(_("Nothing to do. Ordering is correct already.\n\n"));
  1740. return;
  1741. }
  1742. while ((i = wrong_p_order(&k)) != 0 && i < 4) {
  1743. /* partition i should have come earlier, move it */
  1744. /* We have to move data in the MBR */
  1745. struct partition *pi, *pk, *pe, pbuf;
  1746. pei = &ptes[i];
  1747. pek = &ptes[k];
  1748. pe = pei->ext_pointer;
  1749. pei->ext_pointer = pek->ext_pointer;
  1750. pek->ext_pointer = pe;
  1751. pi = pei->part_table;
  1752. pk = pek->part_table;
  1753. memmove(&pbuf, pi, sizeof(struct partition));
  1754. memmove(pi, pk, sizeof(struct partition));
  1755. memmove(pk, &pbuf, sizeof(struct partition));
  1756. pei->changed = pek->changed = 1;
  1757. }
  1758. if (i)
  1759. fix_chain_of_logicals();
  1760. printf("Done.\n");
  1761. }
  1762. #endif
  1763. static void
  1764. list_table(int xtra)
  1765. {
  1766. const struct partition *p;
  1767. int i, w;
  1768. if (LABEL_IS_SUN) {
  1769. sun_list_table(xtra);
  1770. return;
  1771. }
  1772. if (LABEL_IS_SUN) {
  1773. sgi_list_table(xtra);
  1774. return;
  1775. }
  1776. list_disk_geometry();
  1777. if (LABEL_IS_OSF) {
  1778. xbsd_print_disklabel(xtra);
  1779. return;
  1780. }
  1781. /* Heuristic: we list partition 3 of /dev/foo as /dev/foo3,
  1782. but if the device name ends in a digit, say /dev/foo1,
  1783. then the partition is called /dev/foo1p3. */
  1784. w = strlen(disk_device);
  1785. if (w && isdigit(disk_device[w-1]))
  1786. w++;
  1787. if (w < 5)
  1788. w = 5;
  1789. // 1 12345678901 12345678901 12345678901 12
  1790. printf(_("%*s Boot Start End Blocks Id System\n"),
  1791. w+1, _("Device"));
  1792. for (i = 0; i < partitions; i++) {
  1793. const struct pte *pe = &ptes[i];
  1794. off_t psects;
  1795. off_t pblocks;
  1796. unsigned podd;
  1797. p = pe->part_table;
  1798. if (!p || is_cleared_partition(p))
  1799. continue;
  1800. psects = get_nr_sects(p);
  1801. pblocks = psects;
  1802. podd = 0;
  1803. if (sector_size < 1024) {
  1804. pblocks /= (1024 / sector_size);
  1805. podd = psects % (1024 / sector_size);
  1806. }
  1807. if (sector_size > 1024)
  1808. pblocks *= (sector_size / 1024);
  1809. printf("%s %c %11llu %11llu %11llu%c %2x %s\n",
  1810. partname(disk_device, i+1, w+2),
  1811. !p->boot_ind ? ' ' : p->boot_ind == ACTIVE_FLAG /* boot flag */
  1812. ? '*' : '?',
  1813. (unsigned long long) cround(get_partition_start(pe)), /* start */
  1814. (unsigned long long) cround(get_partition_start(pe) + psects /* end */
  1815. - (psects ? 1 : 0)),
  1816. (unsigned long long) pblocks, podd ? '+' : ' ', /* odd flag on end */
  1817. p->sys_ind, /* type id */
  1818. partition_type(p->sys_ind)); /* type name */
  1819. check_consistency(p, i);
  1820. }
  1821. /* Is partition table in disk order? It need not be, but... */
  1822. /* partition table entries are not checked for correct order if this
  1823. is a sgi, sun or aix labeled disk... */
  1824. if (LABEL_IS_DOS && wrong_p_order(NULL)) {
  1825. /* FIXME */
  1826. printf(_("\nPartition table entries are not in disk order\n"));
  1827. }
  1828. }
  1829. #if ENABLE_FEATURE_FDISK_ADVANCED
  1830. static void
  1831. x_list_table(int extend)
  1832. {
  1833. const struct pte *pe;
  1834. const struct partition *p;
  1835. int i;
  1836. printf(_("\nDisk %s: %d heads, %d sectors, %d cylinders\n\n"),
  1837. disk_device, heads, sectors, cylinders);
  1838. printf(_("Nr AF Hd Sec Cyl Hd Sec Cyl Start Size ID\n"));
  1839. for (i = 0 ; i < partitions; i++) {
  1840. pe = &ptes[i];
  1841. p = (extend ? pe->ext_pointer : pe->part_table);
  1842. if (p != NULL) {
  1843. printf("%2d %02x%4d%4d%5d%4d%4d%5d%11u%11u %02x\n",
  1844. i + 1, p->boot_ind, p->head,
  1845. sector(p->sector),
  1846. cylinder(p->sector, p->cyl), p->end_head,
  1847. sector(p->end_sector),
  1848. cylinder(p->end_sector, p->end_cyl),
  1849. get_start_sect(p), get_nr_sects(p), p->sys_ind);
  1850. if (p->sys_ind)
  1851. check_consistency(p, i);
  1852. }
  1853. }
  1854. }
  1855. #endif
  1856. #if ENABLE_FEATURE_FDISK_WRITABLE
  1857. static void
  1858. fill_bounds(off_t *first, off_t *last)
  1859. {
  1860. int i;
  1861. const struct pte *pe = &ptes[0];
  1862. const struct partition *p;
  1863. for (i = 0; i < partitions; pe++,i++) {
  1864. p = pe->part_table;
  1865. if (!p->sys_ind || IS_EXTENDED(p->sys_ind)) {
  1866. first[i] = 0xffffffff;
  1867. last[i] = 0;
  1868. } else {
  1869. first[i] = get_partition_start(pe);
  1870. last[i] = first[i] + get_nr_sects(p) - 1;
  1871. }
  1872. }
  1873. }
  1874. static void
  1875. check(int n, unsigned h, unsigned s, unsigned c, off_t start)
  1876. {
  1877. off_t total, real_s, real_c;
  1878. real_s = sector(s) - 1;
  1879. real_c = cylinder(s, c);
  1880. total = (real_c * sectors + real_s) * heads + h;
  1881. if (!total)
  1882. printf(_("Warning: partition %d contains sector 0\n"), n);
  1883. if (h >= heads)
  1884. printf(_("Partition %d: head %d greater than maximum %d\n"),
  1885. n, h + 1, heads);
  1886. if (real_s >= sectors)
  1887. printf(_("Partition %d: sector %d greater than "
  1888. "maximum %d\n"), n, s, sectors);
  1889. if (real_c >= cylinders)
  1890. printf(_("Partitions %d: cylinder %llu greater than "
  1891. "maximum %d\n"), n, (unsigned long long)real_c + 1, cylinders);
  1892. if (cylinders <= 1024 && start != total)
  1893. printf(_("Partition %d: previous sectors %llu disagrees with "
  1894. "total %llu\n"), n, (unsigned long long)start, (unsigned long long)total);
  1895. }
  1896. static void
  1897. verify(void)
  1898. {
  1899. int i, j;
  1900. unsigned total = 1;
  1901. off_t first[partitions], last[partitions];
  1902. struct partition *p;
  1903. if (warn_geometry())
  1904. return;
  1905. if (LABEL_IS_SUN) {
  1906. verify_sun();
  1907. return;
  1908. }
  1909. if (LABEL_IS_SGI) {
  1910. verify_sgi(1);
  1911. return;
  1912. }
  1913. fill_bounds(first, last);
  1914. for (i = 0; i < partitions; i++) {
  1915. struct pte *pe = &ptes[i];
  1916. p = pe->part_table;
  1917. if (p->sys_ind && !IS_EXTENDED(p->sys_ind)) {
  1918. check_consistency(p, i);
  1919. if (get_partition_start(pe) < first[i])
  1920. printf(_("Warning: bad start-of-data in "
  1921. "partition %d\n"), i + 1);
  1922. check(i + 1, p->end_head, p->end_sector, p->end_cyl,
  1923. last[i]);
  1924. total += last[i] + 1 - first[i];
  1925. for (j = 0; j < i; j++)
  1926. if ((first[i] >= first[j] && first[i] <= last[j])
  1927. || ((last[i] <= last[j] && last[i] >= first[j]))) {
  1928. printf(_("Warning: partition %d overlaps "
  1929. "partition %d.\n"), j + 1, i + 1);
  1930. total += first[i] >= first[j] ?
  1931. first[i] : first[j];
  1932. total -= last[i] <= last[j] ?
  1933. last[i] : last[j];
  1934. }
  1935. }
  1936. }
  1937. if (extended_offset) {
  1938. struct pte *pex = &ptes[ext_index];
  1939. off_t e_last = get_start_sect(pex->part_table) +
  1940. get_nr_sects(pex->part_table) - 1;
  1941. for (i = 4; i < partitions; i++) {
  1942. total++;
  1943. p = ptes[i].part_table;
  1944. if (!p->sys_ind) {
  1945. if (i != 4 || i + 1 < partitions)
  1946. printf(_("Warning: partition %d "
  1947. "is empty\n"), i + 1);
  1948. }
  1949. else if (first[i] < extended_offset ||
  1950. last[i] > e_last)
  1951. printf(_("Logical partition %d not entirely in "
  1952. "partition %d\n"), i + 1, ext_index + 1);
  1953. }
  1954. }
  1955. if (total > heads * sectors * cylinders)
  1956. printf(_("Total allocated sectors %d greater than the maximum "
  1957. "%d\n"), total, heads * sectors * cylinders);
  1958. else if ((total = heads * sectors * cylinders - total) != 0)
  1959. printf(_("%d unallocated sectors\n"), total);
  1960. }
  1961. static void
  1962. add_partition(int n, int sys)
  1963. {
  1964. char mesg[256]; /* 48 does not suffice in Japanese */
  1965. int i, num_read = 0;
  1966. struct partition *p = ptes[n].part_table;
  1967. struct partition *q = ptes[ext_index].part_table;
  1968. long long llimit;
  1969. off_t start, stop = 0, limit, temp,
  1970. first[partitions], last[partitions];
  1971. if (p && p->sys_ind) {
  1972. printf(_("Partition %d is already defined. Delete "
  1973. "it before re-adding it.\n"), n + 1);
  1974. return;
  1975. }
  1976. fill_bounds(first, last);
  1977. if (n < 4) {
  1978. start = sector_offset;
  1979. if (display_in_cyl_units || !total_number_of_sectors)
  1980. llimit = heads * sectors * cylinders - 1;
  1981. else
  1982. llimit = total_number_of_sectors - 1;
  1983. limit = llimit;
  1984. if (limit != llimit)
  1985. limit = 0x7fffffff;
  1986. if (extended_offset) {
  1987. first[ext_index] = extended_offset;
  1988. last[ext_index] = get_start_sect(q) +
  1989. get_nr_sects(q) - 1;
  1990. }
  1991. } else {
  1992. start = extended_offset + sector_offset;
  1993. limit = get_start_sect(q) + get_nr_sects(q) - 1;
  1994. }
  1995. if (display_in_cyl_units)
  1996. for (i = 0; i < partitions; i++)
  1997. first[i] = (cround(first[i]) - 1) * units_per_sector;
  1998. snprintf(mesg, sizeof(mesg), _("First %s"), str_units(SINGULAR));
  1999. do {
  2000. temp = start;
  2001. for (i = 0; i < partitions; i++) {
  2002. int lastplusoff;
  2003. if (start == ptes[i].offset)
  2004. start += sector_offset;
  2005. lastplusoff = last[i] + ((n < 4) ? 0 : sector_offset);
  2006. if (start >= first[i] && start <= lastplusoff)
  2007. start = lastplusoff + 1;
  2008. }
  2009. if (start > limit)
  2010. break;
  2011. if (start >= temp+units_per_sector && num_read) {
  2012. printf(_("Sector %"OFF_FMT"d is already allocated\n"), temp);
  2013. temp = start;
  2014. num_read = 0;
  2015. }
  2016. if (!num_read && start == temp) {
  2017. off_t saved_start;
  2018. saved_start = start;
  2019. start = read_int(cround(saved_start), cround(saved_start), cround(limit),
  2020. 0, mesg);
  2021. if (display_in_cyl_units) {
  2022. start = (start - 1) * units_per_sector;
  2023. if (start < saved_start) start = saved_start;
  2024. }
  2025. num_read = 1;
  2026. }
  2027. } while (start != temp || !num_read);
  2028. if (n > 4) { /* NOT for fifth partition */
  2029. struct pte *pe = &ptes[n];
  2030. pe->offset = start - sector_offset;
  2031. if (pe->offset == extended_offset) { /* must be corrected */
  2032. pe->offset++;
  2033. if (sector_offset == 1)
  2034. start++;
  2035. }
  2036. }
  2037. for (i = 0; i < partitions; i++) {
  2038. struct pte *pe = &ptes[i];
  2039. if (start < pe->offset && limit >= pe->offset)
  2040. limit = pe->offset - 1;
  2041. if (start < first[i] && limit >= first[i])
  2042. limit = first[i] - 1;
  2043. }
  2044. if (start > limit) {
  2045. printf(_("No free sectors available\n"));
  2046. if (n > 4)
  2047. partitions--;
  2048. return;
  2049. }
  2050. if (cround(start) == cround(limit)) {
  2051. stop = limit;
  2052. } else {
  2053. snprintf(mesg, sizeof(mesg),
  2054. _("Last %s or +size or +sizeM or +sizeK"),
  2055. str_units(SINGULAR));
  2056. stop = read_int(cround(start), cround(limit), cround(limit),
  2057. cround(start), mesg);
  2058. if (display_in_cyl_units) {
  2059. stop = stop * units_per_sector - 1;
  2060. if (stop >limit)
  2061. stop = limit;
  2062. }
  2063. }
  2064. set_partition(n, 0, start, stop, sys);
  2065. if (n > 4)
  2066. set_partition(n - 1, 1, ptes[n].offset, stop, EXTENDED);
  2067. if (IS_EXTENDED(sys)) {
  2068. struct pte *pe4 = &ptes[4];
  2069. struct pte *pen = &ptes[n];
  2070. ext_index = n;
  2071. pen->ext_pointer = p;
  2072. pe4->offset = extended_offset = start;
  2073. pe4->sectorbuffer = xzalloc(sector_size);
  2074. pe4->part_table = pt_offset(pe4->sectorbuffer, 0);
  2075. pe4->ext_pointer = pe4->part_table + 1;
  2076. pe4->changed = 1;
  2077. partitions = 5;
  2078. }
  2079. }
  2080. static void
  2081. add_logical(void)
  2082. {
  2083. if (partitions > 5 || ptes[4].part_table->sys_ind) {
  2084. struct pte *pe = &ptes[partitions];
  2085. pe->sectorbuffer = xzalloc(sector_size);
  2086. pe->part_table = pt_offset(pe->sectorbuffer, 0);
  2087. pe->ext_pointer = pe->part_table + 1;
  2088. pe->offset = 0;
  2089. pe->changed = 1;
  2090. partitions++;
  2091. }
  2092. add_partition(partitions - 1, LINUX_NATIVE);
  2093. }
  2094. static void
  2095. new_partition(void)
  2096. {
  2097. int i, free_primary = 0;
  2098. if (warn_geometry())
  2099. return;
  2100. if (LABEL_IS_SUN) {
  2101. add_sun_partition(get_partition(0, partitions), LINUX_NATIVE);
  2102. return;
  2103. }
  2104. if (LABEL_IS_SGI) {
  2105. sgi_add_partition(get_partition(0, partitions), LINUX_NATIVE);
  2106. return;
  2107. }
  2108. if (LABEL_IS_AIX) {
  2109. printf(_("\tSorry - this fdisk cannot handle AIX disk labels."
  2110. "\n\tIf you want to add DOS-type partitions, create"
  2111. "\n\ta new empty DOS partition table first. (Use o.)"
  2112. "\n\tWARNING: "
  2113. "This will destroy the present disk contents.\n"));
  2114. return;
  2115. }
  2116. for (i = 0; i < 4; i++)
  2117. free_primary += !ptes[i].part_table->sys_ind;
  2118. if (!free_primary && partitions >= MAXIMUM_PARTS) {
  2119. printf(_("The maximum number of partitions has been created\n"));
  2120. return;
  2121. }
  2122. if (!free_primary) {
  2123. if (extended_offset)
  2124. add_logical();
  2125. else
  2126. printf(_("You must delete some partition and add "
  2127. "an extended partition first\n"));
  2128. } else {
  2129. char c, line[LINE_LENGTH];
  2130. snprintf(line, sizeof(line), "%s\n %s\n p primary "
  2131. "partition (1-4)\n",
  2132. "Command action", (extended_offset ?
  2133. "l logical (5 or over)" : "e extended"));
  2134. while (1) {
  2135. c = read_nonempty(line);
  2136. if (c == 'p' || c == 'P') {
  2137. i = get_nonexisting_partition(0, 4);
  2138. if (i >= 0)
  2139. add_partition(i, LINUX_NATIVE);
  2140. return;
  2141. }
  2142. else if (c == 'l' && extended_offset) {
  2143. add_logical();
  2144. return;
  2145. }
  2146. else if (c == 'e' && !extended_offset) {
  2147. i = get_nonexisting_partition(0, 4);
  2148. if (i >= 0)
  2149. add_partition(i, EXTENDED);
  2150. return;
  2151. }
  2152. else
  2153. printf(_("Invalid partition number "
  2154. "for type '%c'\n"), c);
  2155. }
  2156. }
  2157. }
  2158. static void
  2159. write_table(void)
  2160. {
  2161. int i;
  2162. if (LABEL_IS_DOS) {
  2163. for (i = 0; i < 3; i++)
  2164. if (ptes[i].changed)
  2165. ptes[3].changed = 1;
  2166. for (i = 3; i < partitions; i++) {
  2167. struct pte *pe = &ptes[i];
  2168. if (pe->changed) {
  2169. write_part_table_flag(pe->sectorbuffer);
  2170. write_sector(pe->offset, pe->sectorbuffer);
  2171. }
  2172. }
  2173. }
  2174. else if (LABEL_IS_SGI) {
  2175. /* no test on change? the printf below might be mistaken */
  2176. sgi_write_table();
  2177. }
  2178. else if (LABEL_IS_SUN) {
  2179. int needw = 0;
  2180. for (i = 0; i < 8; i++)
  2181. if (ptes[i].changed)
  2182. needw = 1;
  2183. if (needw)
  2184. sun_write_table();
  2185. }
  2186. printf(_("The partition table has been altered!\n\n"));
  2187. reread_partition_table(1);
  2188. }
  2189. static void
  2190. reread_partition_table(int leave)
  2191. {
  2192. int i;
  2193. printf(_("Calling ioctl() to re-read partition table\n"));
  2194. sync();
  2195. sleep(2); /* Huh? */
  2196. i = ioctl(fd, BLKRRPART);
  2197. #if 0
  2198. else {
  2199. /* some kernel versions (1.2.x) seem to have trouble
  2200. rereading the partition table, but if asked to do it
  2201. twice, the second time works. - biro@yggdrasil.com */
  2202. sync();
  2203. sleep(2);
  2204. i = ioctl(fd, BLKRRPART);
  2205. }
  2206. #endif
  2207. if (i) {
  2208. bb_perror_msg("WARNING: rereading partition table "
  2209. "failed, kernel still uses old table");
  2210. }
  2211. #if 0
  2212. if (dos_changed)
  2213. printf(
  2214. _("\nWARNING: If you have created or modified any DOS 6.x\n"
  2215. "partitions, please see the fdisk manual page for additional\n"
  2216. "information.\n"));
  2217. #endif
  2218. if (leave) {
  2219. if (ENABLE_FEATURE_CLEAN_UP)
  2220. close(fd);
  2221. exit(i != 0);
  2222. }
  2223. }
  2224. #endif /* FEATURE_FDISK_WRITABLE */
  2225. #if ENABLE_FEATURE_FDISK_ADVANCED
  2226. #define MAX_PER_LINE 16
  2227. static void
  2228. print_buffer(char *pbuffer)
  2229. {
  2230. int i,l;
  2231. for (i = 0, l = 0; i < sector_size; i++, l++) {
  2232. if (l == 0)
  2233. printf("0x%03X:", i);
  2234. printf(" %02X", (unsigned char) pbuffer[i]);
  2235. if (l == MAX_PER_LINE - 1) {
  2236. puts("");
  2237. l = -1;
  2238. }
  2239. }
  2240. if (l > 0)
  2241. puts("");
  2242. puts("");
  2243. }
  2244. static void
  2245. print_raw(void)
  2246. {
  2247. int i;
  2248. printf(_("Device: %s\n"), disk_device);
  2249. if (LABEL_IS_SGI || LABEL_IS_SUN)
  2250. print_buffer(MBRbuffer);
  2251. else {
  2252. for (i = 3; i < partitions; i++)
  2253. print_buffer(ptes[i].sectorbuffer);
  2254. }
  2255. }
  2256. static void
  2257. move_begin(int i)
  2258. {
  2259. struct pte *pe = &ptes[i];
  2260. struct partition *p = pe->part_table;
  2261. off_t new, first;
  2262. if (warn_geometry())
  2263. return;
  2264. if (!p->sys_ind || !get_nr_sects(p) || IS_EXTENDED(p->sys_ind)) {
  2265. printf(_("Partition %d has no data area\n"), i + 1);
  2266. return;
  2267. }
  2268. first = get_partition_start(pe);
  2269. new = read_int(first, first, first + get_nr_sects(p) - 1, first,
  2270. _("New beginning of data")) - pe->offset;
  2271. if (new != get_nr_sects(p)) {
  2272. first = get_nr_sects(p) + get_start_sect(p) - new;
  2273. set_nr_sects(p, first);
  2274. set_start_sect(p, new);
  2275. pe->changed = 1;
  2276. }
  2277. }
  2278. static void
  2279. xselect(void)
  2280. {
  2281. char c;
  2282. while (1) {
  2283. putchar('\n');
  2284. c = tolower(read_nonempty(_("Expert command (m for help): ")));
  2285. switch (c) {
  2286. case 'a':
  2287. if (LABEL_IS_SUN)
  2288. sun_set_alt_cyl();
  2289. break;
  2290. case 'b':
  2291. if (LABEL_IS_DOS)
  2292. move_begin(get_partition(0, partitions));
  2293. break;
  2294. case 'c':
  2295. user_cylinders = cylinders =
  2296. read_int(1, cylinders, 1048576, 0,
  2297. _("Number of cylinders"));
  2298. if (LABEL_IS_SUN)
  2299. sun_set_ncyl(cylinders);
  2300. if (LABEL_IS_DOS)
  2301. warn_cylinders();
  2302. break;
  2303. case 'd':
  2304. print_raw();
  2305. break;
  2306. case 'e':
  2307. if (LABEL_IS_SGI)
  2308. sgi_set_xcyl();
  2309. else if (LABEL_IS_SUN)
  2310. sun_set_xcyl();
  2311. else if (LABEL_IS_DOS)
  2312. x_list_table(1);
  2313. break;
  2314. case 'f':
  2315. if (LABEL_IS_DOS)
  2316. fix_partition_table_order();
  2317. break;
  2318. case 'g':
  2319. #if ENABLE_FEATURE_SGI_LABEL
  2320. create_sgilabel();
  2321. #endif
  2322. break;
  2323. case 'h':
  2324. user_heads = heads = read_int(1, heads, 256, 0,
  2325. _("Number of heads"));
  2326. update_units();
  2327. break;
  2328. case 'i':
  2329. if (LABEL_IS_SUN)
  2330. sun_set_ilfact();
  2331. break;
  2332. case 'o':
  2333. if (LABEL_IS_SUN)
  2334. sun_set_rspeed();
  2335. break;
  2336. case 'p':
  2337. if (LABEL_IS_SUN)
  2338. list_table(1);
  2339. else
  2340. x_list_table(0);
  2341. break;
  2342. case 'q':
  2343. close(fd);
  2344. puts("");
  2345. exit(0);
  2346. case 'r':
  2347. return;
  2348. case 's':
  2349. user_sectors = sectors = read_int(1, sectors, 63, 0,
  2350. _("Number of sectors"));
  2351. if (dos_compatible_flag) {
  2352. sector_offset = sectors;
  2353. printf(_("Warning: setting sector offset for DOS "
  2354. "compatiblity\n"));
  2355. }
  2356. update_units();
  2357. break;
  2358. case 'v':
  2359. verify();
  2360. break;
  2361. case 'w':
  2362. write_table(); /* does not return */
  2363. break;
  2364. case 'y':
  2365. if (LABEL_IS_SUN)
  2366. sun_set_pcylcount();
  2367. break;
  2368. default:
  2369. xmenu();
  2370. }
  2371. }
  2372. }
  2373. #endif /* ADVANCED mode */
  2374. static int
  2375. is_ide_cdrom_or_tape(const char *device)
  2376. {
  2377. FILE *procf;
  2378. char buf[100];
  2379. struct stat statbuf;
  2380. int is_ide = 0;
  2381. /* No device was given explicitly, and we are trying some
  2382. likely things. But opening /dev/hdc may produce errors like
  2383. "hdc: tray open or drive not ready"
  2384. if it happens to be a CD-ROM drive. It even happens that
  2385. the process hangs on the attempt to read a music CD.
  2386. So try to be careful. This only works since 2.1.73. */
  2387. if (strncmp("/dev/hd", device, 7))
  2388. return 0;
  2389. snprintf(buf, sizeof(buf), "/proc/ide/%s/media", device+5);
  2390. procf = fopen(buf, "r");
  2391. if (procf != NULL && fgets(buf, sizeof(buf), procf))
  2392. is_ide = (!strncmp(buf, "cdrom", 5) ||
  2393. !strncmp(buf, "tape", 4));
  2394. else
  2395. /* Now when this proc file does not exist, skip the
  2396. device when it is read-only. */
  2397. if (stat(device, &statbuf) == 0)
  2398. is_ide = ((statbuf.st_mode & 0222) == 0);
  2399. if (procf)
  2400. fclose(procf);
  2401. return is_ide;
  2402. }
  2403. static void
  2404. trydev(const char *device, int user_specified)
  2405. {
  2406. int gb;
  2407. disk_device = device;
  2408. if (setjmp(listingbuf))
  2409. return;
  2410. if (!user_specified)
  2411. if (is_ide_cdrom_or_tape(device))
  2412. return;
  2413. fd = open(disk_device, type_open);
  2414. if (fd >= 0) {
  2415. gb = get_boot(try_only);
  2416. if (gb > 0) { /* I/O error */
  2417. close(fd);
  2418. } else if (gb < 0) { /* no DOS signature */
  2419. list_disk_geometry();
  2420. if (LABEL_IS_AIX) {
  2421. return;
  2422. }
  2423. #if ENABLE_FEATURE_OSF_LABEL
  2424. if (bsd_trydev(device) < 0)
  2425. #endif
  2426. printf(_("Disk %s doesn't contain a valid "
  2427. "partition table\n"), device);
  2428. close(fd);
  2429. } else {
  2430. close(fd);
  2431. list_table(0);
  2432. #if ENABLE_FEATURE_FDISK_WRITABLE
  2433. if (!LABEL_IS_SUN && partitions > 4){
  2434. delete_partition(ext_index);
  2435. }
  2436. #endif
  2437. }
  2438. } else {
  2439. /* Ignore other errors, since we try IDE
  2440. and SCSI hard disks which may not be
  2441. installed on the system. */
  2442. if (errno == EACCES) {
  2443. printf(_("Cannot open %s\n"), device);
  2444. return;
  2445. }
  2446. }
  2447. }
  2448. /* for fdisk -l: try all things in /proc/partitions
  2449. that look like a partition name (do not end in a digit) */
  2450. static void
  2451. tryprocpt(void)
  2452. {
  2453. FILE *procpt;
  2454. char line[100], ptname[100], devname[120], *s;
  2455. int ma, mi, sz;
  2456. procpt = fopen_or_warn("/proc/partitions", "r");
  2457. while (fgets(line, sizeof(line), procpt)) {
  2458. if (sscanf(line, " %d %d %d %[^\n ]",
  2459. &ma, &mi, &sz, ptname) != 4)
  2460. continue;
  2461. for (s = ptname; *s; s++);
  2462. if (isdigit(s[-1]))
  2463. continue;
  2464. sprintf(devname, "/dev/%s", ptname);
  2465. trydev(devname, 0);
  2466. }
  2467. #if ENABLE_FEATURE_CLEAN_UP
  2468. fclose(procpt);
  2469. #endif
  2470. }
  2471. #if ENABLE_FEATURE_FDISK_WRITABLE
  2472. static void
  2473. unknown_command(int c)
  2474. {
  2475. printf(_("%c: unknown command\n"), c);
  2476. }
  2477. #endif
  2478. int fdisk_main(int argc, char **argv)
  2479. {
  2480. char *str_b, *str_C, *str_H, *str_S;
  2481. unsigned opt;
  2482. /*
  2483. * fdisk -v
  2484. * fdisk -l [-b sectorsize] [-u] device ...
  2485. * fdisk -s [partition] ...
  2486. * fdisk [-b sectorsize] [-u] device
  2487. *
  2488. * Options -C, -H, -S set the geometry.
  2489. */
  2490. enum {
  2491. OPT_b = 1 << 0,
  2492. OPT_C = 1 << 1,
  2493. OPT_H = 1 << 2,
  2494. OPT_l = 1 << 3,
  2495. OPT_S = 1 << 4,
  2496. OPT_u = 1 << 5,
  2497. OPT_s = (1 << 6) * ENABLE_FEATURE_FDISK_BLKSIZE,
  2498. };
  2499. opt = getopt32(argc, argv, "b:C:H:lS:u" USE_FEATURE_FDISK_BLKSIZE("s"),
  2500. &str_b, &str_C, &str_H, &str_S);
  2501. argc -= optind;
  2502. argv += optind;
  2503. if (opt & OPT_b) { // -b
  2504. /* Ugly: this sector size is really per device,
  2505. so cannot be combined with multiple disks,
  2506. and the same goes for the C/H/S options.
  2507. */
  2508. sector_size = xatoi_u(str_b);
  2509. if (sector_size != 512 && sector_size != 1024 &&
  2510. sector_size != 2048)
  2511. bb_show_usage();
  2512. sector_offset = 2;
  2513. user_set_sector_size = 1;
  2514. }
  2515. if (opt & OPT_C) user_cylinders = xatoi_u(str_C); // -C
  2516. if (opt & OPT_H) { // -H
  2517. user_heads = xatoi_u(str_H);
  2518. if (user_heads <= 0 || user_heads >= 256)
  2519. user_heads = 0;
  2520. }
  2521. //if (opt & OPT_l) // -l
  2522. if (opt & OPT_S) { // -S
  2523. user_sectors = xatoi_u(str_S);
  2524. if (user_sectors <= 0 || user_sectors >= 64)
  2525. user_sectors = 0;
  2526. }
  2527. if (opt & OPT_u) display_in_cyl_units = 0; // -u
  2528. //if (opt & OPT_s) // -s
  2529. if (user_set_sector_size && argc != 1)
  2530. printf(_("Warning: the -b (set sector size) option should"
  2531. " be used with one specified device\n"));
  2532. #if ENABLE_FEATURE_FDISK_WRITABLE
  2533. if (opt & OPT_l) {
  2534. nowarn = 1;
  2535. #endif
  2536. type_open = O_RDONLY;
  2537. if (argc > 0) {
  2538. int k;
  2539. #if defined(__GNUC__)
  2540. /* avoid gcc warning:
  2541. variable `k' might be clobbered by `longjmp' */
  2542. (void)&k;
  2543. #endif
  2544. listing = 1;
  2545. for (k = 0; k < argc; k++)
  2546. trydev(argv[k], 1);
  2547. } else {
  2548. /* we no longer have default device names */
  2549. /* but, we can use /proc/partitions instead */
  2550. tryprocpt();
  2551. }
  2552. return 0;
  2553. #if ENABLE_FEATURE_FDISK_WRITABLE
  2554. }
  2555. #endif
  2556. #if ENABLE_FEATURE_FDISK_BLKSIZE
  2557. if (opt & OPT_s) {
  2558. long size;
  2559. int j;
  2560. nowarn = 1;
  2561. type_open = O_RDONLY;
  2562. if (argc <= 0)
  2563. bb_show_usage();
  2564. for (j = 0; j < argc; j++) {
  2565. disk_device = argv[j];
  2566. fd = open(disk_device, type_open);
  2567. if (fd < 0)
  2568. fdisk_fatal(unable_to_open);
  2569. if (ioctl(fd, BLKGETSIZE, &size))
  2570. fdisk_fatal(ioctl_error);
  2571. close(fd);
  2572. if (argc == 1)
  2573. printf("%ld\n", size/2);
  2574. else
  2575. printf("%s: %ld\n", argv[j], size/2);
  2576. }
  2577. return 0;
  2578. }
  2579. #endif
  2580. #if ENABLE_FEATURE_FDISK_WRITABLE
  2581. if (argc != 1)
  2582. bb_show_usage();
  2583. disk_device = argv[0];
  2584. get_boot(fdisk);
  2585. if (LABEL_IS_OSF) {
  2586. /* OSF label, and no DOS label */
  2587. printf(_("Detected an OSF/1 disklabel on %s, entering "
  2588. "disklabel mode.\n"), disk_device);
  2589. bsd_select();
  2590. /*Why do we do this? It seems to be counter-intuitive*/
  2591. current_label_type = label_dos;
  2592. /* If we return we may want to make an empty DOS label? */
  2593. }
  2594. while (1) {
  2595. int c;
  2596. putchar('\n');
  2597. c = tolower(read_nonempty(_("Command (m for help): ")));
  2598. switch (c) {
  2599. case 'a':
  2600. if (LABEL_IS_DOS)
  2601. toggle_active(get_partition(1, partitions));
  2602. else if (LABEL_IS_SUN)
  2603. toggle_sunflags(get_partition(1, partitions),
  2604. 0x01);
  2605. else if (LABEL_IS_SGI)
  2606. sgi_set_bootpartition(
  2607. get_partition(1, partitions));
  2608. else
  2609. unknown_command(c);
  2610. break;
  2611. case 'b':
  2612. if (LABEL_IS_SGI) {
  2613. printf(_("\nThe current boot file is: %s\n"),
  2614. sgi_get_bootfile());
  2615. if (read_maybe_empty(_("Please enter the name of the "
  2616. "new boot file: ")) == '\n')
  2617. printf(_("Boot file unchanged\n"));
  2618. else
  2619. sgi_set_bootfile(line_ptr);
  2620. }
  2621. #if ENABLE_FEATURE_OSF_LABEL
  2622. else
  2623. bsd_select();
  2624. #endif
  2625. break;
  2626. case 'c':
  2627. if (LABEL_IS_DOS)
  2628. toggle_dos_compatibility_flag();
  2629. else if (LABEL_IS_SUN)
  2630. toggle_sunflags(get_partition(1, partitions),
  2631. 0x10);
  2632. else if (LABEL_IS_SGI)
  2633. sgi_set_swappartition(
  2634. get_partition(1, partitions));
  2635. else
  2636. unknown_command(c);
  2637. break;
  2638. case 'd':
  2639. {
  2640. int j;
  2641. /* If sgi_label then don't use get_existing_partition,
  2642. let the user select a partition, since
  2643. get_existing_partition() only works for Linux-like
  2644. partition tables */
  2645. if (!LABEL_IS_SGI) {
  2646. j = get_existing_partition(1, partitions);
  2647. } else {
  2648. j = get_partition(1, partitions);
  2649. }
  2650. if (j >= 0)
  2651. delete_partition(j);
  2652. }
  2653. break;
  2654. case 'i':
  2655. if (LABEL_IS_SGI)
  2656. create_sgiinfo();
  2657. else
  2658. unknown_command(c);
  2659. case 'l':
  2660. list_types(get_sys_types());
  2661. break;
  2662. case 'm':
  2663. menu();
  2664. break;
  2665. case 'n':
  2666. new_partition();
  2667. break;
  2668. case 'o':
  2669. create_doslabel();
  2670. break;
  2671. case 'p':
  2672. list_table(0);
  2673. break;
  2674. case 'q':
  2675. close(fd);
  2676. puts("");
  2677. return 0;
  2678. case 's':
  2679. #if ENABLE_FEATURE_SUN_LABEL
  2680. create_sunlabel();
  2681. #endif
  2682. break;
  2683. case 't':
  2684. change_sysid();
  2685. break;
  2686. case 'u':
  2687. change_units();
  2688. break;
  2689. case 'v':
  2690. verify();
  2691. break;
  2692. case 'w':
  2693. write_table(); /* does not return */
  2694. break;
  2695. #if ENABLE_FEATURE_FDISK_ADVANCED
  2696. case 'x':
  2697. if (LABEL_IS_SGI) {
  2698. printf(_("\n\tSorry, no experts menu for SGI "
  2699. "partition tables available.\n\n"));
  2700. } else
  2701. xselect();
  2702. break;
  2703. #endif
  2704. default:
  2705. unknown_command(c);
  2706. menu();
  2707. }
  2708. }
  2709. return 0;
  2710. #endif /* FEATURE_FDISK_WRITABLE */
  2711. }