3
0

fdisk.c 81 KB

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