fdisk.c 72 KB

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