fdisk.c 81 KB

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