fdisk.c 77 KB

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