fdisk.c 76 KB

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