fdisk.c 80 KB

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