fdisk.c 81 KB

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