fdisk.c 155 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627162816291630163116321633163416351636163716381639164016411642164316441645164616471648164916501651165216531654165516561657165816591660166116621663166416651666166716681669167016711672167316741675167616771678167916801681168216831684168516861687168816891690169116921693169416951696169716981699170017011702170317041705170617071708170917101711171217131714171517161717171817191720172117221723172417251726172717281729173017311732173317341735173617371738173917401741174217431744174517461747174817491750175117521753175417551756175717581759176017611762176317641765176617671768176917701771177217731774177517761777177817791780178117821783178417851786178717881789179017911792179317941795179617971798179918001801180218031804180518061807180818091810181118121813181418151816181718181819182018211822182318241825182618271828182918301831183218331834183518361837183818391840184118421843184418451846184718481849185018511852185318541855185618571858185918601861186218631864186518661867186818691870187118721873187418751876187718781879188018811882188318841885188618871888188918901891189218931894189518961897189818991900190119021903190419051906190719081909191019111912191319141915191619171918191919201921192219231924192519261927192819291930193119321933193419351936193719381939194019411942194319441945194619471948194919501951195219531954195519561957195819591960196119621963196419651966196719681969197019711972197319741975197619771978197919801981198219831984198519861987198819891990199119921993199419951996199719981999200020012002200320042005200620072008200920102011201220132014201520162017201820192020202120222023202420252026202720282029203020312032203320342035203620372038203920402041204220432044204520462047204820492050205120522053205420552056205720582059206020612062206320642065206620672068206920702071207220732074207520762077207820792080208120822083208420852086208720882089209020912092209320942095209620972098209921002101210221032104210521062107210821092110211121122113211421152116211721182119212021212122212321242125212621272128212921302131213221332134213521362137213821392140214121422143214421452146214721482149215021512152215321542155215621572158215921602161216221632164216521662167216821692170217121722173217421752176217721782179218021812182218321842185218621872188218921902191219221932194219521962197219821992200220122022203220422052206220722082209221022112212221322142215221622172218221922202221222222232224222522262227222822292230223122322233223422352236223722382239224022412242224322442245224622472248224922502251225222532254225522562257225822592260226122622263226422652266226722682269227022712272227322742275227622772278227922802281228222832284228522862287228822892290229122922293229422952296229722982299230023012302230323042305230623072308230923102311231223132314231523162317231823192320232123222323232423252326232723282329233023312332233323342335233623372338233923402341234223432344234523462347234823492350235123522353235423552356235723582359236023612362236323642365236623672368236923702371237223732374237523762377237823792380238123822383238423852386238723882389239023912392239323942395239623972398239924002401240224032404240524062407240824092410241124122413241424152416241724182419242024212422242324242425242624272428242924302431243224332434243524362437243824392440244124422443244424452446244724482449245024512452245324542455245624572458245924602461246224632464246524662467246824692470247124722473247424752476247724782479248024812482248324842485248624872488248924902491249224932494249524962497249824992500250125022503250425052506250725082509251025112512251325142515251625172518251925202521252225232524252525262527252825292530253125322533253425352536253725382539254025412542254325442545254625472548254925502551255225532554255525562557255825592560256125622563256425652566256725682569257025712572257325742575257625772578257925802581258225832584258525862587258825892590259125922593259425952596259725982599260026012602260326042605260626072608260926102611261226132614261526162617261826192620262126222623262426252626262726282629263026312632263326342635263626372638263926402641264226432644264526462647264826492650265126522653265426552656265726582659266026612662266326642665266626672668266926702671267226732674267526762677267826792680268126822683268426852686268726882689269026912692269326942695269626972698269927002701270227032704270527062707270827092710271127122713271427152716271727182719272027212722272327242725272627272728272927302731273227332734273527362737273827392740274127422743274427452746274727482749275027512752275327542755275627572758275927602761276227632764276527662767276827692770277127722773277427752776277727782779278027812782278327842785278627872788278927902791279227932794279527962797279827992800280128022803280428052806280728082809281028112812281328142815281628172818281928202821282228232824282528262827282828292830283128322833283428352836283728382839284028412842284328442845284628472848284928502851285228532854285528562857285828592860286128622863286428652866286728682869287028712872287328742875287628772878287928802881288228832884288528862887288828892890289128922893289428952896289728982899290029012902290329042905290629072908290929102911291229132914291529162917291829192920292129222923292429252926292729282929293029312932293329342935293629372938293929402941294229432944294529462947294829492950295129522953295429552956295729582959296029612962296329642965296629672968296929702971297229732974297529762977297829792980298129822983298429852986298729882989299029912992299329942995299629972998299930003001300230033004300530063007300830093010301130123013301430153016301730183019302030213022302330243025302630273028302930303031303230333034303530363037303830393040304130423043304430453046304730483049305030513052305330543055305630573058305930603061306230633064306530663067306830693070307130723073307430753076307730783079308030813082308330843085308630873088308930903091309230933094309530963097309830993100310131023103310431053106310731083109311031113112311331143115311631173118311931203121312231233124312531263127312831293130313131323133313431353136313731383139314031413142314331443145314631473148314931503151315231533154315531563157315831593160316131623163316431653166316731683169317031713172317331743175317631773178317931803181318231833184318531863187318831893190319131923193319431953196319731983199320032013202320332043205320632073208320932103211321232133214321532163217321832193220322132223223322432253226322732283229323032313232323332343235323632373238323932403241324232433244324532463247324832493250325132523253325432553256325732583259326032613262326332643265326632673268326932703271327232733274327532763277327832793280328132823283328432853286328732883289329032913292329332943295329632973298329933003301330233033304330533063307330833093310331133123313331433153316331733183319332033213322332333243325332633273328332933303331333233333334333533363337333833393340334133423343334433453346334733483349335033513352335333543355335633573358335933603361336233633364336533663367336833693370337133723373337433753376337733783379338033813382338333843385338633873388338933903391339233933394339533963397339833993400340134023403340434053406340734083409341034113412341334143415341634173418341934203421342234233424342534263427342834293430343134323433343434353436343734383439344034413442344334443445344634473448344934503451345234533454345534563457345834593460346134623463346434653466346734683469347034713472347334743475347634773478347934803481348234833484348534863487348834893490349134923493349434953496349734983499350035013502350335043505350635073508350935103511351235133514351535163517351835193520352135223523352435253526352735283529353035313532353335343535353635373538353935403541354235433544354535463547354835493550355135523553355435553556355735583559356035613562356335643565356635673568356935703571357235733574357535763577357835793580358135823583358435853586358735883589359035913592359335943595359635973598359936003601360236033604360536063607360836093610361136123613361436153616361736183619362036213622362336243625362636273628362936303631363236333634363536363637363836393640364136423643364436453646364736483649365036513652365336543655365636573658365936603661366236633664366536663667366836693670367136723673367436753676367736783679368036813682368336843685368636873688368936903691369236933694369536963697369836993700370137023703370437053706370737083709371037113712371337143715371637173718371937203721372237233724372537263727372837293730373137323733373437353736373737383739374037413742374337443745374637473748374937503751375237533754375537563757375837593760376137623763376437653766376737683769377037713772377337743775377637773778377937803781378237833784378537863787378837893790379137923793379437953796379737983799380038013802380338043805380638073808380938103811381238133814381538163817381838193820382138223823382438253826382738283829383038313832383338343835383638373838383938403841384238433844384538463847384838493850385138523853385438553856385738583859386038613862386338643865386638673868386938703871387238733874387538763877387838793880388138823883388438853886388738883889389038913892389338943895389638973898389939003901390239033904390539063907390839093910391139123913391439153916391739183919392039213922392339243925392639273928392939303931393239333934393539363937393839393940394139423943394439453946394739483949395039513952395339543955395639573958395939603961396239633964396539663967396839693970397139723973397439753976397739783979398039813982398339843985398639873988398939903991399239933994399539963997399839994000400140024003400440054006400740084009401040114012401340144015401640174018401940204021402240234024402540264027402840294030403140324033403440354036403740384039404040414042404340444045404640474048404940504051405240534054405540564057405840594060406140624063406440654066406740684069407040714072407340744075407640774078407940804081408240834084408540864087408840894090409140924093409440954096409740984099410041014102410341044105410641074108410941104111411241134114411541164117411841194120412141224123412441254126412741284129413041314132413341344135413641374138413941404141414241434144414541464147414841494150415141524153415441554156415741584159416041614162416341644165416641674168416941704171417241734174417541764177417841794180418141824183418441854186418741884189419041914192419341944195419641974198419942004201420242034204420542064207420842094210421142124213421442154216421742184219422042214222422342244225422642274228422942304231423242334234423542364237423842394240424142424243424442454246424742484249425042514252425342544255425642574258425942604261426242634264426542664267426842694270427142724273427442754276427742784279428042814282428342844285428642874288428942904291429242934294429542964297429842994300430143024303430443054306430743084309431043114312431343144315431643174318431943204321432243234324432543264327432843294330433143324333433443354336433743384339434043414342434343444345434643474348434943504351435243534354435543564357435843594360436143624363436443654366436743684369437043714372437343744375437643774378437943804381438243834384438543864387438843894390439143924393439443954396439743984399440044014402440344044405440644074408440944104411441244134414441544164417441844194420442144224423442444254426442744284429443044314432443344344435443644374438443944404441444244434444444544464447444844494450445144524453445444554456445744584459446044614462446344644465446644674468446944704471447244734474447544764477447844794480448144824483448444854486448744884489449044914492449344944495449644974498449945004501450245034504450545064507450845094510451145124513451445154516451745184519452045214522452345244525452645274528452945304531453245334534453545364537453845394540454145424543454445454546454745484549455045514552455345544555455645574558455945604561456245634564456545664567456845694570457145724573457445754576457745784579458045814582458345844585458645874588458945904591459245934594459545964597459845994600460146024603460446054606460746084609461046114612461346144615461646174618461946204621462246234624462546264627462846294630463146324633463446354636463746384639464046414642464346444645464646474648464946504651465246534654465546564657465846594660466146624663466446654666466746684669467046714672467346744675467646774678467946804681468246834684468546864687468846894690469146924693469446954696469746984699470047014702470347044705470647074708470947104711471247134714471547164717471847194720472147224723472447254726472747284729473047314732473347344735473647374738473947404741474247434744474547464747474847494750475147524753475447554756475747584759476047614762476347644765476647674768476947704771477247734774477547764777477847794780478147824783478447854786478747884789479047914792479347944795479647974798479948004801480248034804480548064807480848094810481148124813481448154816481748184819482048214822482348244825482648274828482948304831483248334834483548364837483848394840484148424843484448454846484748484849485048514852485348544855485648574858485948604861486248634864486548664867486848694870487148724873487448754876487748784879488048814882488348844885488648874888488948904891489248934894489548964897489848994900490149024903490449054906490749084909491049114912491349144915491649174918491949204921492249234924492549264927492849294930493149324933493449354936493749384939494049414942494349444945494649474948494949504951495249534954495549564957495849594960496149624963496449654966496749684969497049714972497349744975497649774978497949804981498249834984498549864987498849894990499149924993499449954996499749984999500050015002500350045005500650075008500950105011501250135014501550165017501850195020502150225023502450255026502750285029503050315032503350345035503650375038503950405041504250435044504550465047504850495050505150525053505450555056505750585059506050615062506350645065506650675068506950705071507250735074507550765077507850795080508150825083508450855086508750885089509050915092509350945095509650975098509951005101510251035104510551065107510851095110511151125113511451155116511751185119512051215122512351245125512651275128512951305131513251335134513551365137513851395140514151425143514451455146514751485149515051515152515351545155515651575158515951605161516251635164516551665167516851695170517151725173517451755176517751785179518051815182518351845185518651875188518951905191519251935194519551965197519851995200520152025203520452055206520752085209521052115212521352145215521652175218521952205221522252235224522552265227522852295230523152325233523452355236523752385239524052415242524352445245524652475248524952505251525252535254525552565257525852595260526152625263526452655266526752685269527052715272527352745275527652775278527952805281528252835284528552865287528852895290529152925293529452955296529752985299530053015302530353045305530653075308530953105311531253135314531553165317531853195320532153225323532453255326532753285329533053315332533353345335533653375338533953405341534253435344534553465347534853495350535153525353535453555356535753585359536053615362536353645365536653675368536953705371537253735374537553765377537853795380538153825383538453855386538753885389539053915392539353945395539653975398539954005401540254035404540554065407540854095410541154125413541454155416541754185419542054215422542354245425542654275428542954305431543254335434543554365437543854395440544154425443544454455446544754485449545054515452545354545455545654575458545954605461546254635464546554665467546854695470547154725473547454755476547754785479548054815482548354845485548654875488548954905491549254935494549554965497549854995500550155025503550455055506550755085509551055115512551355145515551655175518551955205521552255235524552555265527552855295530553155325533553455355536553755385539554055415542554355445545554655475548554955505551555255535554555555565557555855595560556155625563556455655566556755685569557055715572557355745575557655775578557955805581558255835584558555865587558855895590559155925593559455955596559755985599560056015602560356045605560656075608560956105611561256135614561556165617561856195620562156225623562456255626562756285629563056315632563356345635563656375638563956405641564256435644564556465647564856495650565156525653565456555656565756585659566056615662566356645665566656675668566956705671567256735674567556765677567856795680568156825683568456855686568756885689569056915692569356945695569656975698569957005701570257035704570557065707570857095710571157125713571457155716571757185719572057215722572357245725572657275728572957305731573257335734573557365737573857395740574157425743574457455746574757485749575057515752575357545755575657575758575957605761576257635764576557665767576857695770577157725773577457755776577757785779578057815782578357845785578657875788578957905791579257935794579557965797579857995800580158025803580458055806580758085809581058115812581358145815581658175818581958205821582258235824582558265827582858295830583158325833583458355836583758385839584058415842584358445845584658475848584958505851585258535854585558565857585858595860586158625863586458655866586758685869587058715872587358745875587658775878587958805881588258835884588558865887588858895890589158925893589458955896589758985899590059015902590359045905590659075908590959105911591259135914591559165917591859195920592159225923592459255926592759285929593059315932593359345935593659375938593959405941594259435944594559465947594859495950595159525953595459555956595759585959596059615962596359645965596659675968596959705971
  1. /* fdisk.c -- Partition table manipulator for Linux.
  2. *
  3. * Copyright (C) 1992 A. V. Le Blanc (LeBlanc@mcc.ac.uk)
  4. * Copyright (C) 2001,2002 Vladimir Oleynik <dzo@simtreas.ru> (initial bb port)
  5. *
  6. * Licensed under the GPL v2 or later, see the file LICENSE in this tarball.
  7. */
  8. /* Current changes have not compatibility with this version */
  9. #define UTIL_LINUX_VERSION "2.12"
  10. #define PROC_PARTITIONS "/proc/partitions"
  11. #include <features.h>
  12. #include <sys/types.h>
  13. #include <sys/stat.h> /* stat */
  14. #include <ctype.h>
  15. #include <stdlib.h>
  16. #include <stdio.h>
  17. #include <string.h>
  18. #include <errno.h>
  19. #include <unistd.h>
  20. #include <fcntl.h>
  21. #include <setjmp.h>
  22. #include <assert.h> /* assert */
  23. #include <getopt.h>
  24. #include <endian.h>
  25. #include <sys/ioctl.h>
  26. #include <sys/param.h>
  27. #include <sys/sysmacros.h> /* major */
  28. #include <stdint.h> /* for uint32_t, uint16_t, uint8_t, int16_t, etc */
  29. /* Copied from linux/major.h */
  30. #define FLOPPY_MAJOR 2
  31. #include <sys/utsname.h>
  32. #include "busybox.h"
  33. #define DKTYPENAMES
  34. #define BLKRRPART _IO(0x12,95) /* re-read partition table */
  35. #define BLKGETSIZE _IO(0x12,96) /* return device size */
  36. #define BLKFLSBUF _IO(0x12,97) /* flush buffer cache */
  37. #define BLKSSZGET _IO(0x12,104) /* get block device sector size */
  38. /* Avoid conflicts with the 2.6 kernel headers, which define
  39. * _IOR rather differently */
  40. #undef _IOR
  41. #define _IOR(type,nr,size) _IOC(_IOC_READ,(type),(nr),sizeof(size))
  42. #define BLKGETSIZE64 _IOR(0x12,114,uint64_t)
  43. /*
  44. fdisk.h
  45. */
  46. #define DEFAULT_SECTOR_SIZE 512
  47. #define MAX_SECTOR_SIZE 2048
  48. #define SECTOR_SIZE 512 /* still used in BSD code */
  49. #define MAXIMUM_PARTS 60
  50. #define ACTIVE_FLAG 0x80
  51. #define EXTENDED 0x05
  52. #define WIN98_EXTENDED 0x0f
  53. #define LINUX_PARTITION 0x81
  54. #define LINUX_SWAP 0x82
  55. #define LINUX_NATIVE 0x83
  56. #define LINUX_EXTENDED 0x85
  57. #define LINUX_LVM 0x8e
  58. #define LINUX_RAID 0xfd
  59. #define SUNOS_SWAP 3
  60. #define WHOLE_DISK 5
  61. #define IS_EXTENDED(i) \
  62. ((i) == EXTENDED || (i) == WIN98_EXTENDED || (i) == LINUX_EXTENDED)
  63. #define SIZE(a) (sizeof(a)/sizeof((a)[0]))
  64. #define cround(n) (display_in_cyl_units ? ((n)/units_per_sector)+1 : (n))
  65. #define scround(x) (((x)+units_per_sector-1)/units_per_sector)
  66. #ifdef CONFIG_FEATURE_SUN_LABEL
  67. #define SCSI_IOCTL_GET_IDLUN 0x5382
  68. #endif
  69. /* including <linux/hdreg.h> also fails */
  70. struct hd_geometry {
  71. unsigned char heads;
  72. unsigned char sectors;
  73. unsigned short cylinders;
  74. unsigned long start;
  75. };
  76. #define HDIO_GETGEO 0x0301 /* get device geometry */
  77. struct systypes {
  78. const char *name;
  79. };
  80. static uint sector_size = DEFAULT_SECTOR_SIZE;
  81. static uint user_set_sector_size;
  82. static uint sector_offset = 1;
  83. /*
  84. * Raw disk label. For DOS-type partition tables the MBR,
  85. * with descriptions of the primary partitions.
  86. */
  87. #if (MAX_SECTOR_SIZE) > (BUFSIZ+1)
  88. static char MBRbuffer[MAX_SECTOR_SIZE];
  89. #else
  90. # define MBRbuffer bb_common_bufsiz1
  91. #endif
  92. #ifdef CONFIG_FEATURE_OSF_LABEL
  93. static int possibly_osf_label;
  94. #endif
  95. static uint heads, sectors, cylinders;
  96. static void update_units(void);
  97. /*
  98. * return partition name - uses static storage unless buf is supplied
  99. */
  100. static const char *
  101. partname(const char *dev, int pno, int lth)
  102. {
  103. static char buffer[80];
  104. const char *p;
  105. int w, wp;
  106. int bufsiz;
  107. char *bufp;
  108. bufp = buffer;
  109. bufsiz = sizeof(buffer);
  110. w = strlen(dev);
  111. p = "";
  112. if (isdigit(dev[w-1]))
  113. p = "p";
  114. /* devfs kludge - note: fdisk partition names are not supposed
  115. to equal kernel names, so there is no reason to do this */
  116. if (strcmp(dev + w - 4, "disc") == 0) {
  117. w -= 4;
  118. p = "part";
  119. }
  120. wp = strlen(p);
  121. if (lth) {
  122. snprintf(bufp, bufsiz, "%*.*s%s%-2u",
  123. lth-wp-2, w, dev, p, pno);
  124. } else {
  125. snprintf(bufp, bufsiz, "%.*s%s%-2u", w, dev, p, pno);
  126. }
  127. return bufp;
  128. }
  129. struct partition {
  130. unsigned char boot_ind; /* 0x80 - active */
  131. unsigned char head; /* starting head */
  132. unsigned char sector; /* starting sector */
  133. unsigned char cyl; /* starting cylinder */
  134. unsigned char sys_ind; /* What partition type */
  135. unsigned char end_head; /* end head */
  136. unsigned char end_sector; /* end sector */
  137. unsigned char end_cyl; /* end cylinder */
  138. unsigned char start4[4]; /* starting sector counting from 0 */
  139. unsigned char size4[4]; /* nr of sectors in partition */
  140. } ATTRIBUTE_PACKED;
  141. enum failure {
  142. ioctl_error, unable_to_open, unable_to_read, unable_to_seek,
  143. unable_to_write
  144. };
  145. enum label_type{
  146. label_dos, label_sun, label_sgi, label_aix, label_osf
  147. };
  148. enum action { fdisk, require, try_only, create_empty_dos, create_empty_sun };
  149. static enum label_type current_label_type;
  150. static const char *disk_device;
  151. static int fd; /* the disk */
  152. static int partitions = 4; /* maximum partition + 1 */
  153. static uint display_in_cyl_units = 1;
  154. static uint units_per_sector = 1;
  155. #ifdef CONFIG_FEATURE_FDISK_WRITABLE
  156. static char *line_ptr;
  157. static void change_units(void);
  158. static void reread_partition_table(int leave);
  159. static void delete_partition(int i);
  160. static int get_partition(int warn, int max);
  161. static void list_types(const struct systypes *sys);
  162. static uint read_int(uint low, uint dflt, uint high, uint base, char *mesg);
  163. #endif
  164. static const char *partition_type(unsigned char type);
  165. static void fdisk_fatal(enum failure why) ATTRIBUTE_NORETURN;
  166. static void get_geometry(void);
  167. static int get_boot(enum action what);
  168. #define PLURAL 0
  169. #define SINGULAR 1
  170. #define hex_val(c) ({ \
  171. char _c = (c); \
  172. isdigit(_c) ? _c - '0' : \
  173. tolower(_c) + 10 - 'a'; \
  174. })
  175. #define LINE_LENGTH 800
  176. #define pt_offset(b, n) ((struct partition *)((b) + 0x1be + \
  177. (n) * sizeof(struct partition)))
  178. #define sector(s) ((s) & 0x3f)
  179. #define cylinder(s, c) ((c) | (((s) & 0xc0) << 2))
  180. #define hsc2sector(h,s,c) (sector(s) - 1 + sectors * \
  181. ((h) + heads * cylinder(s,c)))
  182. #define set_hsc(h,s,c,sector) { \
  183. s = sector % sectors + 1; \
  184. sector /= sectors; \
  185. h = sector % heads; \
  186. sector /= heads; \
  187. c = sector & 0xff; \
  188. s |= (sector >> 2) & 0xc0; \
  189. }
  190. static int32_t get_start_sect(const struct partition *p);
  191. static int32_t get_nr_sects(const struct partition *p);
  192. /*
  193. * per partition table entry data
  194. *
  195. * The four primary partitions have the same sectorbuffer (MBRbuffer)
  196. * and have NULL ext_pointer.
  197. * Each logical partition table entry has two pointers, one for the
  198. * partition and one link to the next one.
  199. */
  200. static struct pte {
  201. struct partition *part_table; /* points into sectorbuffer */
  202. struct partition *ext_pointer; /* points into sectorbuffer */
  203. #ifdef CONFIG_FEATURE_FDISK_WRITABLE
  204. char changed; /* boolean */
  205. #endif
  206. off_t offset; /* disk sector number */
  207. char *sectorbuffer; /* disk sector contents */
  208. } ptes[MAXIMUM_PARTS];
  209. #ifdef CONFIG_FEATURE_FDISK_WRITABLE
  210. static void
  211. set_all_unchanged(void)
  212. {
  213. int i;
  214. for (i = 0; i < MAXIMUM_PARTS; i++)
  215. ptes[i].changed = 0;
  216. }
  217. static void
  218. set_changed(int i)
  219. {
  220. ptes[i].changed = 1;
  221. }
  222. #endif /* CONFIG_FEATURE_FDISK_WRITABLE */
  223. #if defined(CONFIG_FEATURE_SGI_LABEL) || defined(CONFIG_FEATURE_OSF_LABEL)
  224. static struct partition *
  225. get_part_table(int i)
  226. {
  227. return ptes[i].part_table;
  228. }
  229. #endif
  230. static const char *
  231. str_units(int n)
  232. { /* n==1: use singular */
  233. if (n == 1)
  234. return display_in_cyl_units ? _("cylinder") : _("sector");
  235. else
  236. return display_in_cyl_units ? _("cylinders") : _("sectors");
  237. }
  238. static int
  239. valid_part_table_flag(const char *mbuffer) {
  240. const unsigned char *b = (const unsigned char *)mbuffer;
  241. return (b[510] == 0x55 && b[511] == 0xaa);
  242. }
  243. #ifdef CONFIG_FEATURE_FDISK_WRITABLE
  244. static char line_buffer[LINE_LENGTH];
  245. /* read line; return 0 or first char */
  246. static int
  247. read_line(void)
  248. {
  249. static int got_eof = 0;
  250. fflush (stdout); /* requested by niles@scyld.com */
  251. line_ptr = line_buffer;
  252. if (!fgets(line_buffer, LINE_LENGTH, stdin)) {
  253. if (feof(stdin))
  254. got_eof++; /* user typed ^D ? */
  255. if (got_eof >= 3) {
  256. fprintf(stderr, _("\ngot EOF thrice - exiting..\n"));
  257. exit(1);
  258. }
  259. return 0;
  260. }
  261. while (*line_ptr && !isgraph(*line_ptr))
  262. line_ptr++;
  263. return *line_ptr;
  264. }
  265. static char
  266. read_char(const char *mesg)
  267. {
  268. do {
  269. fputs(mesg, stdout);
  270. } while (!read_line());
  271. return *line_ptr;
  272. }
  273. static char
  274. read_chars(const char *mesg)
  275. {
  276. fputs(mesg, stdout);
  277. if (!read_line()) {
  278. *line_ptr = '\n';
  279. line_ptr[1] = 0;
  280. }
  281. return *line_ptr;
  282. }
  283. static int
  284. read_hex(const struct systypes *sys)
  285. {
  286. int hex;
  287. while (1) {
  288. read_char(_("Hex code (type L to list codes): "));
  289. if (*line_ptr == 'l' || *line_ptr == 'L')
  290. list_types(sys);
  291. else if (isxdigit (*line_ptr)) {
  292. hex = 0;
  293. do
  294. hex = hex << 4 | hex_val(*line_ptr++);
  295. while (isxdigit(*line_ptr));
  296. return hex;
  297. }
  298. }
  299. }
  300. #endif /* CONFIG_FEATURE_FDISK_WRITABLE */
  301. #ifdef CONFIG_FEATURE_AIX_LABEL
  302. /*
  303. * Copyright (C) Andreas Neuper, Sep 1998.
  304. * This file may be redistributed under
  305. * the terms of the GNU Public License.
  306. */
  307. typedef struct {
  308. unsigned int magic; /* expect AIX_LABEL_MAGIC */
  309. unsigned int fillbytes1[124];
  310. unsigned int physical_volume_id;
  311. unsigned int fillbytes2[124];
  312. } aix_partition;
  313. #define AIX_LABEL_MAGIC 0xc9c2d4c1
  314. #define AIX_LABEL_MAGIC_SWAPPED 0xc1d4c2c9
  315. #define AIX_INFO_MAGIC 0x00072959
  316. #define AIX_INFO_MAGIC_SWAPPED 0x59290700
  317. #define aixlabel ((aix_partition *)MBRbuffer)
  318. /*
  319. Changes:
  320. * 1999-03-20 Arnaldo Carvalho de Melo <acme@conectiva.com.br>
  321. * Internationalization
  322. *
  323. * 2003-03-20 Phillip Kesling <pkesling@sgi.com>
  324. * Some fixes
  325. */
  326. static int aix_other_endian;
  327. static short aix_volumes = 1;
  328. /*
  329. * only dealing with free blocks here
  330. */
  331. static void
  332. aix_info(void)
  333. {
  334. puts(
  335. _("\n\tThere is a valid AIX label on this disk.\n"
  336. "\tUnfortunately Linux cannot handle these\n"
  337. "\tdisks at the moment. Nevertheless some\n"
  338. "\tadvice:\n"
  339. "\t1. fdisk will destroy its contents on write.\n"
  340. "\t2. Be sure that this disk is NOT a still vital\n"
  341. "\t part of a volume group. (Otherwise you may\n"
  342. "\t erase the other disks as well, if unmirrored.)\n"
  343. "\t3. Before deleting this physical volume be sure\n"
  344. "\t to remove the disk logically from your AIX\n"
  345. "\t machine. (Otherwise you become an AIXpert).")
  346. );
  347. }
  348. static int
  349. check_aix_label(void)
  350. {
  351. if (aixlabel->magic != AIX_LABEL_MAGIC &&
  352. aixlabel->magic != AIX_LABEL_MAGIC_SWAPPED) {
  353. current_label_type = 0;
  354. aix_other_endian = 0;
  355. return 0;
  356. }
  357. aix_other_endian = (aixlabel->magic == AIX_LABEL_MAGIC_SWAPPED);
  358. update_units();
  359. current_label_type = label_aix;
  360. partitions = 1016;
  361. aix_volumes = 15;
  362. aix_info();
  363. /*aix_nolabel();*/ /* %% */
  364. /*aix_label = 1;*/ /* %% */
  365. return 1;
  366. }
  367. #endif /* AIX_LABEL */
  368. #ifdef CONFIG_FEATURE_OSF_LABEL
  369. /*
  370. * Copyright (c) 1987, 1988 Regents of the University of California.
  371. * All rights reserved.
  372. *
  373. * Redistribution and use in source and binary forms, with or without
  374. * modification, are permitted provided that the following conditions
  375. * are met:
  376. * 1. Redistributions of source code must retain the above copyright
  377. * notice, this list of conditions and the following disclaimer.
  378. * 2. Redistributions in binary form must reproduce the above copyright
  379. * notice, this list of conditions and the following disclaimer in the
  380. * documentation and/or other materials provided with the distribution.
  381. * 3. All advertising materials mentioning features or use of this software
  382. * must display the following acknowledgment:
  383. * This product includes software developed by the University of
  384. * California, Berkeley and its contributors.
  385. * 4. Neither the name of the University nor the names of its contributors
  386. * may be used to endorse or promote products derived from this software
  387. * without specific prior written permission.
  388. *
  389. * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
  390. * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
  391. * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
  392. * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
  393. * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
  394. * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
  395. * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
  396. * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
  397. * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
  398. * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
  399. * SUCH DAMAGE.
  400. */
  401. #ifndef BSD_DISKMAGIC
  402. #define BSD_DISKMAGIC ((uint32_t) 0x82564557)
  403. #endif
  404. #ifndef BSD_MAXPARTITIONS
  405. #define BSD_MAXPARTITIONS 16
  406. #endif
  407. #define BSD_LINUX_BOOTDIR "/usr/ucb/mdec"
  408. #if defined (i386) || defined (__sparc__) || defined (__arm__) || defined (__mips__) || defined (__s390__) || defined (__sh__) || defined(__x86_64__)
  409. #define BSD_LABELSECTOR 1
  410. #define BSD_LABELOFFSET 0
  411. #elif defined (__alpha__) || defined (__powerpc__) || defined (__ia64__) || defined (__hppa__)
  412. #define BSD_LABELSECTOR 0
  413. #define BSD_LABELOFFSET 64
  414. #elif defined (__s390__) || defined (__s390x__)
  415. #define BSD_LABELSECTOR 1
  416. #define BSD_LABELOFFSET 0
  417. #else
  418. #error unknown architecture
  419. #endif
  420. #define BSD_BBSIZE 8192 /* size of boot area, with label */
  421. #define BSD_SBSIZE 8192 /* max size of fs superblock */
  422. struct xbsd_disklabel {
  423. uint32_t d_magic; /* the magic number */
  424. int16_t d_type; /* drive type */
  425. int16_t d_subtype; /* controller/d_type specific */
  426. char d_typename[16]; /* type name, e.g. "eagle" */
  427. char d_packname[16]; /* pack identifier */
  428. /* disk geometry: */
  429. uint32_t d_secsize; /* # of bytes per sector */
  430. uint32_t d_nsectors; /* # of data sectors per track */
  431. uint32_t d_ntracks; /* # of tracks per cylinder */
  432. uint32_t d_ncylinders; /* # of data cylinders per unit */
  433. uint32_t d_secpercyl; /* # of data sectors per cylinder */
  434. uint32_t d_secperunit; /* # of data sectors per unit */
  435. /*
  436. * Spares (bad sector replacements) below
  437. * are not counted in d_nsectors or d_secpercyl.
  438. * Spare sectors are assumed to be physical sectors
  439. * which occupy space at the end of each track and/or cylinder.
  440. */
  441. uint16_t d_sparespertrack; /* # of spare sectors per track */
  442. uint16_t d_sparespercyl; /* # of spare sectors per cylinder */
  443. /*
  444. * Alternate cylinders include maintenance, replacement,
  445. * configuration description areas, etc.
  446. */
  447. uint32_t d_acylinders; /* # of alt. cylinders per unit */
  448. /* hardware characteristics: */
  449. /*
  450. * d_interleave, d_trackskew and d_cylskew describe perturbations
  451. * in the media format used to compensate for a slow controller.
  452. * Interleave is physical sector interleave, set up by the formatter
  453. * or controller when formatting. When interleaving is in use,
  454. * logically adjacent sectors are not physically contiguous,
  455. * but instead are separated by some number of sectors.
  456. * It is specified as the ratio of physical sectors traversed
  457. * per logical sector. Thus an interleave of 1:1 implies contiguous
  458. * layout, while 2:1 implies that logical sector 0 is separated
  459. * by one sector from logical sector 1.
  460. * d_trackskew is the offset of sector 0 on track N
  461. * relative to sector 0 on track N-1 on the same cylinder.
  462. * Finally, d_cylskew is the offset of sector 0 on cylinder N
  463. * relative to sector 0 on cylinder N-1.
  464. */
  465. uint16_t d_rpm; /* rotational speed */
  466. uint16_t d_interleave; /* hardware sector interleave */
  467. uint16_t d_trackskew; /* sector 0 skew, per track */
  468. uint16_t d_cylskew; /* sector 0 skew, per cylinder */
  469. uint32_t d_headswitch; /* head switch time, usec */
  470. uint32_t d_trkseek; /* track-to-track seek, usec */
  471. uint32_t d_flags; /* generic flags */
  472. #define NDDATA 5
  473. uint32_t d_drivedata[NDDATA]; /* drive-type specific information */
  474. #define NSPARE 5
  475. uint32_t d_spare[NSPARE]; /* reserved for future use */
  476. uint32_t d_magic2; /* the magic number (again) */
  477. uint16_t d_checksum; /* xor of data incl. partitions */
  478. /* filesystem and partition information: */
  479. uint16_t d_npartitions; /* number of partitions in following */
  480. uint32_t d_bbsize; /* size of boot area at sn0, bytes */
  481. uint32_t d_sbsize; /* max size of fs superblock, bytes */
  482. struct xbsd_partition { /* the partition table */
  483. uint32_t p_size; /* number of sectors in partition */
  484. uint32_t p_offset; /* starting sector */
  485. uint32_t p_fsize; /* filesystem basic fragment size */
  486. uint8_t p_fstype; /* filesystem type, see below */
  487. uint8_t p_frag; /* filesystem fragments per block */
  488. uint16_t p_cpg; /* filesystem cylinders per group */
  489. } d_partitions[BSD_MAXPARTITIONS]; /* actually may be more */
  490. };
  491. /* d_type values: */
  492. #define BSD_DTYPE_SMD 1 /* SMD, XSMD; VAX hp/up */
  493. #define BSD_DTYPE_MSCP 2 /* MSCP */
  494. #define BSD_DTYPE_DEC 3 /* other DEC (rk, rl) */
  495. #define BSD_DTYPE_SCSI 4 /* SCSI */
  496. #define BSD_DTYPE_ESDI 5 /* ESDI interface */
  497. #define BSD_DTYPE_ST506 6 /* ST506 etc. */
  498. #define BSD_DTYPE_HPIB 7 /* CS/80 on HP-IB */
  499. #define BSD_DTYPE_HPFL 8 /* HP Fiber-link */
  500. #define BSD_DTYPE_FLOPPY 10 /* floppy */
  501. /* d_subtype values: */
  502. #define BSD_DSTYPE_INDOSPART 0x8 /* is inside dos partition */
  503. #define BSD_DSTYPE_DOSPART(s) ((s) & 3) /* dos partition number */
  504. #define BSD_DSTYPE_GEOMETRY 0x10 /* drive params in label */
  505. #ifdef DKTYPENAMES
  506. static const char * const xbsd_dktypenames[] = {
  507. "unknown",
  508. "SMD",
  509. "MSCP",
  510. "old DEC",
  511. "SCSI",
  512. "ESDI",
  513. "ST506",
  514. "HP-IB",
  515. "HP-FL",
  516. "type 9",
  517. "floppy",
  518. 0
  519. };
  520. #define BSD_DKMAXTYPES (sizeof(xbsd_dktypenames) / sizeof(xbsd_dktypenames[0]) - 1)
  521. #endif
  522. /*
  523. * Filesystem type and version.
  524. * Used to interpret other filesystem-specific
  525. * per-partition information.
  526. */
  527. #define BSD_FS_UNUSED 0 /* unused */
  528. #define BSD_FS_SWAP 1 /* swap */
  529. #define BSD_FS_V6 2 /* Sixth Edition */
  530. #define BSD_FS_V7 3 /* Seventh Edition */
  531. #define BSD_FS_SYSV 4 /* System V */
  532. #define BSD_FS_V71K 5 /* V7 with 1K blocks (4.1, 2.9) */
  533. #define BSD_FS_V8 6 /* Eighth Edition, 4K blocks */
  534. #define BSD_FS_BSDFFS 7 /* 4.2BSD fast file system */
  535. #define BSD_FS_BSDLFS 9 /* 4.4BSD log-structured file system */
  536. #define BSD_FS_OTHER 10 /* in use, but unknown/unsupported */
  537. #define BSD_FS_HPFS 11 /* OS/2 high-performance file system */
  538. #define BSD_FS_ISO9660 12 /* ISO-9660 filesystem (cdrom) */
  539. #define BSD_FS_ISOFS BSD_FS_ISO9660
  540. #define BSD_FS_BOOT 13 /* partition contains bootstrap */
  541. #define BSD_FS_ADOS 14 /* AmigaDOS fast file system */
  542. #define BSD_FS_HFS 15 /* Macintosh HFS */
  543. #define BSD_FS_ADVFS 16 /* Digital Unix AdvFS */
  544. /* this is annoying, but it's also the way it is :-( */
  545. #ifdef __alpha__
  546. #define BSD_FS_EXT2 8 /* ext2 file system */
  547. #else
  548. #define BSD_FS_MSDOS 8 /* MS-DOS file system */
  549. #endif
  550. #ifdef DKTYPENAMES
  551. static const struct systypes xbsd_fstypes[] = {
  552. { "\x00" "unused" }, /* BSD_FS_UNUSED */
  553. { "\x01" "swap" }, /* BSD_FS_SWAP */
  554. { "\x02" "Version 6" }, /* BSD_FS_V6 */
  555. { "\x03" "Version 7" }, /* BSD_FS_V7 */
  556. { "\x04" "System V" }, /* BSD_FS_SYSV */
  557. { "\x05" "4.1BSD" }, /* BSD_FS_V71K */
  558. { "\x06" "Eighth Edition" }, /* BSD_FS_V8 */
  559. { "\x07" "4.2BSD" }, /* BSD_FS_BSDFFS */
  560. #ifdef __alpha__
  561. { "\x08" "ext2" }, /* BSD_FS_EXT2 */
  562. #else
  563. { "\x08" "MS-DOS" }, /* BSD_FS_MSDOS */
  564. #endif
  565. { "\x09" "4.4LFS" }, /* BSD_FS_BSDLFS */
  566. { "\x0a" "unknown" }, /* BSD_FS_OTHER */
  567. { "\x0b" "HPFS" }, /* BSD_FS_HPFS */
  568. { "\x0c" "ISO-9660" }, /* BSD_FS_ISO9660 */
  569. { "\x0d" "boot" }, /* BSD_FS_BOOT */
  570. { "\x0e" "ADOS" }, /* BSD_FS_ADOS */
  571. { "\x0f" "HFS" }, /* BSD_FS_HFS */
  572. { "\x10" "AdvFS" }, /* BSD_FS_ADVFS */
  573. { NULL }
  574. };
  575. #define BSD_FSMAXTYPES (SIZE(xbsd_fstypes)-1)
  576. #endif
  577. /*
  578. * flags shared by various drives:
  579. */
  580. #define BSD_D_REMOVABLE 0x01 /* removable media */
  581. #define BSD_D_ECC 0x02 /* supports ECC */
  582. #define BSD_D_BADSECT 0x04 /* supports bad sector forw. */
  583. #define BSD_D_RAMDISK 0x08 /* disk emulator */
  584. #define BSD_D_CHAIN 0x10 /* can do back-back transfers */
  585. #define BSD_D_DOSPART 0x20 /* within MSDOS partition */
  586. #endif /* OSF_LABEL */
  587. /*
  588. * Copyright (C) Andreas Neuper, Sep 1998.
  589. * This file may be modified and redistributed under
  590. * the terms of the GNU Public License.
  591. */
  592. struct device_parameter { /* 48 bytes */
  593. unsigned char skew;
  594. unsigned char gap1;
  595. unsigned char gap2;
  596. unsigned char sparecyl;
  597. unsigned short pcylcount;
  598. unsigned short head_vol0;
  599. unsigned short ntrks; /* tracks in cyl 0 or vol 0 */
  600. unsigned char cmd_tag_queue_depth;
  601. unsigned char unused0;
  602. unsigned short unused1;
  603. unsigned short nsect; /* sectors/tracks in cyl 0 or vol 0 */
  604. unsigned short bytes;
  605. unsigned short ilfact;
  606. unsigned int flags; /* controller flags */
  607. unsigned int datarate;
  608. unsigned int retries_on_error;
  609. unsigned int ms_per_word;
  610. unsigned short xylogics_gap1;
  611. unsigned short xylogics_syncdelay;
  612. unsigned short xylogics_readdelay;
  613. unsigned short xylogics_gap2;
  614. unsigned short xylogics_readgate;
  615. unsigned short xylogics_writecont;
  616. };
  617. #define SGI_VOLHDR 0x00
  618. /* 1 and 2 were used for drive types no longer supported by SGI */
  619. #define SGI_SWAP 0x03
  620. /* 4 and 5 were for filesystem types SGI haven't ever supported on MIPS CPUs */
  621. #define SGI_VOLUME 0x06
  622. #define SGI_EFS 0x07
  623. #define SGI_LVOL 0x08
  624. #define SGI_RLVOL 0x09
  625. #define SGI_XFS 0x0a
  626. #define SGI_XFSLOG 0x0b
  627. #define SGI_XLV 0x0c
  628. #define SGI_XVM 0x0d
  629. #define ENTIRE_DISK SGI_VOLUME
  630. /*
  631. * controller flags
  632. */
  633. #define SECTOR_SLIP 0x01
  634. #define SECTOR_FWD 0x02
  635. #define TRACK_FWD 0x04
  636. #define TRACK_MULTIVOL 0x08
  637. #define IGNORE_ERRORS 0x10
  638. #define RESEEK 0x20
  639. #define ENABLE_CMDTAGQ 0x40
  640. typedef struct {
  641. unsigned int magic; /* expect SGI_LABEL_MAGIC */
  642. unsigned short boot_part; /* active boot partition */
  643. unsigned short swap_part; /* active swap partition */
  644. unsigned char boot_file[16]; /* name of the bootfile */
  645. struct device_parameter devparam; /* 1 * 48 bytes */
  646. struct volume_directory { /* 15 * 16 bytes */
  647. unsigned char vol_file_name[8]; /* a character array */
  648. unsigned int vol_file_start; /* number of logical block */
  649. unsigned int vol_file_size; /* number of bytes */
  650. } directory[15];
  651. struct sgi_partition { /* 16 * 12 bytes */
  652. unsigned int num_sectors; /* number of blocks */
  653. unsigned int start_sector; /* must be cylinder aligned */
  654. unsigned int id;
  655. } partitions[16];
  656. unsigned int csum;
  657. unsigned int fillbytes;
  658. } sgi_partition;
  659. typedef struct {
  660. unsigned int magic; /* looks like a magic number */
  661. unsigned int a2;
  662. unsigned int a3;
  663. unsigned int a4;
  664. unsigned int b1;
  665. unsigned short b2;
  666. unsigned short b3;
  667. unsigned int c[16];
  668. unsigned short d[3];
  669. unsigned char scsi_string[50];
  670. unsigned char serial[137];
  671. unsigned short check1816;
  672. unsigned char installer[225];
  673. } sgiinfo;
  674. #define SGI_LABEL_MAGIC 0x0be5a941
  675. #define SGI_LABEL_MAGIC_SWAPPED 0x41a9e50b
  676. #define SGI_INFO_MAGIC 0x00072959
  677. #define SGI_INFO_MAGIC_SWAPPED 0x59290700
  678. #define SGI_SSWAP16(x) (sgi_other_endian ? __swap16(x) \
  679. : (uint16_t)(x))
  680. #define SGI_SSWAP32(x) (sgi_other_endian ? __swap32(x) \
  681. : (uint32_t)(x))
  682. #define sgilabel ((sgi_partition *)MBRbuffer)
  683. #define sgiparam (sgilabel->devparam)
  684. typedef struct {
  685. unsigned char info[128]; /* Informative text string */
  686. unsigned char spare0[14];
  687. struct sun_info {
  688. unsigned char spare1;
  689. unsigned char id;
  690. unsigned char spare2;
  691. unsigned char flags;
  692. } infos[8];
  693. unsigned char spare1[246]; /* Boot information etc. */
  694. unsigned short rspeed; /* Disk rotational speed */
  695. unsigned short pcylcount; /* Physical cylinder count */
  696. unsigned short sparecyl; /* extra sects per cylinder */
  697. unsigned char spare2[4]; /* More magic... */
  698. unsigned short ilfact; /* Interleave factor */
  699. unsigned short ncyl; /* Data cylinder count */
  700. unsigned short nacyl; /* Alt. cylinder count */
  701. unsigned short ntrks; /* Tracks per cylinder */
  702. unsigned short nsect; /* Sectors per track */
  703. unsigned char spare3[4]; /* Even more magic... */
  704. struct sun_partition {
  705. uint32_t start_cylinder;
  706. uint32_t num_sectors;
  707. } partitions[8];
  708. unsigned short magic; /* Magic number */
  709. unsigned short csum; /* Label xor'd checksum */
  710. } sun_partition;
  711. #define SUN_LABEL_MAGIC 0xDABE
  712. #define SUN_LABEL_MAGIC_SWAPPED 0xBEDA
  713. #define sunlabel ((sun_partition *)MBRbuffer)
  714. #define SUN_SSWAP16(x) (sun_other_endian ? __swap16(x) \
  715. : (uint16_t)(x))
  716. #define SUN_SSWAP32(x) (sun_other_endian ? __swap32(x) \
  717. : (uint32_t)(x))
  718. #ifdef CONFIG_FEATURE_OSF_LABEL
  719. /*
  720. Changes:
  721. 19990319 - Arnaldo Carvalho de Melo <acme@conectiva.com.br> - i18n/nls
  722. 20000101 - David Huggins-Daines <dhuggins@linuxcare.com> - Better
  723. support for OSF/1 disklabels on Alpha.
  724. Also fixed unaligned accesses in alpha_bootblock_checksum()
  725. */
  726. #define FREEBSD_PARTITION 0xa5
  727. #define NETBSD_PARTITION 0xa9
  728. static void xbsd_delete_part(void);
  729. static void xbsd_new_part(void);
  730. static void xbsd_write_disklabel(void);
  731. static int xbsd_create_disklabel(void);
  732. static void xbsd_edit_disklabel(void);
  733. static void xbsd_write_bootstrap(void);
  734. static void xbsd_change_fstype(void);
  735. static int xbsd_get_part_index(int max);
  736. static int xbsd_check_new_partition(int *i);
  737. static void xbsd_list_types(void);
  738. static u_short xbsd_dkcksum(struct xbsd_disklabel *lp);
  739. static int xbsd_initlabel(struct partition *p, struct xbsd_disklabel *d);
  740. static int xbsd_readlabel(struct partition *p, struct xbsd_disklabel *d);
  741. static int xbsd_writelabel(struct partition *p, struct xbsd_disklabel *d);
  742. #if defined (__alpha__)
  743. static void alpha_bootblock_checksum(char *boot);
  744. #endif
  745. #if !defined (__alpha__)
  746. static int xbsd_translate_fstype(int linux_type);
  747. static void xbsd_link_part(void);
  748. static struct partition *xbsd_part;
  749. static int xbsd_part_index;
  750. #endif
  751. #if defined (__alpha__)
  752. /* We access this through a uint64_t * when checksumming */
  753. static char disklabelbuffer[BSD_BBSIZE] ATTRIBUTE_ALIGNED(8);
  754. #else
  755. static char disklabelbuffer[BSD_BBSIZE];
  756. #endif
  757. static struct xbsd_disklabel xbsd_dlabel;
  758. #define bsd_cround(n) \
  759. (display_in_cyl_units ? ((n)/xbsd_dlabel.d_secpercyl) + 1 : (n))
  760. /*
  761. * Test whether the whole disk has BSD disk label magic.
  762. *
  763. * Note: often reformatting with DOS-type label leaves the BSD magic,
  764. * so this does not mean that there is a BSD disk label.
  765. */
  766. static int
  767. check_osf_label(void)
  768. {
  769. if (xbsd_readlabel(NULL, &xbsd_dlabel) == 0)
  770. return 0;
  771. return 1;
  772. }
  773. static void xbsd_print_disklabel(int);
  774. static int
  775. btrydev(const char * dev)
  776. {
  777. if (xbsd_readlabel (NULL, &xbsd_dlabel) == 0)
  778. return -1;
  779. printf(_("\nBSD label for device: %s\n"), dev);
  780. xbsd_print_disklabel (0);
  781. return 0;
  782. }
  783. static void
  784. bmenu(void)
  785. {
  786. puts (_("Command action"));
  787. puts (_("\td\tdelete a BSD partition"));
  788. puts (_("\te\tedit drive data"));
  789. puts (_("\ti\tinstall bootstrap"));
  790. puts (_("\tl\tlist known filesystem types"));
  791. puts (_("\tm\tprint this menu"));
  792. puts (_("\tn\tadd a new BSD partition"));
  793. puts (_("\tp\tprint BSD partition table"));
  794. puts (_("\tq\tquit without saving changes"));
  795. puts (_("\tr\treturn to main menu"));
  796. puts (_("\ts\tshow complete disklabel"));
  797. puts (_("\tt\tchange a partition's filesystem id"));
  798. puts (_("\tu\tchange units (cylinders/sectors)"));
  799. puts (_("\tw\twrite disklabel to disk"));
  800. #if !defined (__alpha__)
  801. puts (_("\tx\tlink BSD partition to non-BSD partition"));
  802. #endif
  803. }
  804. #if !defined (__alpha__)
  805. static int
  806. hidden(int type)
  807. {
  808. return type ^ 0x10;
  809. }
  810. static int
  811. is_bsd_partition_type(int type)
  812. {
  813. return (type == FREEBSD_PARTITION ||
  814. type == hidden(FREEBSD_PARTITION) ||
  815. type == NETBSD_PARTITION ||
  816. type == hidden(NETBSD_PARTITION));
  817. }
  818. #endif
  819. static void
  820. bselect(void)
  821. {
  822. #if !defined (__alpha__)
  823. int t, ss;
  824. struct partition *p;
  825. for (t = 0; t < 4; t++) {
  826. p = get_part_table(t);
  827. if (p && is_bsd_partition_type(p->sys_ind)) {
  828. xbsd_part = p;
  829. xbsd_part_index = t;
  830. ss = get_start_sect(xbsd_part);
  831. if (ss == 0) {
  832. fprintf(stderr, _("Partition %s has invalid starting sector 0.\n"),
  833. partname(disk_device, t+1, 0));
  834. return;
  835. }
  836. printf(_("Reading disklabel of %s at sector %d.\n"),
  837. partname(disk_device, t+1, 0), ss + BSD_LABELSECTOR);
  838. if (xbsd_readlabel(xbsd_part, &xbsd_dlabel) == 0)
  839. if (xbsd_create_disklabel() == 0)
  840. return;
  841. break;
  842. }
  843. }
  844. if (t == 4) {
  845. printf(_("There is no *BSD partition on %s.\n"), disk_device);
  846. return;
  847. }
  848. #elif defined (__alpha__)
  849. if (xbsd_readlabel(NULL, &xbsd_dlabel) == 0)
  850. if (xbsd_create_disklabel() == 0)
  851. exit (EXIT_SUCCESS);
  852. #endif
  853. while (1) {
  854. putchar('\n');
  855. switch (tolower(read_char(_("BSD disklabel command (m for help): ")))) {
  856. case 'd':
  857. xbsd_delete_part();
  858. break;
  859. case 'e':
  860. xbsd_edit_disklabel();
  861. break;
  862. case 'i':
  863. xbsd_write_bootstrap();
  864. break;
  865. case 'l':
  866. xbsd_list_types();
  867. break;
  868. case 'n':
  869. xbsd_new_part();
  870. break;
  871. case 'p':
  872. xbsd_print_disklabel(0);
  873. break;
  874. case 'q':
  875. close(fd);
  876. exit(EXIT_SUCCESS);
  877. case 'r':
  878. return;
  879. case 's':
  880. xbsd_print_disklabel(1);
  881. break;
  882. case 't':
  883. xbsd_change_fstype();
  884. break;
  885. case 'u':
  886. change_units();
  887. break;
  888. case 'w':
  889. xbsd_write_disklabel();
  890. break;
  891. #if !defined (__alpha__)
  892. case 'x':
  893. xbsd_link_part();
  894. break;
  895. #endif
  896. default:
  897. bmenu();
  898. break;
  899. }
  900. }
  901. }
  902. static void
  903. xbsd_delete_part(void)
  904. {
  905. int i;
  906. i = xbsd_get_part_index(xbsd_dlabel.d_npartitions);
  907. xbsd_dlabel.d_partitions[i].p_size = 0;
  908. xbsd_dlabel.d_partitions[i].p_offset = 0;
  909. xbsd_dlabel.d_partitions[i].p_fstype = BSD_FS_UNUSED;
  910. if (xbsd_dlabel.d_npartitions == i + 1)
  911. while (xbsd_dlabel.d_partitions[xbsd_dlabel.d_npartitions-1].p_size == 0)
  912. xbsd_dlabel.d_npartitions--;
  913. }
  914. static void
  915. xbsd_new_part(void)
  916. {
  917. off_t begin, end;
  918. char mesg[256];
  919. int i;
  920. if (!xbsd_check_new_partition(&i))
  921. return;
  922. #if !defined (__alpha__) && !defined (__powerpc__) && !defined (__hppa__)
  923. begin = get_start_sect(xbsd_part);
  924. end = begin + get_nr_sects(xbsd_part) - 1;
  925. #else
  926. begin = 0;
  927. end = xbsd_dlabel.d_secperunit - 1;
  928. #endif
  929. snprintf(mesg, sizeof(mesg), _("First %s"), str_units(SINGULAR));
  930. begin = read_int(bsd_cround(begin), bsd_cround(begin), bsd_cround(end),
  931. 0, mesg);
  932. if (display_in_cyl_units)
  933. begin = (begin - 1) * xbsd_dlabel.d_secpercyl;
  934. snprintf(mesg, sizeof(mesg), _("Last %s or +size or +sizeM or +sizeK"),
  935. str_units(SINGULAR));
  936. end = read_int(bsd_cround (begin), bsd_cround (end), bsd_cround (end),
  937. bsd_cround (begin), mesg);
  938. if (display_in_cyl_units)
  939. end = end * xbsd_dlabel.d_secpercyl - 1;
  940. xbsd_dlabel.d_partitions[i].p_size = end - begin + 1;
  941. xbsd_dlabel.d_partitions[i].p_offset = begin;
  942. xbsd_dlabel.d_partitions[i].p_fstype = BSD_FS_UNUSED;
  943. }
  944. static void
  945. xbsd_print_disklabel(int show_all)
  946. {
  947. struct xbsd_disklabel *lp = &xbsd_dlabel;
  948. struct xbsd_partition *pp;
  949. int i, j;
  950. if (show_all) {
  951. #if defined (__alpha__)
  952. printf("# %s:\n", disk_device);
  953. #else
  954. printf("# %s:\n", partname(disk_device, xbsd_part_index+1, 0));
  955. #endif
  956. if ((unsigned) lp->d_type < BSD_DKMAXTYPES)
  957. printf(_("type: %s\n"), xbsd_dktypenames[lp->d_type]);
  958. else
  959. printf(_("type: %d\n"), lp->d_type);
  960. printf(_("disk: %.*s\n"), (int) sizeof(lp->d_typename), lp->d_typename);
  961. printf(_("label: %.*s\n"), (int) sizeof(lp->d_packname), lp->d_packname);
  962. printf(_("flags:"));
  963. if (lp->d_flags & BSD_D_REMOVABLE)
  964. printf(_(" removable"));
  965. if (lp->d_flags & BSD_D_ECC)
  966. printf(_(" ecc"));
  967. if (lp->d_flags & BSD_D_BADSECT)
  968. printf(_(" badsect"));
  969. printf("\n");
  970. /* On various machines the fields of *lp are short/int/long */
  971. /* In order to avoid problems, we cast them all to long. */
  972. printf(_("bytes/sector: %ld\n"), (long) lp->d_secsize);
  973. printf(_("sectors/track: %ld\n"), (long) lp->d_nsectors);
  974. printf(_("tracks/cylinder: %ld\n"), (long) lp->d_ntracks);
  975. printf(_("sectors/cylinder: %ld\n"), (long) lp->d_secpercyl);
  976. printf(_("cylinders: %ld\n"), (long) lp->d_ncylinders);
  977. printf(_("rpm: %d\n"), lp->d_rpm);
  978. printf(_("interleave: %d\n"), lp->d_interleave);
  979. printf(_("trackskew: %d\n"), lp->d_trackskew);
  980. printf(_("cylinderskew: %d\n"), lp->d_cylskew);
  981. printf(_("headswitch: %ld\t\t# milliseconds\n"),
  982. (long) lp->d_headswitch);
  983. printf(_("track-to-track seek: %ld\t# milliseconds\n"),
  984. (long) lp->d_trkseek);
  985. printf(_("drivedata: "));
  986. for (i = NDDATA - 1; i >= 0; i--)
  987. if (lp->d_drivedata[i])
  988. break;
  989. if (i < 0)
  990. i = 0;
  991. for (j = 0; j <= i; j++)
  992. printf("%ld ", (long) lp->d_drivedata[j]);
  993. }
  994. printf(_("\n%d partitions:\n"), lp->d_npartitions);
  995. printf(_("# start end size fstype [fsize bsize cpg]\n"));
  996. pp = lp->d_partitions;
  997. for (i = 0; i < lp->d_npartitions; i++, pp++) {
  998. if (pp->p_size) {
  999. if (display_in_cyl_units && lp->d_secpercyl) {
  1000. printf(" %c: %8ld%c %8ld%c %8ld%c ",
  1001. 'a' + i,
  1002. (long) pp->p_offset / lp->d_secpercyl + 1,
  1003. (pp->p_offset % lp->d_secpercyl) ? '*' : ' ',
  1004. (long) (pp->p_offset + pp->p_size + lp->d_secpercyl - 1) / lp->d_secpercyl,
  1005. ((pp->p_offset + pp->p_size) % lp->d_secpercyl) ? '*' : ' ',
  1006. (long) pp->p_size / lp->d_secpercyl,
  1007. (pp->p_size % lp->d_secpercyl) ? '*' : ' '
  1008. );
  1009. } else {
  1010. printf(" %c: %8ld %8ld %8ld ",
  1011. 'a' + i,
  1012. (long) pp->p_offset,
  1013. (long) pp->p_offset + pp->p_size - 1,
  1014. (long) pp->p_size
  1015. );
  1016. }
  1017. if ((unsigned) pp->p_fstype < BSD_FSMAXTYPES)
  1018. printf("%8.8s", xbsd_fstypes[pp->p_fstype].name);
  1019. else
  1020. printf("%8x", pp->p_fstype);
  1021. switch (pp->p_fstype) {
  1022. case BSD_FS_UNUSED:
  1023. printf(" %5ld %5ld %5.5s ",
  1024. (long) pp->p_fsize, (long) pp->p_fsize * pp->p_frag, "");
  1025. break;
  1026. case BSD_FS_BSDFFS:
  1027. printf(" %5ld %5ld %5d ",
  1028. (long) pp->p_fsize, (long) pp->p_fsize * pp->p_frag, pp->p_cpg);
  1029. break;
  1030. default:
  1031. printf("%22.22s", "");
  1032. break;
  1033. }
  1034. printf("\n");
  1035. }
  1036. }
  1037. }
  1038. static void
  1039. xbsd_write_disklabel(void)
  1040. {
  1041. #if defined (__alpha__)
  1042. printf(_("Writing disklabel to %s.\n"), disk_device);
  1043. xbsd_writelabel(NULL, &xbsd_dlabel);
  1044. #else
  1045. printf(_("Writing disklabel to %s.\n"),
  1046. partname(disk_device, xbsd_part_index + 1, 0));
  1047. xbsd_writelabel(xbsd_part, &xbsd_dlabel);
  1048. #endif
  1049. reread_partition_table(0); /* no exit yet */
  1050. }
  1051. static int
  1052. xbsd_create_disklabel(void)
  1053. {
  1054. char c;
  1055. #if defined (__alpha__)
  1056. fprintf(stderr, _("%s contains no disklabel.\n"), disk_device);
  1057. #else
  1058. fprintf(stderr, _("%s contains no disklabel.\n"),
  1059. partname(disk_device, xbsd_part_index + 1, 0));
  1060. #endif
  1061. while (1) {
  1062. c = read_char(_("Do you want to create a disklabel? (y/n) "));
  1063. if (c == 'y' || c == 'Y') {
  1064. if (xbsd_initlabel(
  1065. #if defined (__alpha__) || defined (__powerpc__) || defined (__hppa__) || \
  1066. defined (__s390__) || defined (__s390x__)
  1067. NULL, &xbsd_dlabel
  1068. #else
  1069. xbsd_part, &xbsd_dlabel/* not used, xbsd_part_index*/
  1070. #endif
  1071. ) == 1) {
  1072. xbsd_print_disklabel (1);
  1073. return 1;
  1074. } else
  1075. return 0;
  1076. } else if (c == 'n')
  1077. return 0;
  1078. }
  1079. }
  1080. static int
  1081. edit_int(int def, char *mesg)
  1082. {
  1083. do {
  1084. fputs(mesg, stdout);
  1085. printf(" (%d): ", def);
  1086. if (!read_line())
  1087. return def;
  1088. }
  1089. while (!isdigit(*line_ptr)); /* FIXME: ?!! */
  1090. return atoi(line_ptr);
  1091. }
  1092. static void
  1093. xbsd_edit_disklabel(void)
  1094. {
  1095. struct xbsd_disklabel *d;
  1096. d = &xbsd_dlabel;
  1097. #if defined (__alpha__) || defined (__ia64__)
  1098. d->d_secsize = (u_long) edit_int((u_long) d->d_secsize ,_("bytes/sector"));
  1099. d->d_nsectors = (u_long) edit_int((u_long) d->d_nsectors ,_("sectors/track"));
  1100. d->d_ntracks = (u_long) edit_int((u_long) d->d_ntracks ,_("tracks/cylinder"));
  1101. d->d_ncylinders = (u_long) edit_int((u_long) d->d_ncylinders ,_("cylinders"));
  1102. #endif
  1103. /* d->d_secpercyl can be != d->d_nsectors * d->d_ntracks */
  1104. while (1) {
  1105. d->d_secpercyl = (u_long) edit_int((u_long) d->d_nsectors * d->d_ntracks,
  1106. _("sectors/cylinder"));
  1107. if (d->d_secpercyl <= d->d_nsectors * d->d_ntracks)
  1108. break;
  1109. printf(_("Must be <= sectors/track * tracks/cylinder (default).\n"));
  1110. }
  1111. d->d_rpm = (u_short) edit_int((u_short) d->d_rpm ,_("rpm"));
  1112. d->d_interleave = (u_short) edit_int((u_short) d->d_interleave,_("interleave"));
  1113. d->d_trackskew = (u_short) edit_int((u_short) d->d_trackskew ,_("trackskew"));
  1114. d->d_cylskew = (u_short) edit_int((u_short) d->d_cylskew ,_("cylinderskew"));
  1115. d->d_headswitch = (u_long) edit_int((u_long) d->d_headswitch ,_("headswitch"));
  1116. d->d_trkseek = (u_long) edit_int((u_long) d->d_trkseek ,_("track-to-track seek"));
  1117. d->d_secperunit = d->d_secpercyl * d->d_ncylinders;
  1118. }
  1119. static int
  1120. xbsd_get_bootstrap (char *path, void *ptr, int size)
  1121. {
  1122. int fdb;
  1123. if ((fdb = open (path, O_RDONLY)) < 0) {
  1124. perror(path);
  1125. return 0;
  1126. }
  1127. if (read(fdb, ptr, size) < 0) {
  1128. perror(path);
  1129. close(fdb);
  1130. return 0;
  1131. }
  1132. printf(" ... %s\n", path);
  1133. close(fdb);
  1134. return 1;
  1135. }
  1136. static void
  1137. sync_disks(void)
  1138. {
  1139. printf(_("\nSyncing disks.\n"));
  1140. sync();
  1141. sleep(4); /* What? */
  1142. }
  1143. static void
  1144. xbsd_write_bootstrap(void)
  1145. {
  1146. char *bootdir = BSD_LINUX_BOOTDIR;
  1147. char path[MAXPATHLEN];
  1148. char *dkbasename;
  1149. struct xbsd_disklabel dl;
  1150. char *d, *p, *e;
  1151. int sector;
  1152. if (xbsd_dlabel.d_type == BSD_DTYPE_SCSI)
  1153. dkbasename = "sd";
  1154. else
  1155. dkbasename = "wd";
  1156. printf(_("Bootstrap: %sboot -> boot%s (%s): "),
  1157. dkbasename, dkbasename, dkbasename);
  1158. if (read_line()) {
  1159. line_ptr[strlen(line_ptr)-1] = '\0';
  1160. dkbasename = line_ptr;
  1161. }
  1162. snprintf(path, sizeof(path), "%s/%sboot", bootdir, dkbasename);
  1163. if (!xbsd_get_bootstrap(path, disklabelbuffer, (int) xbsd_dlabel.d_secsize))
  1164. return;
  1165. /* We need a backup of the disklabel (xbsd_dlabel might have changed). */
  1166. d = &disklabelbuffer[BSD_LABELSECTOR * SECTOR_SIZE];
  1167. bcopy(d, &dl, sizeof(struct xbsd_disklabel));
  1168. /* The disklabel will be overwritten by 0's from bootxx anyway */
  1169. memset(d, 0, sizeof(struct xbsd_disklabel));
  1170. snprintf(path, sizeof(path), "%s/boot%s", bootdir, dkbasename);
  1171. if (!xbsd_get_bootstrap (path, &disklabelbuffer[xbsd_dlabel.d_secsize],
  1172. (int) xbsd_dlabel.d_bbsize - xbsd_dlabel.d_secsize))
  1173. return;
  1174. e = d + sizeof(struct xbsd_disklabel);
  1175. for (p = d; p < e; p++)
  1176. if (*p) {
  1177. fprintf(stderr, _("Bootstrap overlaps with disk label!\n"));
  1178. exit(EXIT_FAILURE);
  1179. }
  1180. bcopy(&dl, d, sizeof(struct xbsd_disklabel));
  1181. #if defined (__powerpc__) || defined (__hppa__)
  1182. sector = 0;
  1183. #elif defined (__alpha__)
  1184. sector = 0;
  1185. alpha_bootblock_checksum(disklabelbuffer);
  1186. #else
  1187. sector = get_start_sect(xbsd_part);
  1188. #endif
  1189. if (lseek(fd, sector * SECTOR_SIZE, SEEK_SET) == -1)
  1190. fdisk_fatal(unable_to_seek);
  1191. if (BSD_BBSIZE != write(fd, disklabelbuffer, BSD_BBSIZE))
  1192. fdisk_fatal(unable_to_write);
  1193. #if defined (__alpha__)
  1194. printf(_("Bootstrap installed on %s.\n"), disk_device);
  1195. #else
  1196. printf(_("Bootstrap installed on %s.\n"),
  1197. partname (disk_device, xbsd_part_index+1, 0));
  1198. #endif
  1199. sync_disks();
  1200. }
  1201. static void
  1202. xbsd_change_fstype(void)
  1203. {
  1204. int i;
  1205. i = xbsd_get_part_index(xbsd_dlabel.d_npartitions);
  1206. xbsd_dlabel.d_partitions[i].p_fstype = read_hex(xbsd_fstypes);
  1207. }
  1208. static int
  1209. xbsd_get_part_index(int max)
  1210. {
  1211. char prompt[256];
  1212. char l;
  1213. snprintf(prompt, sizeof(prompt), _("Partition (a-%c): "), 'a' + max - 1);
  1214. do
  1215. l = tolower(read_char(prompt));
  1216. while (l < 'a' || l > 'a' + max - 1);
  1217. return l - 'a';
  1218. }
  1219. static int
  1220. xbsd_check_new_partition(int *i)
  1221. {
  1222. /* room for more? various BSD flavours have different maxima */
  1223. if (xbsd_dlabel.d_npartitions == BSD_MAXPARTITIONS) {
  1224. int t;
  1225. for (t = 0; t < BSD_MAXPARTITIONS; t++)
  1226. if (xbsd_dlabel.d_partitions[t].p_size == 0)
  1227. break;
  1228. if (t == BSD_MAXPARTITIONS) {
  1229. fprintf(stderr, _("The maximum number of partitions "
  1230. "has been created\n"));
  1231. return 0;
  1232. }
  1233. }
  1234. *i = xbsd_get_part_index (BSD_MAXPARTITIONS);
  1235. if (*i >= xbsd_dlabel.d_npartitions)
  1236. xbsd_dlabel.d_npartitions = (*i) + 1;
  1237. if (xbsd_dlabel.d_partitions[*i].p_size != 0) {
  1238. fprintf(stderr, _("This partition already exists.\n"));
  1239. return 0;
  1240. }
  1241. return 1;
  1242. }
  1243. static void
  1244. xbsd_list_types(void)
  1245. {
  1246. list_types(xbsd_fstypes);
  1247. }
  1248. static u_short
  1249. xbsd_dkcksum(struct xbsd_disklabel *lp)
  1250. {
  1251. u_short *start, *end;
  1252. u_short sum = 0;
  1253. start = (u_short *) lp;
  1254. end = (u_short *) &lp->d_partitions[lp->d_npartitions];
  1255. while (start < end)
  1256. sum ^= *start++;
  1257. return sum;
  1258. }
  1259. static int
  1260. xbsd_initlabel(struct partition *p, struct xbsd_disklabel *d)
  1261. {
  1262. struct xbsd_partition *pp;
  1263. get_geometry();
  1264. memset(d, 0, sizeof(struct xbsd_disklabel));
  1265. d->d_magic = BSD_DISKMAGIC;
  1266. if (strncmp(disk_device, "/dev/sd", 7) == 0)
  1267. d->d_type = BSD_DTYPE_SCSI;
  1268. else
  1269. d->d_type = BSD_DTYPE_ST506;
  1270. #if 0 /* not used (at least not written to disk) by NetBSD/i386 1.0 */
  1271. d->d_subtype = BSD_DSTYPE_INDOSPART & pindex;
  1272. #endif
  1273. #if !defined (__alpha__)
  1274. d->d_flags = BSD_D_DOSPART;
  1275. #else
  1276. d->d_flags = 0;
  1277. #endif
  1278. d->d_secsize = SECTOR_SIZE; /* bytes/sector */
  1279. d->d_nsectors = sectors; /* sectors/track */
  1280. d->d_ntracks = heads; /* tracks/cylinder (heads) */
  1281. d->d_ncylinders = cylinders;
  1282. d->d_secpercyl = sectors * heads;/* sectors/cylinder */
  1283. if (d->d_secpercyl == 0)
  1284. d->d_secpercyl = 1; /* avoid segfaults */
  1285. d->d_secperunit = d->d_secpercyl * d->d_ncylinders;
  1286. d->d_rpm = 3600;
  1287. d->d_interleave = 1;
  1288. d->d_trackskew = 0;
  1289. d->d_cylskew = 0;
  1290. d->d_headswitch = 0;
  1291. d->d_trkseek = 0;
  1292. d->d_magic2 = BSD_DISKMAGIC;
  1293. d->d_bbsize = BSD_BBSIZE;
  1294. d->d_sbsize = BSD_SBSIZE;
  1295. #if !defined (__alpha__)
  1296. d->d_npartitions = 4;
  1297. pp = &d->d_partitions[2]; /* Partition C should be
  1298. the NetBSD partition */
  1299. pp->p_offset = get_start_sect(p);
  1300. pp->p_size = get_nr_sects(p);
  1301. pp->p_fstype = BSD_FS_UNUSED;
  1302. pp = &d->d_partitions[3]; /* Partition D should be
  1303. the whole disk */
  1304. pp->p_offset = 0;
  1305. pp->p_size = d->d_secperunit;
  1306. pp->p_fstype = BSD_FS_UNUSED;
  1307. #elif defined (__alpha__)
  1308. d->d_npartitions = 3;
  1309. pp = &d->d_partitions[2]; /* Partition C should be
  1310. the whole disk */
  1311. pp->p_offset = 0;
  1312. pp->p_size = d->d_secperunit;
  1313. pp->p_fstype = BSD_FS_UNUSED;
  1314. #endif
  1315. return 1;
  1316. }
  1317. /*
  1318. * Read a xbsd_disklabel from sector 0 or from the starting sector of p.
  1319. * If it has the right magic, return 1.
  1320. */
  1321. static int
  1322. xbsd_readlabel (struct partition *p, struct xbsd_disklabel *d)
  1323. {
  1324. int t, sector;
  1325. /* p is used only to get the starting sector */
  1326. #if !defined (__alpha__)
  1327. sector = (p ? get_start_sect(p) : 0);
  1328. #elif defined (__alpha__)
  1329. sector = 0;
  1330. #endif
  1331. if (lseek(fd, sector * SECTOR_SIZE, SEEK_SET) == -1)
  1332. fdisk_fatal(unable_to_seek);
  1333. if (BSD_BBSIZE != read(fd, disklabelbuffer, BSD_BBSIZE))
  1334. fdisk_fatal(unable_to_read);
  1335. bcopy(&disklabelbuffer[BSD_LABELSECTOR * SECTOR_SIZE + BSD_LABELOFFSET],
  1336. d, sizeof(struct xbsd_disklabel));
  1337. if (d->d_magic != BSD_DISKMAGIC || d->d_magic2 != BSD_DISKMAGIC)
  1338. return 0;
  1339. for (t = d->d_npartitions; t < BSD_MAXPARTITIONS; t++) {
  1340. d->d_partitions[t].p_size = 0;
  1341. d->d_partitions[t].p_offset = 0;
  1342. d->d_partitions[t].p_fstype = BSD_FS_UNUSED;
  1343. }
  1344. if (d->d_npartitions > BSD_MAXPARTITIONS)
  1345. fprintf(stderr, _("Warning: too many partitions "
  1346. "(%d, maximum is %d).\n"),
  1347. d->d_npartitions, BSD_MAXPARTITIONS);
  1348. return 1;
  1349. }
  1350. static int
  1351. xbsd_writelabel (struct partition *p, struct xbsd_disklabel *d)
  1352. {
  1353. unsigned int sector;
  1354. #if !defined (__alpha__) && !defined (__powerpc__) && !defined (__hppa__)
  1355. sector = get_start_sect(p) + BSD_LABELSECTOR;
  1356. #else
  1357. sector = BSD_LABELSECTOR;
  1358. #endif
  1359. d->d_checksum = 0;
  1360. d->d_checksum = xbsd_dkcksum (d);
  1361. /* This is necessary if we want to write the bootstrap later,
  1362. otherwise we'd write the old disklabel with the bootstrap.
  1363. */
  1364. bcopy(d, &disklabelbuffer[BSD_LABELSECTOR * SECTOR_SIZE + BSD_LABELOFFSET],
  1365. sizeof(struct xbsd_disklabel));
  1366. #if defined (__alpha__) && BSD_LABELSECTOR == 0
  1367. alpha_bootblock_checksum (disklabelbuffer);
  1368. if (lseek(fd, 0, SEEK_SET) == -1)
  1369. fdisk_fatal(unable_to_seek);
  1370. if (BSD_BBSIZE != write(fd, disklabelbuffer, BSD_BBSIZE))
  1371. fdisk_fatal(unable_to_write);
  1372. #else
  1373. if (lseek(fd, sector * SECTOR_SIZE + BSD_LABELOFFSET, SEEK_SET) == -1)
  1374. fdisk_fatal(unable_to_seek);
  1375. if (sizeof(struct xbsd_disklabel) != write(fd, d, sizeof(struct xbsd_disklabel)))
  1376. fdisk_fatal(unable_to_write);
  1377. #endif
  1378. sync_disks();
  1379. return 1;
  1380. }
  1381. #if !defined (__alpha__)
  1382. static int
  1383. xbsd_translate_fstype(int linux_type)
  1384. {
  1385. switch (linux_type) {
  1386. case 0x01: /* DOS 12-bit FAT */
  1387. case 0x04: /* DOS 16-bit <32M */
  1388. case 0x06: /* DOS 16-bit >=32M */
  1389. case 0xe1: /* DOS access */
  1390. case 0xe3: /* DOS R/O */
  1391. case 0xf2: /* DOS secondary */
  1392. return BSD_FS_MSDOS;
  1393. case 0x07: /* OS/2 HPFS */
  1394. return BSD_FS_HPFS;
  1395. default:
  1396. return BSD_FS_OTHER;
  1397. }
  1398. }
  1399. static void
  1400. xbsd_link_part(void)
  1401. {
  1402. int k, i;
  1403. struct partition *p;
  1404. k = get_partition(1, partitions);
  1405. if (!xbsd_check_new_partition(&i))
  1406. return;
  1407. p = get_part_table(k);
  1408. xbsd_dlabel.d_partitions[i].p_size = get_nr_sects(p);
  1409. xbsd_dlabel.d_partitions[i].p_offset = get_start_sect(p);
  1410. xbsd_dlabel.d_partitions[i].p_fstype = xbsd_translate_fstype(p->sys_ind);
  1411. }
  1412. #endif
  1413. #if defined (__alpha__)
  1414. #if !defined(__GLIBC__)
  1415. typedef unsigned long long uint64_t;
  1416. #endif
  1417. static void
  1418. alpha_bootblock_checksum(char *boot)
  1419. {
  1420. uint64_t *dp, sum;
  1421. int i;
  1422. dp = (uint64_t *)boot;
  1423. sum = 0;
  1424. for (i = 0; i < 63; i++)
  1425. sum += dp[i];
  1426. dp[63] = sum;
  1427. }
  1428. #endif /* __alpha__ */
  1429. #endif /* OSF_LABEL */
  1430. #if defined(CONFIG_FEATURE_SGI_LABEL) || defined(CONFIG_FEATURE_SUN_LABEL)
  1431. static inline unsigned short
  1432. __swap16(unsigned short x)
  1433. {
  1434. return (((uint16_t)(x) & 0xFF) << 8) | (((uint16_t)(x) & 0xFF00) >> 8);
  1435. }
  1436. static inline uint32_t
  1437. __swap32(uint32_t x)
  1438. {
  1439. return (((x & 0xFF) << 24) |
  1440. ((x & 0xFF00) << 8) |
  1441. ((x & 0xFF0000) >> 8) |
  1442. ((x & 0xFF000000) >> 24));
  1443. }
  1444. #endif
  1445. #ifdef CONFIG_FEATURE_SGI_LABEL
  1446. /*
  1447. *
  1448. * fdisksgilabel.c
  1449. *
  1450. * Copyright (C) Andreas Neuper, Sep 1998.
  1451. * This file may be modified and redistributed under
  1452. * the terms of the GNU Public License.
  1453. *
  1454. * Sat Mar 20 EST 1999 Arnaldo Carvalho de Melo <acme@conectiva.com.br>
  1455. * Internationalization
  1456. */
  1457. static int sgi_other_endian;
  1458. static int debug;
  1459. static short sgi_volumes = 1;
  1460. /*
  1461. * only dealing with free blocks here
  1462. */
  1463. typedef struct {
  1464. unsigned int first;
  1465. unsigned int last;
  1466. } freeblocks;
  1467. static freeblocks freelist[17]; /* 16 partitions can produce 17 vacant slots */
  1468. static void
  1469. setfreelist(int i, unsigned int f, unsigned int l)
  1470. {
  1471. freelist[i].first = f;
  1472. freelist[i].last = l;
  1473. }
  1474. static void
  1475. add2freelist(unsigned int f, unsigned int l)
  1476. {
  1477. int i;
  1478. for (i = 0; i < 17 ; i++)
  1479. if (freelist[i].last == 0)
  1480. break;
  1481. setfreelist(i, f, l);
  1482. }
  1483. static void
  1484. clearfreelist(void)
  1485. {
  1486. int i;
  1487. for (i = 0; i < 17 ; i++)
  1488. setfreelist(i, 0, 0);
  1489. }
  1490. static unsigned int
  1491. isinfreelist(unsigned int b)
  1492. {
  1493. int i;
  1494. for (i = 0; i < 17 ; i++)
  1495. if (freelist[i].first <= b && freelist[i].last >= b)
  1496. return freelist[i].last;
  1497. return 0;
  1498. }
  1499. /* return last vacant block of this stride (never 0). */
  1500. /* the '>=' is not quite correct, but simplifies the code */
  1501. /*
  1502. * end of free blocks section
  1503. */
  1504. static const struct systypes sgi_sys_types[] = {
  1505. /* SGI_VOLHDR */ { "\x00" "SGI volhdr" },
  1506. /* 0x01 */ { "\x01" "SGI trkrepl" },
  1507. /* 0x02 */ { "\x02" "SGI secrepl" },
  1508. /* SGI_SWAP */ { "\x03" "SGI raw" },
  1509. /* 0x04 */ { "\x04" "SGI bsd" },
  1510. /* 0x05 */ { "\x05" "SGI sysv" },
  1511. /* ENTIRE_DISK */ { "\x06" "SGI volume" },
  1512. /* SGI_EFS */ { "\x07" "SGI efs" },
  1513. /* 0x08 */ { "\x08" "SGI lvol" },
  1514. /* 0x09 */ { "\x09" "SGI rlvol" },
  1515. /* SGI_XFS */ { "\x0a" "SGI xfs" },
  1516. /* SGI_XFSLOG */ { "\x0b" "SGI xfslog" },
  1517. /* SGI_XLV */ { "\x0c" "SGI xlv" },
  1518. /* SGI_XVM */ { "\x0d" "SGI xvm" },
  1519. /* LINUX_SWAP */ { "\x82" "Linux swap" },
  1520. /* LINUX_NATIVE */ { "\x83" "Linux native" },
  1521. /* LINUX_LVM */ { "\x8d" "Linux LVM" },
  1522. /* LINUX_RAID */ { "\xfd" "Linux RAID" },
  1523. { NULL }
  1524. };
  1525. static int
  1526. sgi_get_nsect(void)
  1527. {
  1528. return SGI_SSWAP16(sgilabel->devparam.nsect);
  1529. }
  1530. static int
  1531. sgi_get_ntrks(void)
  1532. {
  1533. return SGI_SSWAP16(sgilabel->devparam.ntrks);
  1534. }
  1535. static unsigned int
  1536. two_s_complement_32bit_sum(unsigned int* base, int size /* in bytes */)
  1537. {
  1538. int i = 0;
  1539. unsigned int sum = 0;
  1540. size /= sizeof(unsigned int);
  1541. for (i = 0; i < size; i++)
  1542. sum -= SGI_SSWAP32(base[i]);
  1543. return sum;
  1544. }
  1545. static int
  1546. check_sgi_label(void)
  1547. {
  1548. if (sizeof(sgilabel) > 512) {
  1549. fprintf(stderr,
  1550. _("According to MIPS Computer Systems, Inc the "
  1551. "Label must not contain more than 512 bytes\n"));
  1552. exit(1);
  1553. }
  1554. if (sgilabel->magic != SGI_LABEL_MAGIC
  1555. && sgilabel->magic != SGI_LABEL_MAGIC_SWAPPED) {
  1556. current_label_type = label_dos;
  1557. return 0;
  1558. }
  1559. sgi_other_endian = (sgilabel->magic == SGI_LABEL_MAGIC_SWAPPED);
  1560. /*
  1561. * test for correct checksum
  1562. */
  1563. if (two_s_complement_32bit_sum((unsigned int*)sgilabel,
  1564. sizeof(*sgilabel))) {
  1565. fprintf(stderr,
  1566. _("Detected sgi disklabel with wrong checksum.\n"));
  1567. }
  1568. update_units();
  1569. current_label_type = label_sgi;
  1570. partitions = 16;
  1571. sgi_volumes = 15;
  1572. return 1;
  1573. }
  1574. static unsigned int
  1575. sgi_get_start_sector(int i)
  1576. {
  1577. return SGI_SSWAP32(sgilabel->partitions[i].start_sector);
  1578. }
  1579. static unsigned int
  1580. sgi_get_num_sectors(int i)
  1581. {
  1582. return SGI_SSWAP32(sgilabel->partitions[i].num_sectors);
  1583. }
  1584. static int
  1585. sgi_get_sysid(int i)
  1586. {
  1587. return SGI_SSWAP32(sgilabel->partitions[i].id);
  1588. }
  1589. static int
  1590. sgi_get_bootpartition(void)
  1591. {
  1592. return SGI_SSWAP16(sgilabel->boot_part);
  1593. }
  1594. static int
  1595. sgi_get_swappartition(void)
  1596. {
  1597. return SGI_SSWAP16(sgilabel->swap_part);
  1598. }
  1599. static void
  1600. sgi_list_table(int xtra)
  1601. {
  1602. int i, w, wd;
  1603. int kpi = 0; /* kernel partition ID */
  1604. if(xtra) {
  1605. printf(_("\nDisk %s (SGI disk label): %d heads, %d sectors\n"
  1606. "%d cylinders, %d physical cylinders\n"
  1607. "%d extra sects/cyl, interleave %d:1\n"
  1608. "%s\n"
  1609. "Units = %s of %d * 512 bytes\n\n"),
  1610. disk_device, heads, sectors, cylinders,
  1611. SGI_SSWAP16(sgiparam.pcylcount),
  1612. SGI_SSWAP16(sgiparam.sparecyl),
  1613. SGI_SSWAP16(sgiparam.ilfact),
  1614. (char *)sgilabel,
  1615. str_units(PLURAL), units_per_sector);
  1616. } else {
  1617. printf( _("\nDisk %s (SGI disk label): "
  1618. "%d heads, %d sectors, %d cylinders\n"
  1619. "Units = %s of %d * 512 bytes\n\n"),
  1620. disk_device, heads, sectors, cylinders,
  1621. str_units(PLURAL), units_per_sector );
  1622. }
  1623. w = strlen(disk_device);
  1624. wd = strlen(_("Device"));
  1625. if (w < wd)
  1626. w = wd;
  1627. printf(_("----- partitions -----\n"
  1628. "Pt# %*s Info Start End Sectors Id System\n"),
  1629. w + 2, _("Device"));
  1630. for (i = 0 ; i < partitions; i++) {
  1631. if( sgi_get_num_sectors(i) || debug ) {
  1632. uint32_t start = sgi_get_start_sector(i);
  1633. uint32_t len = sgi_get_num_sectors(i);
  1634. kpi++; /* only count nonempty partitions */
  1635. printf(
  1636. "%2d: %s %4s %9ld %9ld %9ld %2x %s\n",
  1637. /* fdisk part number */ i+1,
  1638. /* device */ partname(disk_device, kpi, w+3),
  1639. /* flags */ (sgi_get_swappartition() == i) ? "swap" :
  1640. /* flags */ (sgi_get_bootpartition() == i) ? "boot" : " ",
  1641. /* start */ (long) scround(start),
  1642. /* end */ (long) scround(start+len)-1,
  1643. /* no odd flag on end */(long) len,
  1644. /* type id */ sgi_get_sysid(i),
  1645. /* type name */ partition_type(sgi_get_sysid(i)));
  1646. }
  1647. }
  1648. printf(_("----- Bootinfo -----\nBootfile: %s\n"
  1649. "----- Directory Entries -----\n"),
  1650. sgilabel->boot_file);
  1651. for (i = 0 ; i < sgi_volumes; i++) {
  1652. if (sgilabel->directory[i].vol_file_size) {
  1653. uint32_t start = SGI_SSWAP32(sgilabel->directory[i].vol_file_start);
  1654. uint32_t len = SGI_SSWAP32(sgilabel->directory[i].vol_file_size);
  1655. unsigned char *name = sgilabel->directory[i].vol_file_name;
  1656. printf(_("%2d: %-10s sector%5u size%8u\n"),
  1657. i, (char*)name, (unsigned int) start, (unsigned int) len);
  1658. }
  1659. }
  1660. }
  1661. static void
  1662. sgi_set_bootpartition(int i)
  1663. {
  1664. sgilabel->boot_part = SGI_SSWAP16(((short)i));
  1665. }
  1666. static unsigned int
  1667. sgi_get_lastblock(void)
  1668. {
  1669. return heads * sectors * cylinders;
  1670. }
  1671. static void
  1672. sgi_set_swappartition(int i)
  1673. {
  1674. sgilabel->swap_part = SGI_SSWAP16(((short)i));
  1675. }
  1676. static int
  1677. sgi_check_bootfile(const char* aFile)
  1678. {
  1679. if (strlen(aFile) < 3) /* "/a\n" is minimum */ {
  1680. printf(_("\nInvalid Bootfile!\n"
  1681. "\tThe bootfile must be an absolute non-zero pathname,\n"
  1682. "\te.g. \"/unix\" or \"/unix.save\".\n"));
  1683. return 0;
  1684. } else {
  1685. if (strlen(aFile) > 16) {
  1686. printf(_("\n\tName of Bootfile too long: "
  1687. "16 bytes maximum.\n"));
  1688. return 0;
  1689. } else {
  1690. if (aFile[0] != '/') {
  1691. printf(_("\n\tBootfile must have a "
  1692. "fully qualified pathname.\n"));
  1693. return 0;
  1694. }
  1695. }
  1696. }
  1697. if (strncmp(aFile, (char*)sgilabel->boot_file, 16)) {
  1698. printf(_("\n\tBe aware, that the bootfile is not checked for existence.\n\t"
  1699. "SGI's default is \"/unix\" and for backup \"/unix.save\".\n"));
  1700. /* filename is correct and did change */
  1701. return 1;
  1702. }
  1703. return 0; /* filename did not change */
  1704. }
  1705. static const char *
  1706. sgi_get_bootfile(void)
  1707. {
  1708. return (char*)sgilabel->boot_file;
  1709. }
  1710. static void
  1711. sgi_set_bootfile(const char* aFile)
  1712. {
  1713. int i = 0;
  1714. if (sgi_check_bootfile(aFile)) {
  1715. while (i < 16) {
  1716. if ((aFile[i] != '\n') /* in principle caught again by next line */
  1717. && (strlen(aFile) > i))
  1718. sgilabel->boot_file[i] = aFile[i];
  1719. else
  1720. sgilabel->boot_file[i] = 0;
  1721. i++;
  1722. }
  1723. printf(_("\n\tBootfile is changed to \"%s\".\n"), sgilabel->boot_file);
  1724. }
  1725. }
  1726. static void
  1727. create_sgiinfo(void)
  1728. {
  1729. /* I keep SGI's habit to write the sgilabel to the second block */
  1730. sgilabel->directory[0].vol_file_start = SGI_SSWAP32(2);
  1731. sgilabel->directory[0].vol_file_size = SGI_SSWAP32(sizeof(sgiinfo));
  1732. strncpy((char*)sgilabel->directory[0].vol_file_name, "sgilabel", 8);
  1733. }
  1734. static sgiinfo *fill_sgiinfo(void);
  1735. static void
  1736. sgi_write_table(void)
  1737. {
  1738. sgilabel->csum = 0;
  1739. sgilabel->csum = SGI_SSWAP32(two_s_complement_32bit_sum(
  1740. (unsigned int*)sgilabel, sizeof(*sgilabel)));
  1741. assert(two_s_complement_32bit_sum(
  1742. (unsigned int*)sgilabel, sizeof(*sgilabel)) == 0);
  1743. if (lseek(fd, 0, SEEK_SET) < 0)
  1744. fdisk_fatal(unable_to_seek);
  1745. if (write(fd, sgilabel, SECTOR_SIZE) != SECTOR_SIZE)
  1746. fdisk_fatal(unable_to_write);
  1747. if (!strncmp((char*)sgilabel->directory[0].vol_file_name, "sgilabel", 8)) {
  1748. /*
  1749. * keep this habit of first writing the "sgilabel".
  1750. * I never tested whether it works without (AN 981002).
  1751. */
  1752. sgiinfo *info = fill_sgiinfo();
  1753. int infostartblock = SGI_SSWAP32(sgilabel->directory[0].vol_file_start);
  1754. if (lseek(fd, infostartblock*SECTOR_SIZE, SEEK_SET) < 0)
  1755. fdisk_fatal(unable_to_seek);
  1756. if (write(fd, info, SECTOR_SIZE) != SECTOR_SIZE)
  1757. fdisk_fatal(unable_to_write);
  1758. free(info);
  1759. }
  1760. }
  1761. static int
  1762. compare_start(int *x, int *y)
  1763. {
  1764. /*
  1765. * sort according to start sectors
  1766. * and prefers largest partition:
  1767. * entry zero is entire disk entry
  1768. */
  1769. unsigned int i = *x;
  1770. unsigned int j = *y;
  1771. unsigned int a = sgi_get_start_sector(i);
  1772. unsigned int b = sgi_get_start_sector(j);
  1773. unsigned int c = sgi_get_num_sectors(i);
  1774. unsigned int d = sgi_get_num_sectors(j);
  1775. if (a == b)
  1776. return (d > c) ? 1 : (d == c) ? 0 : -1;
  1777. return (a > b) ? 1 : -1;
  1778. }
  1779. static int
  1780. verify_sgi(int verbose)
  1781. {
  1782. int Index[16]; /* list of valid partitions */
  1783. int sortcount = 0; /* number of used partitions, i.e. non-zero lengths */
  1784. int entire = 0, i = 0;
  1785. unsigned int start = 0;
  1786. long long gap = 0; /* count unused blocks */
  1787. unsigned int lastblock = sgi_get_lastblock();
  1788. clearfreelist();
  1789. for (i = 0; i < 16; i++) {
  1790. if (sgi_get_num_sectors(i) != 0) {
  1791. Index[sortcount++] = i;
  1792. if (sgi_get_sysid(i) == ENTIRE_DISK) {
  1793. if (entire++ == 1) {
  1794. if (verbose)
  1795. printf(_("More than one entire disk entry present.\n"));
  1796. }
  1797. }
  1798. }
  1799. }
  1800. if (sortcount == 0) {
  1801. if (verbose)
  1802. printf(_("No partitions defined\n"));
  1803. return (lastblock > 0) ? 1 : (lastblock == 0) ? 0 : -1;
  1804. }
  1805. qsort(Index, sortcount, sizeof(Index[0]), (void*)compare_start);
  1806. if (sgi_get_sysid(Index[0]) == ENTIRE_DISK) {
  1807. if ((Index[0] != 10) && verbose)
  1808. printf(_("IRIX likes when Partition 11 covers the entire disk.\n"));
  1809. if ((sgi_get_start_sector(Index[0]) != 0) && verbose)
  1810. printf(_("The entire disk partition should start "
  1811. "at block 0,\n"
  1812. "not at diskblock %d.\n"),
  1813. sgi_get_start_sector(Index[0]));
  1814. if (debug) /* I do not understand how some disks fulfil it */
  1815. if ((sgi_get_num_sectors(Index[0]) != lastblock) && verbose)
  1816. printf(_("The entire disk partition is only %d diskblock large,\n"
  1817. "but the disk is %d diskblocks long.\n"),
  1818. sgi_get_num_sectors(Index[0]), lastblock);
  1819. lastblock = sgi_get_num_sectors(Index[0]);
  1820. } else {
  1821. if (verbose)
  1822. printf(_("One Partition (#11) should cover the entire disk.\n"));
  1823. if (debug > 2)
  1824. printf("sysid=%d\tpartition=%d\n",
  1825. sgi_get_sysid(Index[0]), Index[0]+1);
  1826. }
  1827. for (i = 1, start = 0; i < sortcount; i++) {
  1828. int cylsize = sgi_get_nsect() * sgi_get_ntrks();
  1829. if ((sgi_get_start_sector(Index[i]) % cylsize) != 0) {
  1830. if (debug) /* I do not understand how some disks fulfil it */
  1831. if (verbose)
  1832. printf(_("Partition %d does not start on cylinder boundary.\n"),
  1833. Index[i]+1);
  1834. }
  1835. if (sgi_get_num_sectors(Index[i]) % cylsize != 0) {
  1836. if (debug) /* I do not understand how some disks fulfil it */
  1837. if (verbose)
  1838. printf(_("Partition %d does not end on cylinder boundary.\n"),
  1839. Index[i]+1);
  1840. }
  1841. /* We cannot handle several "entire disk" entries. */
  1842. if (sgi_get_sysid(Index[i]) == ENTIRE_DISK) continue;
  1843. if (start > sgi_get_start_sector(Index[i])) {
  1844. if (verbose)
  1845. printf(_("The Partition %d and %d overlap by %d sectors.\n"),
  1846. Index[i-1]+1, Index[i]+1,
  1847. start - sgi_get_start_sector(Index[i]));
  1848. if (gap > 0) gap = -gap;
  1849. if (gap == 0) gap = -1;
  1850. }
  1851. if (start < sgi_get_start_sector(Index[i])) {
  1852. if (verbose)
  1853. printf(_("Unused gap of %8u sectors - sectors %8u-%u\n"),
  1854. sgi_get_start_sector(Index[i]) - start,
  1855. start, sgi_get_start_sector(Index[i])-1);
  1856. gap += sgi_get_start_sector(Index[i]) - start;
  1857. add2freelist(start, sgi_get_start_sector(Index[i]));
  1858. }
  1859. start = sgi_get_start_sector(Index[i])
  1860. + sgi_get_num_sectors(Index[i]);
  1861. if (debug > 1) {
  1862. if (verbose)
  1863. printf("%2d:%12d\t%12d\t%12d\n", Index[i],
  1864. sgi_get_start_sector(Index[i]),
  1865. sgi_get_num_sectors(Index[i]),
  1866. sgi_get_sysid(Index[i]));
  1867. }
  1868. }
  1869. if (start < lastblock) {
  1870. if (verbose)
  1871. printf(_("Unused gap of %8u sectors - sectors %8u-%u\n"),
  1872. lastblock - start, start, lastblock-1);
  1873. gap += lastblock - start;
  1874. add2freelist(start, lastblock);
  1875. }
  1876. /*
  1877. * Done with arithmetics
  1878. * Go for details now
  1879. */
  1880. if (verbose) {
  1881. if (!sgi_get_num_sectors(sgi_get_bootpartition())) {
  1882. printf(_("\nThe boot partition does not exist.\n"));
  1883. }
  1884. if (!sgi_get_num_sectors(sgi_get_swappartition())) {
  1885. printf(_("\nThe swap partition does not exist.\n"));
  1886. } else {
  1887. if ((sgi_get_sysid(sgi_get_swappartition()) != SGI_SWAP)
  1888. && (sgi_get_sysid(sgi_get_swappartition()) != LINUX_SWAP))
  1889. printf(_("\nThe swap partition has no swap type.\n"));
  1890. }
  1891. if (sgi_check_bootfile("/unix"))
  1892. printf(_("\tYou have chosen an unusual boot file name.\n"));
  1893. }
  1894. return (gap > 0) ? 1 : (gap == 0) ? 0 : -1;
  1895. }
  1896. static int
  1897. sgi_gaps(void)
  1898. {
  1899. /*
  1900. * returned value is:
  1901. * = 0 : disk is properly filled to the rim
  1902. * < 0 : there is an overlap
  1903. * > 0 : there is still some vacant space
  1904. */
  1905. return verify_sgi(0);
  1906. }
  1907. static void
  1908. sgi_change_sysid(int i, int sys)
  1909. {
  1910. if( sgi_get_num_sectors(i) == 0 ) { /* caught already before, ... */
  1911. printf(_("Sorry You may change the Tag of non-empty partitions.\n"));
  1912. return;
  1913. }
  1914. if (((sys != ENTIRE_DISK ) && (sys != SGI_VOLHDR))
  1915. && (sgi_get_start_sector(i) < 1) ) {
  1916. read_chars(
  1917. _("It is highly recommended that the partition at offset 0\n"
  1918. "is of type \"SGI volhdr\", the IRIX system will rely on it to\n"
  1919. "retrieve from its directory standalone tools like sash and fx.\n"
  1920. "Only the \"SGI volume\" entire disk section may violate this.\n"
  1921. "Type YES if you are sure about tagging this partition differently.\n"));
  1922. if (strcmp(line_ptr, _("YES\n")))
  1923. return;
  1924. }
  1925. sgilabel->partitions[i].id = SGI_SSWAP32(sys);
  1926. }
  1927. /* returns partition index of first entry marked as entire disk */
  1928. static int
  1929. sgi_entire(void)
  1930. {
  1931. int i;
  1932. for (i = 0; i < 16; i++)
  1933. if (sgi_get_sysid(i) == SGI_VOLUME)
  1934. return i;
  1935. return -1;
  1936. }
  1937. static void
  1938. sgi_set_partition(int i, unsigned int start, unsigned int length, int sys)
  1939. {
  1940. sgilabel->partitions[i].id = SGI_SSWAP32(sys);
  1941. sgilabel->partitions[i].num_sectors = SGI_SSWAP32(length);
  1942. sgilabel->partitions[i].start_sector = SGI_SSWAP32(start);
  1943. set_changed(i);
  1944. if (sgi_gaps() < 0) /* rebuild freelist */
  1945. printf(_("Do You know, You got a partition overlap on the disk?\n"));
  1946. }
  1947. static void
  1948. sgi_set_entire(void)
  1949. {
  1950. int n;
  1951. for (n = 10; n < partitions; n++) {
  1952. if(!sgi_get_num_sectors(n) ) {
  1953. sgi_set_partition(n, 0, sgi_get_lastblock(), SGI_VOLUME);
  1954. break;
  1955. }
  1956. }
  1957. }
  1958. static void
  1959. sgi_set_volhdr(void)
  1960. {
  1961. int n;
  1962. for (n = 8; n < partitions; n++) {
  1963. if (!sgi_get_num_sectors(n)) {
  1964. /*
  1965. * 5 cylinders is an arbitrary value I like
  1966. * IRIX 5.3 stored files in the volume header
  1967. * (like sash, symmon, fx, ide) with ca. 3200
  1968. * sectors.
  1969. */
  1970. if (heads * sectors * 5 < sgi_get_lastblock())
  1971. sgi_set_partition(n, 0, heads * sectors * 5, SGI_VOLHDR);
  1972. break;
  1973. }
  1974. }
  1975. }
  1976. static void
  1977. sgi_delete_partition(int i)
  1978. {
  1979. sgi_set_partition(i, 0, 0, 0);
  1980. }
  1981. static void
  1982. sgi_add_partition(int n, int sys)
  1983. {
  1984. char mesg[256];
  1985. unsigned int first = 0, last = 0;
  1986. if (n == 10) {
  1987. sys = SGI_VOLUME;
  1988. } else if (n == 8) {
  1989. sys = 0;
  1990. }
  1991. if(sgi_get_num_sectors(n)) {
  1992. printf(_("Partition %d is already defined. Delete "
  1993. "it before re-adding it.\n"), n + 1);
  1994. return;
  1995. }
  1996. if ((sgi_entire() == -1) && (sys != SGI_VOLUME)) {
  1997. printf(_("Attempting to generate entire disk entry automatically.\n"));
  1998. sgi_set_entire();
  1999. sgi_set_volhdr();
  2000. }
  2001. if ((sgi_gaps() == 0) && (sys != SGI_VOLUME)) {
  2002. printf(_("The entire disk is already covered with partitions.\n"));
  2003. return;
  2004. }
  2005. if (sgi_gaps() < 0) {
  2006. printf(_("You got a partition overlap on the disk. Fix it first!\n"));
  2007. return;
  2008. }
  2009. snprintf(mesg, sizeof(mesg), _("First %s"), str_units(SINGULAR));
  2010. while (1) {
  2011. if(sys == SGI_VOLUME) {
  2012. last = sgi_get_lastblock();
  2013. first = read_int(0, 0, last-1, 0, mesg);
  2014. if (first != 0) {
  2015. printf(_("It is highly recommended that eleventh partition\n"
  2016. "covers the entire disk and is of type `SGI volume'\n"));
  2017. }
  2018. } else {
  2019. first = freelist[0].first;
  2020. last = freelist[0].last;
  2021. first = read_int(scround(first), scround(first), scround(last)-1,
  2022. 0, mesg);
  2023. }
  2024. if (display_in_cyl_units)
  2025. first *= units_per_sector;
  2026. else
  2027. first = first; /* align to cylinder if you know how ... */
  2028. if(!last )
  2029. last = isinfreelist(first);
  2030. if(last == 0) {
  2031. printf(_("You will get a partition overlap on the disk. "
  2032. "Fix it first!\n"));
  2033. } else
  2034. break;
  2035. }
  2036. snprintf(mesg, sizeof(mesg), _(" Last %s"), str_units(SINGULAR));
  2037. last = read_int(scround(first), scround(last)-1, scround(last)-1,
  2038. scround(first), mesg)+1;
  2039. if (display_in_cyl_units)
  2040. last *= units_per_sector;
  2041. else
  2042. last = last; /* align to cylinder if You know how ... */
  2043. if ( (sys == SGI_VOLUME) && (first != 0 || last != sgi_get_lastblock() ) )
  2044. printf(_("It is highly recommended that eleventh partition\n"
  2045. "covers the entire disk and is of type `SGI volume'\n"));
  2046. sgi_set_partition(n, first, last-first, sys);
  2047. }
  2048. #ifdef CONFIG_FEATURE_FDISK_ADVANCED
  2049. static void
  2050. create_sgilabel(void)
  2051. {
  2052. struct hd_geometry geometry;
  2053. struct {
  2054. unsigned int start;
  2055. unsigned int nsect;
  2056. int sysid;
  2057. } old[4];
  2058. int i = 0;
  2059. long longsectors; /* the number of sectors on the device */
  2060. int res; /* the result from the ioctl */
  2061. int sec_fac; /* the sector factor */
  2062. sec_fac = sector_size / 512; /* determine the sector factor */
  2063. fprintf( stderr,
  2064. _("Building a new SGI disklabel. Changes will remain in memory only,\n"
  2065. "until you decide to write them. After that, of course, the previous\n"
  2066. "content will be unrecoverably lost.\n\n"));
  2067. sgi_other_endian = (BYTE_ORDER == LITTLE_ENDIAN);
  2068. res = ioctl(fd, BLKGETSIZE, &longsectors);
  2069. if (!ioctl(fd, HDIO_GETGEO, &geometry)) {
  2070. heads = geometry.heads;
  2071. sectors = geometry.sectors;
  2072. if (res == 0) {
  2073. /* the get device size ioctl was successful */
  2074. cylinders = longsectors / (heads * sectors);
  2075. cylinders /= sec_fac;
  2076. } else {
  2077. /* otherwise print error and use truncated version */
  2078. cylinders = geometry.cylinders;
  2079. fprintf(stderr,
  2080. _("Warning: BLKGETSIZE ioctl failed on %s. "
  2081. "Using geometry cylinder value of %d.\n"
  2082. "This value may be truncated for devices"
  2083. " > 33.8 GB.\n"), disk_device, cylinders);
  2084. }
  2085. }
  2086. for (i = 0; i < 4; i++) {
  2087. old[i].sysid = 0;
  2088. if (valid_part_table_flag(MBRbuffer)) {
  2089. if(get_part_table(i)->sys_ind) {
  2090. old[i].sysid = get_part_table(i)->sys_ind;
  2091. old[i].start = get_start_sect(get_part_table(i));
  2092. old[i].nsect = get_nr_sects(get_part_table(i));
  2093. printf(_("Trying to keep parameters of partition %d.\n"), i);
  2094. if (debug)
  2095. printf(_("ID=%02x\tSTART=%d\tLENGTH=%d\n"),
  2096. old[i].sysid, old[i].start, old[i].nsect);
  2097. }
  2098. }
  2099. }
  2100. memset(MBRbuffer, 0, sizeof(MBRbuffer));
  2101. sgilabel->magic = SGI_SSWAP32(SGI_LABEL_MAGIC);
  2102. sgilabel->boot_part = SGI_SSWAP16(0);
  2103. sgilabel->swap_part = SGI_SSWAP16(1);
  2104. /* sizeof(sgilabel->boot_file) = 16 > 6 */
  2105. memset(sgilabel->boot_file, 0, 16);
  2106. strcpy((char*)sgilabel->boot_file, "/unix");
  2107. sgilabel->devparam.skew = (0);
  2108. sgilabel->devparam.gap1 = (0);
  2109. sgilabel->devparam.gap2 = (0);
  2110. sgilabel->devparam.sparecyl = (0);
  2111. sgilabel->devparam.pcylcount = SGI_SSWAP16(geometry.cylinders);
  2112. sgilabel->devparam.head_vol0 = SGI_SSWAP16(0);
  2113. sgilabel->devparam.ntrks = SGI_SSWAP16(geometry.heads);
  2114. /* tracks/cylinder (heads) */
  2115. sgilabel->devparam.cmd_tag_queue_depth = (0);
  2116. sgilabel->devparam.unused0 = (0);
  2117. sgilabel->devparam.unused1 = SGI_SSWAP16(0);
  2118. sgilabel->devparam.nsect = SGI_SSWAP16(geometry.sectors);
  2119. /* sectors/track */
  2120. sgilabel->devparam.bytes = SGI_SSWAP16(512);
  2121. sgilabel->devparam.ilfact = SGI_SSWAP16(1);
  2122. sgilabel->devparam.flags = SGI_SSWAP32(TRACK_FWD|
  2123. IGNORE_ERRORS|RESEEK);
  2124. sgilabel->devparam.datarate = SGI_SSWAP32(0);
  2125. sgilabel->devparam.retries_on_error = SGI_SSWAP32(1);
  2126. sgilabel->devparam.ms_per_word = SGI_SSWAP32(0);
  2127. sgilabel->devparam.xylogics_gap1 = SGI_SSWAP16(0);
  2128. sgilabel->devparam.xylogics_syncdelay = SGI_SSWAP16(0);
  2129. sgilabel->devparam.xylogics_readdelay = SGI_SSWAP16(0);
  2130. sgilabel->devparam.xylogics_gap2 = SGI_SSWAP16(0);
  2131. sgilabel->devparam.xylogics_readgate = SGI_SSWAP16(0);
  2132. sgilabel->devparam.xylogics_writecont = SGI_SSWAP16(0);
  2133. memset( &(sgilabel->directory), 0, sizeof(struct volume_directory)*15 );
  2134. memset( &(sgilabel->partitions), 0, sizeof(struct sgi_partition)*16 );
  2135. current_label_type = label_sgi;
  2136. partitions = 16;
  2137. sgi_volumes = 15;
  2138. sgi_set_entire();
  2139. sgi_set_volhdr();
  2140. for (i = 0; i < 4; i++) {
  2141. if(old[i].sysid) {
  2142. sgi_set_partition(i, old[i].start, old[i].nsect, old[i].sysid);
  2143. }
  2144. }
  2145. }
  2146. static void
  2147. sgi_set_xcyl(void)
  2148. {
  2149. /* do nothing in the beginning */
  2150. }
  2151. #endif /* CONFIG_FEATURE_FDISK_ADVANCED */
  2152. /* _____________________________________________________________
  2153. */
  2154. static sgiinfo *
  2155. fill_sgiinfo(void)
  2156. {
  2157. sgiinfo *info = calloc(1, sizeof(sgiinfo));
  2158. info->magic = SGI_SSWAP32(SGI_INFO_MAGIC);
  2159. info->b1 = SGI_SSWAP32(-1);
  2160. info->b2 = SGI_SSWAP16(-1);
  2161. info->b3 = SGI_SSWAP16(1);
  2162. /* You may want to replace this string !!!!!!! */
  2163. strcpy( (char*)info->scsi_string, "IBM OEM 0662S12 3 30" );
  2164. strcpy( (char*)info->serial, "0000" );
  2165. info->check1816 = SGI_SSWAP16(18*256 +16 );
  2166. strcpy( (char*)info->installer, "Sfx version 5.3, Oct 18, 1994" );
  2167. return info;
  2168. }
  2169. #endif /* SGI_LABEL */
  2170. #ifdef CONFIG_FEATURE_SUN_LABEL
  2171. /*
  2172. * fdisksunlabel.c
  2173. *
  2174. * I think this is mostly, or entirely, due to
  2175. * Jakub Jelinek (jj@sunsite.mff.cuni.cz), July 1996
  2176. *
  2177. * Merged with fdisk for other architectures, aeb, June 1998.
  2178. *
  2179. * Sat Mar 20 EST 1999 Arnaldo Carvalho de Melo <acme@conectiva.com.br>
  2180. * Internationalization
  2181. */
  2182. static int sun_other_endian;
  2183. static int scsi_disk;
  2184. static int floppy;
  2185. #ifndef IDE0_MAJOR
  2186. #define IDE0_MAJOR 3
  2187. #endif
  2188. #ifndef IDE1_MAJOR
  2189. #define IDE1_MAJOR 22
  2190. #endif
  2191. static void
  2192. guess_device_type(void)
  2193. {
  2194. struct stat bootstat;
  2195. if (fstat(fd, &bootstat) < 0) {
  2196. scsi_disk = 0;
  2197. floppy = 0;
  2198. } else if (S_ISBLK(bootstat.st_mode)
  2199. && (major(bootstat.st_rdev) == IDE0_MAJOR ||
  2200. major(bootstat.st_rdev) == IDE1_MAJOR)) {
  2201. scsi_disk = 0;
  2202. floppy = 0;
  2203. } else if (S_ISBLK(bootstat.st_mode)
  2204. && major(bootstat.st_rdev) == FLOPPY_MAJOR) {
  2205. scsi_disk = 0;
  2206. floppy = 1;
  2207. } else {
  2208. scsi_disk = 1;
  2209. floppy = 0;
  2210. }
  2211. }
  2212. static const struct systypes sun_sys_types[] = {
  2213. { "\x00" "Empty" }, /* 0 */
  2214. { "\x01" "Boot" }, /* 1 */
  2215. { "\x02" "SunOS root" }, /* 2 */
  2216. { "\x03" "SunOS swap" }, /* SUNOS_SWAP */
  2217. { "\x04" "SunOS usr" }, /* 4 */
  2218. { "\x05" "Whole disk" }, /* WHOLE_DISK */
  2219. { "\x06" "SunOS stand" }, /* 6 */
  2220. { "\x07" "SunOS var" }, /* 7 */
  2221. { "\x08" "SunOS home" }, /* 8 */
  2222. { "\x82" "Linux swap" }, /* LINUX_SWAP */
  2223. { "\x83" "Linux native" }, /* LINUX_NATIVE */
  2224. { "\x8e" "Linux LVM" }, /* 0x8e */
  2225. /* New (2.2.x) raid partition with autodetect using persistent superblock */
  2226. { "\xfd" "Linux raid autodetect" }, /* 0xfd */
  2227. { NULL }
  2228. };
  2229. static void
  2230. set_sun_partition(int i, uint start, uint stop, int sysid)
  2231. {
  2232. sunlabel->infos[i].id = sysid;
  2233. sunlabel->partitions[i].start_cylinder =
  2234. SUN_SSWAP32(start / (heads * sectors));
  2235. sunlabel->partitions[i].num_sectors =
  2236. SUN_SSWAP32(stop - start);
  2237. set_changed(i);
  2238. }
  2239. static int
  2240. check_sun_label(void)
  2241. {
  2242. unsigned short *ush;
  2243. int csum;
  2244. if (sunlabel->magic != SUN_LABEL_MAGIC
  2245. && sunlabel->magic != SUN_LABEL_MAGIC_SWAPPED) {
  2246. current_label_type = label_dos;
  2247. sun_other_endian = 0;
  2248. return 0;
  2249. }
  2250. sun_other_endian = (sunlabel->magic == SUN_LABEL_MAGIC_SWAPPED);
  2251. ush = ((unsigned short *) (sunlabel + 1)) - 1;
  2252. for (csum = 0; ush >= (unsigned short *)sunlabel;) csum ^= *ush--;
  2253. if (csum) {
  2254. fprintf(stderr,_("Detected sun disklabel with wrong checksum.\n"
  2255. "Probably you'll have to set all the values,\n"
  2256. "e.g. heads, sectors, cylinders and partitions\n"
  2257. "or force a fresh label (s command in main menu)\n"));
  2258. } else {
  2259. heads = SUN_SSWAP16(sunlabel->ntrks);
  2260. cylinders = SUN_SSWAP16(sunlabel->ncyl);
  2261. sectors = SUN_SSWAP16(sunlabel->nsect);
  2262. }
  2263. update_units();
  2264. current_label_type = label_sun;
  2265. partitions = 8;
  2266. return 1;
  2267. }
  2268. static const struct sun_predefined_drives {
  2269. const char *vendor;
  2270. const char *model;
  2271. unsigned short sparecyl;
  2272. unsigned short ncyl;
  2273. unsigned short nacyl;
  2274. unsigned short pcylcount;
  2275. unsigned short ntrks;
  2276. unsigned short nsect;
  2277. unsigned short rspeed;
  2278. } sun_drives[] = {
  2279. { "Quantum","ProDrive 80S",1,832,2,834,6,34,3662},
  2280. { "Quantum","ProDrive 105S",1,974,2,1019,6,35,3662},
  2281. { "CDC","Wren IV 94171-344",3,1545,2,1549,9,46,3600},
  2282. { "IBM","DPES-31080",0,4901,2,4903,4,108,5400},
  2283. { "IBM","DORS-32160",0,1015,2,1017,67,62,5400},
  2284. { "IBM","DNES-318350",0,11199,2,11474,10,320,7200},
  2285. { "SEAGATE","ST34371",0,3880,2,3882,16,135,7228},
  2286. { "","SUN0104",1,974,2,1019,6,35,3662},
  2287. { "","SUN0207",4,1254,2,1272,9,36,3600},
  2288. { "","SUN0327",3,1545,2,1549,9,46,3600},
  2289. { "","SUN0340",0,1538,2,1544,6,72,4200},
  2290. { "","SUN0424",2,1151,2,2500,9,80,4400},
  2291. { "","SUN0535",0,1866,2,2500,7,80,5400},
  2292. { "","SUN0669",5,1614,2,1632,15,54,3600},
  2293. { "","SUN1.0G",5,1703,2,1931,15,80,3597},
  2294. { "","SUN1.05",0,2036,2,2038,14,72,5400},
  2295. { "","SUN1.3G",6,1965,2,3500,17,80,5400},
  2296. { "","SUN2.1G",0,2733,2,3500,19,80,5400},
  2297. { "IOMEGA","Jaz",0,1019,2,1021,64,32,5394},
  2298. };
  2299. static const struct sun_predefined_drives *
  2300. sun_autoconfigure_scsi(void)
  2301. {
  2302. const struct sun_predefined_drives *p = NULL;
  2303. #ifdef SCSI_IOCTL_GET_IDLUN
  2304. unsigned int id[2];
  2305. char buffer[2048];
  2306. char buffer2[2048];
  2307. FILE *pfd;
  2308. char *vendor;
  2309. char *model;
  2310. char *q;
  2311. int i;
  2312. if (!ioctl(fd, SCSI_IOCTL_GET_IDLUN, &id)) {
  2313. sprintf(buffer,
  2314. "Host: scsi%d Channel: %02d Id: %02d Lun: %02d\n",
  2315. #if 0
  2316. ((id[0]>>24)&0xff)-/*PROC_SCSI_SCSI+PROC_SCSI_FILE*/33,
  2317. #else
  2318. /* This is very wrong (works only if you have one HBA),
  2319. but I haven't found a way how to get hostno
  2320. from the current kernel */
  2321. 0,
  2322. #endif
  2323. (id[0]>>16) & 0xff,
  2324. id[0] & 0xff,
  2325. (id[0]>>8) & 0xff
  2326. );
  2327. pfd = fopen("/proc/scsi/scsi","r");
  2328. if (pfd) {
  2329. while (fgets(buffer2, 2048, pfd)) {
  2330. if (!strcmp(buffer, buffer2)) {
  2331. if (fgets(buffer2,2048,pfd)) {
  2332. q = strstr(buffer2,"Vendor: ");
  2333. if (q) {
  2334. q += 8;
  2335. vendor = q;
  2336. q = strstr(q," ");
  2337. *q++ = 0; /* truncate vendor name */
  2338. q = strstr(q,"Model: ");
  2339. if (q) {
  2340. *q = 0;
  2341. q += 7;
  2342. model = q;
  2343. q = strstr(q," Rev: ");
  2344. if (q) {
  2345. *q = 0;
  2346. for (i = 0; i < SIZE(sun_drives); i++) {
  2347. if (*sun_drives[i].vendor && strcasecmp(sun_drives[i].vendor, vendor))
  2348. continue;
  2349. if (!strstr(model, sun_drives[i].model))
  2350. continue;
  2351. printf(_("Autoconfigure found a %s%s%s\n"),sun_drives[i].vendor,(*sun_drives[i].vendor) ? " " : "",sun_drives[i].model);
  2352. p = sun_drives + i;
  2353. break;
  2354. }
  2355. }
  2356. }
  2357. }
  2358. }
  2359. break;
  2360. }
  2361. }
  2362. fclose(pfd);
  2363. }
  2364. }
  2365. #endif
  2366. return p;
  2367. }
  2368. static void
  2369. create_sunlabel(void)
  2370. {
  2371. struct hd_geometry geometry;
  2372. unsigned int ndiv;
  2373. int i;
  2374. unsigned char c;
  2375. const struct sun_predefined_drives *p = NULL;
  2376. fprintf(stderr,
  2377. _("Building a new sun disklabel. Changes will remain in memory only,\n"
  2378. "until you decide to write them. After that, of course, the previous\n"
  2379. "content won't be recoverable.\n\n"));
  2380. #if BYTE_ORDER == LITTLE_ENDIAN
  2381. sun_other_endian = 1;
  2382. #else
  2383. sun_other_endian = 0;
  2384. #endif
  2385. memset(MBRbuffer, 0, sizeof(MBRbuffer));
  2386. sunlabel->magic = SUN_SSWAP16(SUN_LABEL_MAGIC);
  2387. if (!floppy) {
  2388. puts(_("Drive type\n"
  2389. " ? auto configure\n"
  2390. " 0 custom (with hardware detected defaults)"));
  2391. for (i = 0; i < SIZE(sun_drives); i++) {
  2392. printf(" %c %s%s%s\n",
  2393. i + 'a', sun_drives[i].vendor,
  2394. (*sun_drives[i].vendor) ? " " : "",
  2395. sun_drives[i].model);
  2396. }
  2397. while (1) {
  2398. c = read_char(_("Select type (? for auto, 0 for custom): "));
  2399. if (c >= 'a' && c < 'a' + SIZE(sun_drives)) {
  2400. p = sun_drives + c - 'a';
  2401. break;
  2402. } else if (c >= 'A' && c < 'A' + SIZE(sun_drives)) {
  2403. p = sun_drives + c - 'A';
  2404. break;
  2405. } else if (c == '0') {
  2406. break;
  2407. } else if (c == '?' && scsi_disk) {
  2408. p = sun_autoconfigure_scsi();
  2409. if (!p)
  2410. printf(_("Autoconfigure failed.\n"));
  2411. else
  2412. break;
  2413. }
  2414. }
  2415. }
  2416. if (!p || floppy) {
  2417. if (!ioctl(fd, HDIO_GETGEO, &geometry)) {
  2418. heads = geometry.heads;
  2419. sectors = geometry.sectors;
  2420. cylinders = geometry.cylinders;
  2421. } else {
  2422. heads = 0;
  2423. sectors = 0;
  2424. cylinders = 0;
  2425. }
  2426. if (floppy) {
  2427. sunlabel->nacyl = 0;
  2428. sunlabel->pcylcount = SUN_SSWAP16(cylinders);
  2429. sunlabel->rspeed = SUN_SSWAP16(300);
  2430. sunlabel->ilfact = SUN_SSWAP16(1);
  2431. sunlabel->sparecyl = 0;
  2432. } else {
  2433. heads = read_int(1,heads,1024,0,_("Heads"));
  2434. sectors = read_int(1,sectors,1024,0,_("Sectors/track"));
  2435. if (cylinders)
  2436. cylinders = read_int(1,cylinders-2,65535,0,_("Cylinders"));
  2437. else
  2438. cylinders = read_int(1,0,65535,0,_("Cylinders"));
  2439. sunlabel->nacyl = SUN_SSWAP16(read_int(0,2,65535,0, _("Alternate cylinders")));
  2440. sunlabel->pcylcount = SUN_SSWAP16(read_int(0,cylinders+SUN_SSWAP16(sunlabel->nacyl), 65535,0, _("Physical cylinders")));
  2441. sunlabel->rspeed = SUN_SSWAP16(read_int(1,5400,100000,0, _("Rotation speed (rpm)")));
  2442. sunlabel->ilfact = SUN_SSWAP16(read_int(1,1,32,0, _("Interleave factor")));
  2443. sunlabel->sparecyl = SUN_SSWAP16(read_int(0,0,sectors,0, _("Extra sectors per cylinder")));
  2444. }
  2445. } else {
  2446. sunlabel->sparecyl = SUN_SSWAP16(p->sparecyl);
  2447. sunlabel->ncyl = SUN_SSWAP16(p->ncyl);
  2448. sunlabel->nacyl = SUN_SSWAP16(p->nacyl);
  2449. sunlabel->pcylcount = SUN_SSWAP16(p->pcylcount);
  2450. sunlabel->ntrks = SUN_SSWAP16(p->ntrks);
  2451. sunlabel->nsect = SUN_SSWAP16(p->nsect);
  2452. sunlabel->rspeed = SUN_SSWAP16(p->rspeed);
  2453. sunlabel->ilfact = SUN_SSWAP16(1);
  2454. cylinders = p->ncyl;
  2455. heads = p->ntrks;
  2456. sectors = p->nsect;
  2457. puts(_("You may change all the disk params from the x menu"));
  2458. }
  2459. snprintf((char *)(sunlabel->info), sizeof(sunlabel->info),
  2460. "%s%s%s cyl %d alt %d hd %d sec %d",
  2461. p ? p->vendor : "", (p && *p->vendor) ? " " : "",
  2462. p ? p->model : (floppy ? _("3,5\" floppy") : _("Linux custom")),
  2463. cylinders, SUN_SSWAP16(sunlabel->nacyl), heads, sectors);
  2464. sunlabel->ntrks = SUN_SSWAP16(heads);
  2465. sunlabel->nsect = SUN_SSWAP16(sectors);
  2466. sunlabel->ncyl = SUN_SSWAP16(cylinders);
  2467. if (floppy)
  2468. set_sun_partition(0, 0, cylinders * heads * sectors, LINUX_NATIVE);
  2469. else {
  2470. if (cylinders * heads * sectors >= 150 * 2048) {
  2471. ndiv = cylinders - (50 * 2048 / (heads * sectors)); /* 50M swap */
  2472. } else
  2473. ndiv = cylinders * 2 / 3;
  2474. set_sun_partition(0, 0, ndiv * heads * sectors, LINUX_NATIVE);
  2475. set_sun_partition(1, ndiv * heads * sectors, cylinders * heads * sectors, LINUX_SWAP);
  2476. sunlabel->infos[1].flags |= 0x01; /* Not mountable */
  2477. }
  2478. set_sun_partition(2, 0, cylinders * heads * sectors, WHOLE_DISK);
  2479. {
  2480. unsigned short *ush = (unsigned short *)sunlabel;
  2481. unsigned short csum = 0;
  2482. while (ush < (unsigned short *)(&sunlabel->csum))
  2483. csum ^= *ush++;
  2484. sunlabel->csum = csum;
  2485. }
  2486. set_all_unchanged();
  2487. set_changed(0);
  2488. get_boot(create_empty_sun);
  2489. }
  2490. static void
  2491. toggle_sunflags(int i, unsigned char mask)
  2492. {
  2493. if (sunlabel->infos[i].flags & mask)
  2494. sunlabel->infos[i].flags &= ~mask;
  2495. else
  2496. sunlabel->infos[i].flags |= mask;
  2497. set_changed(i);
  2498. }
  2499. static void
  2500. fetch_sun(uint *starts, uint *lens, uint *start, uint *stop)
  2501. {
  2502. int i, continuous = 1;
  2503. *start = 0;
  2504. *stop = cylinders * heads * sectors;
  2505. for (i = 0; i < partitions; i++) {
  2506. if (sunlabel->partitions[i].num_sectors
  2507. && sunlabel->infos[i].id
  2508. && sunlabel->infos[i].id != WHOLE_DISK) {
  2509. starts[i] = SUN_SSWAP32(sunlabel->partitions[i].start_cylinder) * heads * sectors;
  2510. lens[i] = SUN_SSWAP32(sunlabel->partitions[i].num_sectors);
  2511. if (continuous) {
  2512. if (starts[i] == *start)
  2513. *start += lens[i];
  2514. else if (starts[i] + lens[i] >= *stop)
  2515. *stop = starts[i];
  2516. else
  2517. continuous = 0;
  2518. /* There will be probably more gaps
  2519. than one, so lets check afterwards */
  2520. }
  2521. } else {
  2522. starts[i] = 0;
  2523. lens[i] = 0;
  2524. }
  2525. }
  2526. }
  2527. static uint *verify_sun_starts;
  2528. static int
  2529. verify_sun_cmp(int *a, int *b)
  2530. {
  2531. if (*a == -1) return 1;
  2532. if (*b == -1) return -1;
  2533. if (verify_sun_starts[*a] > verify_sun_starts[*b]) return 1;
  2534. return -1;
  2535. }
  2536. static void
  2537. verify_sun(void)
  2538. {
  2539. uint starts[8], lens[8], start, stop;
  2540. int i,j,k,starto,endo;
  2541. int array[8];
  2542. verify_sun_starts = starts;
  2543. fetch_sun(starts,lens,&start,&stop);
  2544. for (k = 0; k < 7; k++) {
  2545. for (i = 0; i < 8; i++) {
  2546. if (k && (lens[i] % (heads * sectors))) {
  2547. printf(_("Partition %d doesn't end on cylinder boundary\n"), i+1);
  2548. }
  2549. if (lens[i]) {
  2550. for (j = 0; j < i; j++)
  2551. if (lens[j]) {
  2552. if (starts[j] == starts[i]+lens[i]) {
  2553. starts[j] = starts[i]; lens[j] += lens[i];
  2554. lens[i] = 0;
  2555. } else if (starts[i] == starts[j]+lens[j]){
  2556. lens[j] += lens[i];
  2557. lens[i] = 0;
  2558. } else if (!k) {
  2559. if (starts[i] < starts[j]+lens[j]
  2560. && starts[j] < starts[i]+lens[i]) {
  2561. starto = starts[i];
  2562. if (starts[j] > starto)
  2563. starto = starts[j];
  2564. endo = starts[i]+lens[i];
  2565. if (starts[j]+lens[j] < endo)
  2566. endo = starts[j]+lens[j];
  2567. printf(_("Partition %d overlaps with others in "
  2568. "sectors %d-%d\n"), i+1, starto, endo);
  2569. }
  2570. }
  2571. }
  2572. }
  2573. }
  2574. }
  2575. for (i = 0; i < 8; i++) {
  2576. if (lens[i])
  2577. array[i] = i;
  2578. else
  2579. array[i] = -1;
  2580. }
  2581. qsort(array,SIZE(array),sizeof(array[0]),
  2582. (int (*)(const void *,const void *)) verify_sun_cmp);
  2583. if (array[0] == -1) {
  2584. printf(_("No partitions defined\n"));
  2585. return;
  2586. }
  2587. stop = cylinders * heads * sectors;
  2588. if (starts[array[0]])
  2589. printf(_("Unused gap - sectors 0-%d\n"),starts[array[0]]);
  2590. for (i = 0; i < 7 && array[i+1] != -1; i++) {
  2591. printf(_("Unused gap - sectors %d-%d\n"),starts[array[i]]+lens[array[i]],starts[array[i+1]]);
  2592. }
  2593. start = starts[array[i]] + lens[array[i]];
  2594. if (start < stop)
  2595. printf(_("Unused gap - sectors %d-%d\n"),start,stop);
  2596. }
  2597. static void
  2598. add_sun_partition(int n, int sys)
  2599. {
  2600. uint start, stop, stop2;
  2601. uint starts[8], lens[8];
  2602. int whole_disk = 0;
  2603. char mesg[256];
  2604. int i, first, last;
  2605. if (sunlabel->partitions[n].num_sectors && sunlabel->infos[n].id) {
  2606. printf(_("Partition %d is already defined. Delete "
  2607. "it before re-adding it.\n"), n + 1);
  2608. return;
  2609. }
  2610. fetch_sun(starts,lens,&start,&stop);
  2611. if (stop <= start) {
  2612. if (n == 2)
  2613. whole_disk = 1;
  2614. else {
  2615. printf(_("Other partitions already cover the whole disk.\nDelete "
  2616. "some/shrink them before retry.\n"));
  2617. return;
  2618. }
  2619. }
  2620. snprintf(mesg, sizeof(mesg), _("First %s"), str_units(SINGULAR));
  2621. while (1) {
  2622. if (whole_disk)
  2623. first = read_int(0, 0, 0, 0, mesg);
  2624. else
  2625. first = read_int(scround(start), scround(stop)+1,
  2626. scround(stop), 0, mesg);
  2627. if (display_in_cyl_units)
  2628. first *= units_per_sector;
  2629. else
  2630. /* Starting sector has to be properly aligned */
  2631. first = (first + heads * sectors - 1) / (heads * sectors);
  2632. if (n == 2 && first != 0)
  2633. printf("\
  2634. It is highly recommended that the third partition covers the whole disk\n\
  2635. and is of type `Whole disk'\n");
  2636. /* ewt asks to add: "don't start a partition at cyl 0"
  2637. However, edmundo@rano.demon.co.uk writes:
  2638. "In addition to having a Sun partition table, to be able to
  2639. boot from the disc, the first partition, /dev/sdX1, must
  2640. start at cylinder 0. This means that /dev/sdX1 contains
  2641. the partition table and the boot block, as these are the
  2642. first two sectors of the disc. Therefore you must be
  2643. careful what you use /dev/sdX1 for. In particular, you must
  2644. not use a partition starting at cylinder 0 for Linux swap,
  2645. as that would overwrite the partition table and the boot
  2646. block. You may, however, use such a partition for a UFS
  2647. or EXT2 file system, as these file systems leave the first
  2648. 1024 bytes undisturbed. */
  2649. /* On the other hand, one should not use partitions
  2650. starting at block 0 in an md, or the label will
  2651. be trashed. */
  2652. for (i = 0; i < partitions; i++)
  2653. if (lens[i] && starts[i] <= first && starts[i] + lens[i] > first)
  2654. break;
  2655. if (i < partitions && !whole_disk) {
  2656. if (n == 2 && !first) {
  2657. whole_disk = 1;
  2658. break;
  2659. }
  2660. printf(_("Sector %d is already allocated\n"), first);
  2661. } else
  2662. break;
  2663. }
  2664. stop = cylinders * heads * sectors;
  2665. stop2 = stop;
  2666. for (i = 0; i < partitions; i++) {
  2667. if (starts[i] > first && starts[i] < stop)
  2668. stop = starts[i];
  2669. }
  2670. snprintf(mesg, sizeof(mesg),
  2671. _("Last %s or +size or +sizeM or +sizeK"),
  2672. str_units(SINGULAR));
  2673. if (whole_disk)
  2674. last = read_int(scround(stop2), scround(stop2), scround(stop2),
  2675. 0, mesg);
  2676. else if (n == 2 && !first)
  2677. last = read_int(scround(first), scround(stop2), scround(stop2),
  2678. scround(first), mesg);
  2679. else
  2680. last = read_int(scround(first), scround(stop), scround(stop),
  2681. scround(first), mesg);
  2682. if (display_in_cyl_units)
  2683. last *= units_per_sector;
  2684. if (n == 2 && !first) {
  2685. if (last >= stop2) {
  2686. whole_disk = 1;
  2687. last = stop2;
  2688. } else if (last > stop) {
  2689. printf(_("You haven't covered the whole disk with "
  2690. "the 3rd partition, but your value\n"
  2691. "%d %s covers some other partition. "
  2692. "Your entry has been changed\n"
  2693. "to %d %s\n"),
  2694. scround(last), str_units(SINGULAR),
  2695. scround(stop), str_units(SINGULAR));
  2696. last = stop;
  2697. }
  2698. } else if (!whole_disk && last > stop)
  2699. last = stop;
  2700. if (whole_disk)
  2701. sys = WHOLE_DISK;
  2702. set_sun_partition(n, first, last, sys);
  2703. }
  2704. static void
  2705. sun_delete_partition(int i)
  2706. {
  2707. unsigned int nsec;
  2708. if (i == 2
  2709. && sunlabel->infos[i].id == WHOLE_DISK
  2710. && !sunlabel->partitions[i].start_cylinder
  2711. && (nsec = SUN_SSWAP32(sunlabel->partitions[i].num_sectors)) == heads * sectors * cylinders)
  2712. printf(_("If you want to maintain SunOS/Solaris compatibility, "
  2713. "consider leaving this\n"
  2714. "partition as Whole disk (5), starting at 0, with %u "
  2715. "sectors\n"), nsec);
  2716. sunlabel->infos[i].id = 0;
  2717. sunlabel->partitions[i].num_sectors = 0;
  2718. }
  2719. static void
  2720. sun_change_sysid(int i, int sys)
  2721. {
  2722. if (sys == LINUX_SWAP && !sunlabel->partitions[i].start_cylinder) {
  2723. read_chars(
  2724. _("It is highly recommended that the partition at offset 0\n"
  2725. "is UFS, EXT2FS filesystem or SunOS swap. Putting Linux swap\n"
  2726. "there may destroy your partition table and bootblock.\n"
  2727. "Type YES if you're very sure you would like that partition\n"
  2728. "tagged with 82 (Linux swap): "));
  2729. if (strcmp (line_ptr, _("YES\n")))
  2730. return;
  2731. }
  2732. switch (sys) {
  2733. case SUNOS_SWAP:
  2734. case LINUX_SWAP:
  2735. /* swaps are not mountable by default */
  2736. sunlabel->infos[i].flags |= 0x01;
  2737. break;
  2738. default:
  2739. /* assume other types are mountable;
  2740. user can change it anyway */
  2741. sunlabel->infos[i].flags &= ~0x01;
  2742. break;
  2743. }
  2744. sunlabel->infos[i].id = sys;
  2745. }
  2746. static void
  2747. sun_list_table(int xtra)
  2748. {
  2749. int i, w;
  2750. w = strlen(disk_device);
  2751. if (xtra)
  2752. printf(
  2753. _("\nDisk %s (Sun disk label): %d heads, %d sectors, %d rpm\n"
  2754. "%d cylinders, %d alternate cylinders, %d physical cylinders\n"
  2755. "%d extra sects/cyl, interleave %d:1\n"
  2756. "%s\n"
  2757. "Units = %s of %d * 512 bytes\n\n"),
  2758. disk_device, heads, sectors, SUN_SSWAP16(sunlabel->rspeed),
  2759. cylinders, SUN_SSWAP16(sunlabel->nacyl),
  2760. SUN_SSWAP16(sunlabel->pcylcount),
  2761. SUN_SSWAP16(sunlabel->sparecyl),
  2762. SUN_SSWAP16(sunlabel->ilfact),
  2763. (char *)sunlabel,
  2764. str_units(PLURAL), units_per_sector);
  2765. else
  2766. printf(
  2767. _("\nDisk %s (Sun disk label): %d heads, %d sectors, %d cylinders\n"
  2768. "Units = %s of %d * 512 bytes\n\n"),
  2769. disk_device, heads, sectors, cylinders,
  2770. str_units(PLURAL), units_per_sector);
  2771. printf(_("%*s Flag Start End Blocks Id System\n"),
  2772. w + 1, _("Device"));
  2773. for (i = 0 ; i < partitions; i++) {
  2774. if (sunlabel->partitions[i].num_sectors) {
  2775. uint32_t start = SUN_SSWAP32(sunlabel->partitions[i].start_cylinder) * heads * sectors;
  2776. uint32_t len = SUN_SSWAP32(sunlabel->partitions[i].num_sectors);
  2777. printf("%s %c%c %9ld %9ld %9ld%c %2x %s\n",
  2778. partname(disk_device, i+1, w), /* device */
  2779. (sunlabel->infos[i].flags & 0x01) ? 'u' : ' ', /* flags */
  2780. (sunlabel->infos[i].flags & 0x10) ? 'r' : ' ',
  2781. (long) scround(start), /* start */
  2782. (long) scround(start+len), /* end */
  2783. (long) len / 2, len & 1 ? '+' : ' ', /* odd flag on end */
  2784. sunlabel->infos[i].id, /* type id */
  2785. partition_type(sunlabel->infos[i].id)); /* type name */
  2786. }
  2787. }
  2788. }
  2789. #ifdef CONFIG_FEATURE_FDISK_ADVANCED
  2790. static void
  2791. sun_set_alt_cyl(void)
  2792. {
  2793. sunlabel->nacyl =
  2794. SUN_SSWAP16(read_int(0,SUN_SSWAP16(sunlabel->nacyl), 65535, 0,
  2795. _("Number of alternate cylinders")));
  2796. }
  2797. static void
  2798. sun_set_ncyl(int cyl)
  2799. {
  2800. sunlabel->ncyl = SUN_SSWAP16(cyl);
  2801. }
  2802. static void
  2803. sun_set_xcyl(void)
  2804. {
  2805. sunlabel->sparecyl =
  2806. SUN_SSWAP16(read_int(0, SUN_SSWAP16(sunlabel->sparecyl), sectors, 0,
  2807. _("Extra sectors per cylinder")));
  2808. }
  2809. static void
  2810. sun_set_ilfact(void)
  2811. {
  2812. sunlabel->ilfact =
  2813. SUN_SSWAP16(read_int(1, SUN_SSWAP16(sunlabel->ilfact), 32, 0,
  2814. _("Interleave factor")));
  2815. }
  2816. static void
  2817. sun_set_rspeed(void)
  2818. {
  2819. sunlabel->rspeed =
  2820. SUN_SSWAP16(read_int(1, SUN_SSWAP16(sunlabel->rspeed), 100000, 0,
  2821. _("Rotation speed (rpm)")));
  2822. }
  2823. static void
  2824. sun_set_pcylcount(void)
  2825. {
  2826. sunlabel->pcylcount =
  2827. SUN_SSWAP16(read_int(0, SUN_SSWAP16(sunlabel->pcylcount), 65535, 0,
  2828. _("Number of physical cylinders")));
  2829. }
  2830. #endif /* CONFIG_FEATURE_FDISK_ADVANCED */
  2831. static void
  2832. sun_write_table(void)
  2833. {
  2834. unsigned short *ush = (unsigned short *)sunlabel;
  2835. unsigned short csum = 0;
  2836. while (ush < (unsigned short *)(&sunlabel->csum))
  2837. csum ^= *ush++;
  2838. sunlabel->csum = csum;
  2839. if (lseek(fd, 0, SEEK_SET) < 0)
  2840. fdisk_fatal(unable_to_seek);
  2841. if (write(fd, sunlabel, SECTOR_SIZE) != SECTOR_SIZE)
  2842. fdisk_fatal(unable_to_write);
  2843. }
  2844. #endif /* SUN_LABEL */
  2845. /* DOS partition types */
  2846. static const struct systypes i386_sys_types[] = {
  2847. { "\x00" "Empty" },
  2848. { "\x01" "FAT12" },
  2849. { "\x04" "FAT16 <32M" },
  2850. { "\x05" "Extended" }, /* DOS 3.3+ extended partition */
  2851. { "\x06" "FAT16" }, /* DOS 16-bit >=32M */
  2852. { "\x07" "HPFS/NTFS" }, /* OS/2 IFS, eg, HPFS or NTFS or QNX */
  2853. { "\x0a" "OS/2 Boot Manager" },/* OS/2 Boot Manager */
  2854. { "\x0b" "Win95 FAT32" },
  2855. { "\x0c" "Win95 FAT32 (LBA)" },/* LBA really is `Extended Int 13h' */
  2856. { "\x0e" "Win95 FAT16 (LBA)" },
  2857. { "\x0f" "Win95 Ext'd (LBA)" },
  2858. { "\x11" "Hidden FAT12" },
  2859. { "\x12" "Compaq diagnostics" },
  2860. { "\x14" "Hidden FAT16 <32M" },
  2861. { "\x16" "Hidden FAT16" },
  2862. { "\x17" "Hidden HPFS/NTFS" },
  2863. { "\x1b" "Hidden Win95 FAT32" },
  2864. { "\x1c" "Hidden Win95 FAT32 (LBA)" },
  2865. { "\x1e" "Hidden Win95 FAT16 (LBA)" },
  2866. { "\x3c" "PartitionMagic recovery" },
  2867. { "\x41" "PPC PReP Boot" },
  2868. { "\x42" "SFS" },
  2869. { "\x63" "GNU HURD or SysV" }, /* GNU HURD or Mach or Sys V/386 (such as ISC UNIX) */
  2870. { "\x80" "Old Minix" }, /* Minix 1.4a and earlier */
  2871. { "\x81" "Minix / old Linux" },/* Minix 1.4b and later */
  2872. { "\x82" "Linux swap" }, /* also Solaris */
  2873. { "\x83" "Linux" },
  2874. { "\x84" "OS/2 hidden C: drive" },
  2875. { "\x85" "Linux extended" },
  2876. { "\x86" "NTFS volume set" },
  2877. { "\x87" "NTFS volume set" },
  2878. { "\x8e" "Linux LVM" },
  2879. { "\x9f" "BSD/OS" }, /* BSDI */
  2880. { "\xa0" "IBM Thinkpad hibernation" },
  2881. { "\xa5" "FreeBSD" }, /* various BSD flavours */
  2882. { "\xa6" "OpenBSD" },
  2883. { "\xa8" "Darwin UFS" },
  2884. { "\xa9" "NetBSD" },
  2885. { "\xab" "Darwin boot" },
  2886. { "\xb7" "BSDI fs" },
  2887. { "\xb8" "BSDI swap" },
  2888. { "\xbe" "Solaris boot" },
  2889. { "\xeb" "BeOS fs" },
  2890. { "\xee" "EFI GPT" }, /* Intel EFI GUID Partition Table */
  2891. { "\xef" "EFI (FAT-12/16/32)" },/* Intel EFI System Partition */
  2892. { "\xf0" "Linux/PA-RISC boot" },/* Linux/PA-RISC boot loader */
  2893. { "\xf2" "DOS secondary" }, /* DOS 3.3+ secondary */
  2894. { "\xfd" "Linux raid autodetect" },/* New (2.2.x) raid partition with
  2895. autodetect using persistent
  2896. superblock */
  2897. #ifdef CONFIG_WEIRD_PARTITION_TYPES
  2898. { "\x02" "XENIX root" },
  2899. { "\x03" "XENIX usr" },
  2900. { "\x08" "AIX" }, /* AIX boot (AIX -- PS/2 port) or SplitDrive */
  2901. { "\x09" "AIX bootable" }, /* AIX data or Coherent */
  2902. { "\x10" "OPUS" },
  2903. { "\x18" "AST SmartSleep" },
  2904. { "\x24" "NEC DOS" },
  2905. { "\x39" "Plan 9" },
  2906. { "\x40" "Venix 80286" },
  2907. { "\x4d" "QNX4.x" },
  2908. { "\x4e" "QNX4.x 2nd part" },
  2909. { "\x4f" "QNX4.x 3rd part" },
  2910. { "\x50" "OnTrack DM" },
  2911. { "\x51" "OnTrack DM6 Aux1" }, /* (or Novell) */
  2912. { "\x52" "CP/M" }, /* CP/M or Microport SysV/AT */
  2913. { "\x53" "OnTrack DM6 Aux3" },
  2914. { "\x54" "OnTrackDM6" },
  2915. { "\x55" "EZ-Drive" },
  2916. { "\x56" "Golden Bow" },
  2917. { "\x5c" "Priam Edisk" },
  2918. { "\x61" "SpeedStor" },
  2919. { "\x64" "Novell Netware 286" },
  2920. { "\x65" "Novell Netware 386" },
  2921. { "\x70" "DiskSecure Multi-Boot" },
  2922. { "\x75" "PC/IX" },
  2923. { "\x93" "Amoeba" },
  2924. { "\x94" "Amoeba BBT" }, /* (bad block table) */
  2925. { "\xa7" "NeXTSTEP" },
  2926. { "\xbb" "Boot Wizard hidden" },
  2927. { "\xc1" "DRDOS/sec (FAT-12)" },
  2928. { "\xc4" "DRDOS/sec (FAT-16 < 32M)" },
  2929. { "\xc6" "DRDOS/sec (FAT-16)" },
  2930. { "\xc7" "Syrinx" },
  2931. { "\xda" "Non-FS data" },
  2932. { "\xdb" "CP/M / CTOS / ..." },/* CP/M or Concurrent CP/M or
  2933. Concurrent DOS or CTOS */
  2934. { "\xde" "Dell Utility" }, /* Dell PowerEdge Server utilities */
  2935. { "\xdf" "BootIt" }, /* BootIt EMBRM */
  2936. { "\xe1" "DOS access" }, /* DOS access or SpeedStor 12-bit FAT
  2937. extended partition */
  2938. { "\xe3" "DOS R/O" }, /* DOS R/O or SpeedStor */
  2939. { "\xe4" "SpeedStor" }, /* SpeedStor 16-bit FAT extended
  2940. partition < 1024 cyl. */
  2941. { "\xf1" "SpeedStor" },
  2942. { "\xf4" "SpeedStor" }, /* SpeedStor large partition */
  2943. { "\xfe" "LANstep" }, /* SpeedStor >1024 cyl. or LANstep */
  2944. { "\xff" "BBT" }, /* Xenix Bad Block Table */
  2945. #endif
  2946. { 0 }
  2947. };
  2948. /* A valid partition table sector ends in 0x55 0xaa */
  2949. static unsigned int
  2950. part_table_flag(const char *b)
  2951. {
  2952. return ((uint) b[510]) + (((uint) b[511]) << 8);
  2953. }
  2954. #ifdef CONFIG_FEATURE_FDISK_WRITABLE
  2955. static void
  2956. write_part_table_flag(char *b)
  2957. {
  2958. b[510] = 0x55;
  2959. b[511] = 0xaa;
  2960. }
  2961. /* start_sect and nr_sects are stored little endian on all machines */
  2962. /* moreover, they are not aligned correctly */
  2963. static void
  2964. store4_little_endian(unsigned char *cp, unsigned int val)
  2965. {
  2966. cp[0] = (val & 0xff);
  2967. cp[1] = ((val >> 8) & 0xff);
  2968. cp[2] = ((val >> 16) & 0xff);
  2969. cp[3] = ((val >> 24) & 0xff);
  2970. }
  2971. #endif /* CONFIG_FEATURE_FDISK_WRITABLE */
  2972. static unsigned int
  2973. read4_little_endian(const unsigned char *cp)
  2974. {
  2975. return (uint)(cp[0]) + ((uint)(cp[1]) << 8)
  2976. + ((uint)(cp[2]) << 16) + ((uint)(cp[3]) << 24);
  2977. }
  2978. #ifdef CONFIG_FEATURE_FDISK_WRITABLE
  2979. static void
  2980. set_start_sect(struct partition *p, unsigned int start_sect)
  2981. {
  2982. store4_little_endian(p->start4, start_sect);
  2983. }
  2984. #endif
  2985. static int32_t
  2986. get_start_sect(const struct partition *p)
  2987. {
  2988. return read4_little_endian(p->start4);
  2989. }
  2990. #ifdef CONFIG_FEATURE_FDISK_WRITABLE
  2991. static void
  2992. set_nr_sects(struct partition *p, int32_t nr_sects)
  2993. {
  2994. store4_little_endian(p->size4, nr_sects);
  2995. }
  2996. #endif
  2997. static int32_t
  2998. get_nr_sects(const struct partition *p)
  2999. {
  3000. return read4_little_endian(p->size4);
  3001. }
  3002. /* normally O_RDWR, -l option gives O_RDONLY */
  3003. static int type_open = O_RDWR;
  3004. static int ext_index; /* the prime extended partition */
  3005. static int listing; /* no aborts for fdisk -l */
  3006. static int dos_compatible_flag = ~0;
  3007. #ifdef CONFIG_FEATURE_FDISK_WRITABLE
  3008. static int dos_changed;
  3009. static int nowarn; /* no warnings for fdisk -l/-s */
  3010. #endif
  3011. static uint user_cylinders, user_heads, user_sectors;
  3012. static uint pt_heads, pt_sectors;
  3013. static uint kern_heads, kern_sectors;
  3014. static off_t extended_offset; /* offset of link pointers */
  3015. static unsigned long long total_number_of_sectors;
  3016. static jmp_buf listingbuf;
  3017. static void fdisk_fatal(enum failure why)
  3018. {
  3019. const char *message;
  3020. if (listing) {
  3021. close(fd);
  3022. longjmp(listingbuf, 1);
  3023. }
  3024. switch (why) {
  3025. case unable_to_open:
  3026. message = "Unable to open %s\n";
  3027. break;
  3028. case unable_to_read:
  3029. message = "Unable to read %s\n";
  3030. break;
  3031. case unable_to_seek:
  3032. message = "Unable to seek on %s\n";
  3033. break;
  3034. case unable_to_write:
  3035. message = "Unable to write %s\n";
  3036. break;
  3037. case ioctl_error:
  3038. message = "BLKGETSIZE ioctl failed on %s\n";
  3039. break;
  3040. default:
  3041. message = "Fatal error\n";
  3042. }
  3043. fputc('\n', stderr);
  3044. fprintf(stderr, message, disk_device);
  3045. exit(1);
  3046. }
  3047. static void
  3048. seek_sector(off_t secno)
  3049. {
  3050. off_t offset = secno * sector_size;
  3051. if (lseek(fd, offset, SEEK_SET) == (off_t) -1)
  3052. fdisk_fatal(unable_to_seek);
  3053. }
  3054. #ifdef CONFIG_FEATURE_FDISK_WRITABLE
  3055. static void
  3056. write_sector(off_t secno, char *buf)
  3057. {
  3058. seek_sector(secno);
  3059. if (write(fd, buf, sector_size) != sector_size)
  3060. fdisk_fatal(unable_to_write);
  3061. }
  3062. #endif
  3063. /* Allocate a buffer and read a partition table sector */
  3064. static void
  3065. read_pte(struct pte *pe, off_t offset)
  3066. {
  3067. pe->offset = offset;
  3068. pe->sectorbuffer = (char *) xmalloc(sector_size);
  3069. seek_sector(offset);
  3070. if (read(fd, pe->sectorbuffer, sector_size) != sector_size)
  3071. fdisk_fatal(unable_to_read);
  3072. #ifdef CONFIG_FEATURE_FDISK_WRITABLE
  3073. pe->changed = 0;
  3074. #endif
  3075. pe->part_table = pe->ext_pointer = NULL;
  3076. }
  3077. static unsigned int
  3078. get_partition_start(const struct pte *pe)
  3079. {
  3080. return pe->offset + get_start_sect(pe->part_table);
  3081. }
  3082. #ifdef CONFIG_FEATURE_FDISK_WRITABLE
  3083. /*
  3084. * Avoid warning about DOS partitions when no DOS partition was changed.
  3085. * Here a heuristic "is probably dos partition".
  3086. * We might also do the opposite and warn in all cases except
  3087. * for "is probably nondos partition".
  3088. */
  3089. static int
  3090. is_dos_partition(int t)
  3091. {
  3092. return (t == 1 || t == 4 || t == 6 ||
  3093. t == 0x0b || t == 0x0c || t == 0x0e ||
  3094. t == 0x11 || t == 0x12 || t == 0x14 || t == 0x16 ||
  3095. t == 0x1b || t == 0x1c || t == 0x1e || t == 0x24 ||
  3096. t == 0xc1 || t == 0xc4 || t == 0xc6);
  3097. }
  3098. static void
  3099. menu(void)
  3100. {
  3101. #ifdef CONFIG_FEATURE_SUN_LABEL
  3102. if (label_sun == current_label_type) {
  3103. puts(_("Command action"));
  3104. puts(_("\ta\ttoggle a read only flag")); /* sun */
  3105. puts(_("\tb\tedit bsd disklabel"));
  3106. puts(_("\tc\ttoggle the mountable flag")); /* sun */
  3107. puts(_("\td\tdelete a partition"));
  3108. puts(_("\tl\tlist known partition types"));
  3109. puts(_("\tm\tprint this menu"));
  3110. puts(_("\tn\tadd a new partition"));
  3111. puts(_("\to\tcreate a new empty DOS partition table"));
  3112. puts(_("\tp\tprint the partition table"));
  3113. puts(_("\tq\tquit without saving changes"));
  3114. puts(_("\ts\tcreate a new empty Sun disklabel")); /* sun */
  3115. puts(_("\tt\tchange a partition's system id"));
  3116. puts(_("\tu\tchange display/entry units"));
  3117. puts(_("\tv\tverify the partition table"));
  3118. puts(_("\tw\twrite table to disk and exit"));
  3119. #ifdef CONFIG_FEATURE_FDISK_ADVANCED
  3120. puts(_("\tx\textra functionality (experts only)"));
  3121. #endif
  3122. } else
  3123. #endif
  3124. #ifdef CONFIG_FEATURE_SGI_LABEL
  3125. if (label_sgi == current_label_type) {
  3126. puts(_("Command action"));
  3127. puts(_("\ta\tselect bootable partition")); /* sgi flavour */
  3128. puts(_("\tb\tedit bootfile entry")); /* sgi */
  3129. puts(_("\tc\tselect sgi swap partition")); /* sgi flavour */
  3130. puts(_("\td\tdelete a partition"));
  3131. puts(_("\tl\tlist known partition types"));
  3132. puts(_("\tm\tprint this menu"));
  3133. puts(_("\tn\tadd a new partition"));
  3134. puts(_("\to\tcreate a new empty DOS partition table"));
  3135. puts(_("\tp\tprint the partition table"));
  3136. puts(_("\tq\tquit without saving changes"));
  3137. puts(_("\ts\tcreate a new empty Sun disklabel")); /* sun */
  3138. puts(_("\tt\tchange a partition's system id"));
  3139. puts(_("\tu\tchange display/entry units"));
  3140. puts(_("\tv\tverify the partition table"));
  3141. puts(_("\tw\twrite table to disk and exit"));
  3142. } else
  3143. #endif
  3144. #ifdef CONFIG_FEATURE_AIX_LABEL
  3145. if (label_aix == current_label_type) {
  3146. puts(_("Command action"));
  3147. puts(_("\tm\tprint this menu"));
  3148. puts(_("\to\tcreate a new empty DOS partition table"));
  3149. puts(_("\tq\tquit without saving changes"));
  3150. puts(_("\ts\tcreate a new empty Sun disklabel")); /* sun */
  3151. } else
  3152. #endif
  3153. {
  3154. puts(_("Command action"));
  3155. puts(_("\ta\ttoggle a bootable flag"));
  3156. puts(_("\tb\tedit bsd disklabel"));
  3157. puts(_("\tc\ttoggle the dos compatibility flag"));
  3158. puts(_("\td\tdelete a partition"));
  3159. puts(_("\tl\tlist known partition types"));
  3160. puts(_("\tm\tprint this menu"));
  3161. puts(_("\tn\tadd a new partition"));
  3162. puts(_("\to\tcreate a new empty DOS partition table"));
  3163. puts(_("\tp\tprint the partition table"));
  3164. puts(_("\tq\tquit without saving changes"));
  3165. puts(_("\ts\tcreate a new empty Sun disklabel")); /* sun */
  3166. puts(_("\tt\tchange a partition's system id"));
  3167. puts(_("\tu\tchange display/entry units"));
  3168. puts(_("\tv\tverify the partition table"));
  3169. puts(_("\tw\twrite table to disk and exit"));
  3170. #ifdef CONFIG_FEATURE_FDISK_ADVANCED
  3171. puts(_("\tx\textra functionality (experts only)"));
  3172. #endif
  3173. }
  3174. }
  3175. #endif /* CONFIG_FEATURE_FDISK_WRITABLE */
  3176. #ifdef CONFIG_FEATURE_FDISK_ADVANCED
  3177. static void
  3178. xmenu(void)
  3179. {
  3180. #ifdef CONFIG_FEATURE_SUN_LABEL
  3181. if (label_sun == current_label_type) {
  3182. puts(_("Command action"));
  3183. puts(_("\ta\tchange number of alternate cylinders")); /*sun*/
  3184. puts(_("\tc\tchange number of cylinders"));
  3185. puts(_("\td\tprint the raw data in the partition table"));
  3186. puts(_("\te\tchange number of extra sectors per cylinder"));/*sun*/
  3187. puts(_("\th\tchange number of heads"));
  3188. puts(_("\ti\tchange interleave factor")); /*sun*/
  3189. puts(_("\to\tchange rotation speed (rpm)")); /*sun*/
  3190. puts(_("\tm\tprint this menu"));
  3191. puts(_("\tp\tprint the partition table"));
  3192. puts(_("\tq\tquit without saving changes"));
  3193. puts(_("\tr\treturn to main menu"));
  3194. puts(_("\ts\tchange number of sectors/track"));
  3195. puts(_("\tv\tverify the partition table"));
  3196. puts(_("\tw\twrite table to disk and exit"));
  3197. puts(_("\ty\tchange number of physical cylinders")); /*sun*/
  3198. } else
  3199. #endif
  3200. #ifdef CONFIG_FEATURE_SGI_LABEL
  3201. if (label_sgi == current_label_type) {
  3202. puts(_("Command action"));
  3203. puts(_("\tb\tmove beginning of data in a partition")); /* !sun */
  3204. puts(_("\tc\tchange number of cylinders"));
  3205. puts(_("\td\tprint the raw data in the partition table"));
  3206. puts(_("\te\tlist extended partitions")); /* !sun */
  3207. puts(_("\tg\tcreate an IRIX (SGI) partition table"));/* sgi */
  3208. puts(_("\th\tchange number of heads"));
  3209. puts(_("\tm\tprint this menu"));
  3210. puts(_("\tp\tprint the partition table"));
  3211. puts(_("\tq\tquit without saving changes"));
  3212. puts(_("\tr\treturn to main menu"));
  3213. puts(_("\ts\tchange number of sectors/track"));
  3214. puts(_("\tv\tverify the partition table"));
  3215. puts(_("\tw\twrite table to disk and exit"));
  3216. } else
  3217. #endif
  3218. #ifdef CONFIG_FEATURE_AIX_LABEL
  3219. if (label_aix == current_label_type) {
  3220. puts(_("Command action"));
  3221. puts(_("\tb\tmove beginning of data in a partition")); /* !sun */
  3222. puts(_("\tc\tchange number of cylinders"));
  3223. puts(_("\td\tprint the raw data in the partition table"));
  3224. puts(_("\te\tlist extended partitions")); /* !sun */
  3225. puts(_("\tg\tcreate an IRIX (SGI) partition table"));/* sgi */
  3226. puts(_("\th\tchange number of heads"));
  3227. puts(_("\tm\tprint this menu"));
  3228. puts(_("\tp\tprint the partition table"));
  3229. puts(_("\tq\tquit without saving changes"));
  3230. puts(_("\tr\treturn to main menu"));
  3231. puts(_("\ts\tchange number of sectors/track"));
  3232. puts(_("\tv\tverify the partition table"));
  3233. puts(_("\tw\twrite table to disk and exit"));
  3234. } else
  3235. #endif
  3236. {
  3237. puts(_("Command action"));
  3238. puts(_("\tb\tmove beginning of data in a partition")); /* !sun */
  3239. puts(_("\tc\tchange number of cylinders"));
  3240. puts(_("\td\tprint the raw data in the partition table"));
  3241. puts(_("\te\tlist extended partitions")); /* !sun */
  3242. puts(_("\tf\tfix partition order")); /* !sun, !aix, !sgi */
  3243. #ifdef CONFIG_FEATURE_SGI_LABEL
  3244. puts(_("\tg\tcreate an IRIX (SGI) partition table"));/* sgi */
  3245. #endif
  3246. puts(_("\th\tchange number of heads"));
  3247. puts(_("\tm\tprint this menu"));
  3248. puts(_("\tp\tprint the partition table"));
  3249. puts(_("\tq\tquit without saving changes"));
  3250. puts(_("\tr\treturn to main menu"));
  3251. puts(_("\ts\tchange number of sectors/track"));
  3252. puts(_("\tv\tverify the partition table"));
  3253. puts(_("\tw\twrite table to disk and exit"));
  3254. }
  3255. }
  3256. #endif /* ADVANCED mode */
  3257. #ifdef CONFIG_FEATURE_FDISK_WRITABLE
  3258. static const struct systypes *
  3259. get_sys_types(void)
  3260. {
  3261. return (
  3262. #ifdef CONFIG_FEATURE_SUN_LABEL
  3263. label_sun == current_label_type ? sun_sys_types :
  3264. #endif
  3265. #ifdef CONFIG_FEATURE_SGI_LABEL
  3266. label_sgi == current_label_type ? sgi_sys_types :
  3267. #endif
  3268. i386_sys_types);
  3269. }
  3270. #else
  3271. #define get_sys_types() i386_sys_types
  3272. #endif /* CONFIG_FEATURE_FDISK_WRITABLE */
  3273. static const char *partition_type(unsigned char type)
  3274. {
  3275. int i;
  3276. const struct systypes *types = get_sys_types();
  3277. for (i = 0; types[i].name; i++)
  3278. if ((unsigned char )types[i].name[0] == type)
  3279. return types[i].name + 1;
  3280. return _("Unknown");
  3281. }
  3282. #ifdef CONFIG_FEATURE_FDISK_WRITABLE
  3283. static int
  3284. get_sysid(int i)
  3285. {
  3286. return (
  3287. #ifdef CONFIG_FEATURE_SUN_LABEL
  3288. label_sun == current_label_type ? sunlabel->infos[i].id :
  3289. #endif
  3290. #ifdef CONFIG_FEATURE_SGI_LABEL
  3291. label_sgi == current_label_type ? sgi_get_sysid(i) :
  3292. #endif
  3293. ptes[i].part_table->sys_ind);
  3294. }
  3295. void list_types(const struct systypes *sys)
  3296. {
  3297. uint last[4], done = 0, next = 0, size;
  3298. int i;
  3299. for (i = 0; sys[i].name; i++);
  3300. size = i;
  3301. for (i = 3; i >= 0; i--)
  3302. last[3 - i] = done += (size + i - done) / (i + 1);
  3303. i = done = 0;
  3304. do {
  3305. printf("%c%2x %-15.15s", i ? ' ' : '\n',
  3306. (unsigned char)sys[next].name[0],
  3307. partition_type((unsigned char)sys[next].name[0]));
  3308. next = last[i++] + done;
  3309. if (i > 3 || next >= last[i]) {
  3310. i = 0;
  3311. next = ++done;
  3312. }
  3313. } while (done < last[0]);
  3314. putchar('\n');
  3315. }
  3316. #endif /* CONFIG_FEATURE_FDISK_WRITABLE */
  3317. static int
  3318. is_cleared_partition(const struct partition *p)
  3319. {
  3320. return !(!p || p->boot_ind || p->head || p->sector || p->cyl ||
  3321. p->sys_ind || p->end_head || p->end_sector || p->end_cyl ||
  3322. get_start_sect(p) || get_nr_sects(p));
  3323. }
  3324. static void
  3325. clear_partition(struct partition *p)
  3326. {
  3327. if (!p)
  3328. return;
  3329. memset(p, 0, sizeof(struct partition));
  3330. }
  3331. #ifdef CONFIG_FEATURE_FDISK_WRITABLE
  3332. static void
  3333. set_partition(int i, int doext, off_t start, off_t stop, int sysid)
  3334. {
  3335. struct partition *p;
  3336. off_t offset;
  3337. if (doext) {
  3338. p = ptes[i].ext_pointer;
  3339. offset = extended_offset;
  3340. } else {
  3341. p = ptes[i].part_table;
  3342. offset = ptes[i].offset;
  3343. }
  3344. p->boot_ind = 0;
  3345. p->sys_ind = sysid;
  3346. set_start_sect(p, start - offset);
  3347. set_nr_sects(p, stop - start + 1);
  3348. if (dos_compatible_flag && (start/(sectors*heads) > 1023))
  3349. start = heads*sectors*1024 - 1;
  3350. set_hsc(p->head, p->sector, p->cyl, start);
  3351. if (dos_compatible_flag && (stop/(sectors*heads) > 1023))
  3352. stop = heads*sectors*1024 - 1;
  3353. set_hsc(p->end_head, p->end_sector, p->end_cyl, stop);
  3354. ptes[i].changed = 1;
  3355. }
  3356. #endif
  3357. static int
  3358. test_c(const char **m, const char *mesg)
  3359. {
  3360. int val = 0;
  3361. if (!*m)
  3362. fprintf(stderr, _("You must set"));
  3363. else {
  3364. fprintf(stderr, " %s", *m);
  3365. val = 1;
  3366. }
  3367. *m = mesg;
  3368. return val;
  3369. }
  3370. static int
  3371. warn_geometry(void)
  3372. {
  3373. const char *m = NULL;
  3374. int prev = 0;
  3375. if (!heads)
  3376. prev = test_c(&m, _("heads"));
  3377. if (!sectors)
  3378. prev = test_c(&m, _("sectors"));
  3379. if (!cylinders)
  3380. prev = test_c(&m, _("cylinders"));
  3381. if (!m)
  3382. return 0;
  3383. fprintf(stderr, "%s%s.\n"
  3384. #ifdef CONFIG_FEATURE_FDISK_WRITABLE
  3385. "You can do this from the extra functions menu.\n"
  3386. #endif
  3387. , prev ? _(" and ") : " ", m);
  3388. return 1;
  3389. }
  3390. static void update_units(void)
  3391. {
  3392. int cyl_units = heads * sectors;
  3393. if (display_in_cyl_units && cyl_units)
  3394. units_per_sector = cyl_units;
  3395. else
  3396. units_per_sector = 1; /* in sectors */
  3397. }
  3398. #ifdef CONFIG_FEATURE_FDISK_WRITABLE
  3399. static void
  3400. warn_cylinders(void)
  3401. {
  3402. if (label_dos == current_label_type && cylinders > 1024 && !nowarn)
  3403. fprintf(stderr, _("\n"
  3404. "The number of cylinders for this disk is set to %d.\n"
  3405. "There is nothing wrong with that, but this is larger than 1024,\n"
  3406. "and could in certain setups cause problems with:\n"
  3407. "1) software that runs at boot time (e.g., old versions of LILO)\n"
  3408. "2) booting and partitioning software from other OSs\n"
  3409. " (e.g., DOS FDISK, OS/2 FDISK)\n"),
  3410. cylinders);
  3411. }
  3412. #endif
  3413. static void
  3414. read_extended(int ext)
  3415. {
  3416. int i;
  3417. struct pte *pex;
  3418. struct partition *p, *q;
  3419. ext_index = ext;
  3420. pex = &ptes[ext];
  3421. pex->ext_pointer = pex->part_table;
  3422. p = pex->part_table;
  3423. if (!get_start_sect(p)) {
  3424. fprintf(stderr,
  3425. _("Bad offset in primary extended partition\n"));
  3426. return;
  3427. }
  3428. while (IS_EXTENDED(p->sys_ind)) {
  3429. struct pte *pe = &ptes[partitions];
  3430. if (partitions >= MAXIMUM_PARTS) {
  3431. /* This is not a Linux restriction, but
  3432. this program uses arrays of size MAXIMUM_PARTS.
  3433. Do not try to `improve' this test. */
  3434. struct pte *pre = &ptes[partitions-1];
  3435. #ifdef CONFIG_FEATURE_FDISK_WRITABLE
  3436. fprintf(stderr,
  3437. _("Warning: deleting partitions after %d\n"),
  3438. partitions);
  3439. pre->changed = 1;
  3440. #endif
  3441. clear_partition(pre->ext_pointer);
  3442. return;
  3443. }
  3444. read_pte(pe, extended_offset + get_start_sect(p));
  3445. if (!extended_offset)
  3446. extended_offset = get_start_sect(p);
  3447. q = p = pt_offset(pe->sectorbuffer, 0);
  3448. for (i = 0; i < 4; i++, p++) if (get_nr_sects(p)) {
  3449. if (IS_EXTENDED(p->sys_ind)) {
  3450. if (pe->ext_pointer)
  3451. fprintf(stderr,
  3452. _("Warning: extra link "
  3453. "pointer in partition table"
  3454. " %d\n"), partitions + 1);
  3455. else
  3456. pe->ext_pointer = p;
  3457. } else if (p->sys_ind) {
  3458. if (pe->part_table)
  3459. fprintf(stderr,
  3460. _("Warning: ignoring extra "
  3461. "data in partition table"
  3462. " %d\n"), partitions + 1);
  3463. else
  3464. pe->part_table = p;
  3465. }
  3466. }
  3467. /* very strange code here... */
  3468. if (!pe->part_table) {
  3469. if (q != pe->ext_pointer)
  3470. pe->part_table = q;
  3471. else
  3472. pe->part_table = q + 1;
  3473. }
  3474. if (!pe->ext_pointer) {
  3475. if (q != pe->part_table)
  3476. pe->ext_pointer = q;
  3477. else
  3478. pe->ext_pointer = q + 1;
  3479. }
  3480. p = pe->ext_pointer;
  3481. partitions++;
  3482. }
  3483. #ifdef CONFIG_FEATURE_FDISK_WRITABLE
  3484. /* remove empty links */
  3485. remove:
  3486. for (i = 4; i < partitions; i++) {
  3487. struct pte *pe = &ptes[i];
  3488. if (!get_nr_sects(pe->part_table) &&
  3489. (partitions > 5 || ptes[4].part_table->sys_ind)) {
  3490. printf("omitting empty partition (%d)\n", i+1);
  3491. delete_partition(i);
  3492. goto remove; /* numbering changed */
  3493. }
  3494. }
  3495. #endif
  3496. }
  3497. #ifdef CONFIG_FEATURE_FDISK_WRITABLE
  3498. static void
  3499. create_doslabel(void)
  3500. {
  3501. int i;
  3502. fprintf(stderr,
  3503. _("Building a new DOS disklabel. Changes will remain in memory only,\n"
  3504. "until you decide to write them. After that, of course, the previous\n"
  3505. "content won't be recoverable.\n\n"));
  3506. current_label_type = label_dos;
  3507. #ifdef CONFIG_FEATURE_OSF_LABEL
  3508. possibly_osf_label = 0;
  3509. #endif
  3510. partitions = 4;
  3511. for (i = 510-64; i < 510; i++)
  3512. MBRbuffer[i] = 0;
  3513. write_part_table_flag(MBRbuffer);
  3514. extended_offset = 0;
  3515. set_all_unchanged();
  3516. set_changed(0);
  3517. get_boot(create_empty_dos);
  3518. }
  3519. #endif /* CONFIG_FEATURE_FDISK_WRITABLE */
  3520. static void
  3521. get_sectorsize(void)
  3522. {
  3523. if (!user_set_sector_size) {
  3524. int arg;
  3525. if (ioctl(fd, BLKSSZGET, &arg) == 0)
  3526. sector_size = arg;
  3527. if (sector_size != DEFAULT_SECTOR_SIZE)
  3528. printf(_("Note: sector size is %d (not %d)\n"),
  3529. sector_size, DEFAULT_SECTOR_SIZE);
  3530. }
  3531. }
  3532. static inline void
  3533. get_kernel_geometry(void)
  3534. {
  3535. struct hd_geometry geometry;
  3536. if (!ioctl(fd, HDIO_GETGEO, &geometry)) {
  3537. kern_heads = geometry.heads;
  3538. kern_sectors = geometry.sectors;
  3539. /* never use geometry.cylinders - it is truncated */
  3540. }
  3541. }
  3542. static void
  3543. get_partition_table_geometry(void)
  3544. {
  3545. const unsigned char *bufp = (const unsigned char *)MBRbuffer;
  3546. struct partition *p;
  3547. int i, h, s, hh, ss;
  3548. int first = 1;
  3549. int bad = 0;
  3550. if (!(valid_part_table_flag((char*)bufp)))
  3551. return;
  3552. hh = ss = 0;
  3553. for (i = 0; i < 4; i++) {
  3554. p = pt_offset(bufp, i);
  3555. if (p->sys_ind != 0) {
  3556. h = p->end_head + 1;
  3557. s = (p->end_sector & 077);
  3558. if (first) {
  3559. hh = h;
  3560. ss = s;
  3561. first = 0;
  3562. } else if (hh != h || ss != s)
  3563. bad = 1;
  3564. }
  3565. }
  3566. if (!first && !bad) {
  3567. pt_heads = hh;
  3568. pt_sectors = ss;
  3569. }
  3570. }
  3571. static void
  3572. get_geometry(void)
  3573. {
  3574. int sec_fac;
  3575. unsigned long long bytes; /* really u64 */
  3576. get_sectorsize();
  3577. sec_fac = sector_size / 512;
  3578. #ifdef CONFIG_FEATURE_SUN_LABEL
  3579. guess_device_type();
  3580. #endif
  3581. heads = cylinders = sectors = 0;
  3582. kern_heads = kern_sectors = 0;
  3583. pt_heads = pt_sectors = 0;
  3584. get_kernel_geometry();
  3585. get_partition_table_geometry();
  3586. heads = user_heads ? user_heads :
  3587. pt_heads ? pt_heads :
  3588. kern_heads ? kern_heads : 255;
  3589. sectors = user_sectors ? user_sectors :
  3590. pt_sectors ? pt_sectors :
  3591. kern_sectors ? kern_sectors : 63;
  3592. if (ioctl(fd, BLKGETSIZE64, &bytes) == 0) {
  3593. /* got bytes */
  3594. } else {
  3595. unsigned long longsectors;
  3596. if (ioctl(fd, BLKGETSIZE, &longsectors))
  3597. longsectors = 0;
  3598. bytes = ((unsigned long long) longsectors) << 9;
  3599. }
  3600. total_number_of_sectors = (bytes >> 9);
  3601. sector_offset = 1;
  3602. if (dos_compatible_flag)
  3603. sector_offset = sectors;
  3604. cylinders = total_number_of_sectors / (heads * sectors * sec_fac);
  3605. if (!cylinders)
  3606. cylinders = user_cylinders;
  3607. }
  3608. /*
  3609. * Read MBR. Returns:
  3610. * -1: no 0xaa55 flag present (possibly entire disk BSD)
  3611. * 0: found or created label
  3612. * 1: I/O error
  3613. */
  3614. static int
  3615. get_boot(enum action what)
  3616. {
  3617. int i;
  3618. partitions = 4;
  3619. for (i = 0; i < 4; i++) {
  3620. struct pte *pe = &ptes[i];
  3621. pe->part_table = pt_offset(MBRbuffer, i);
  3622. pe->ext_pointer = NULL;
  3623. pe->offset = 0;
  3624. pe->sectorbuffer = MBRbuffer;
  3625. #ifdef CONFIG_FEATURE_FDISK_WRITABLE
  3626. pe->changed = (what == create_empty_dos);
  3627. #endif
  3628. }
  3629. #ifdef CONFIG_FEATURE_SUN_LABEL
  3630. if (what == create_empty_sun && check_sun_label())
  3631. return 0;
  3632. #endif
  3633. memset(MBRbuffer, 0, 512);
  3634. #ifdef CONFIG_FEATURE_FDISK_WRITABLE
  3635. if (what == create_empty_dos)
  3636. goto got_dos_table; /* skip reading disk */
  3637. if ((fd = open(disk_device, type_open)) < 0) {
  3638. if ((fd = open(disk_device, O_RDONLY)) < 0) {
  3639. if (what == try_only)
  3640. return 1;
  3641. fdisk_fatal(unable_to_open);
  3642. } else
  3643. printf(_("You will not be able to write "
  3644. "the partition table.\n"));
  3645. }
  3646. if (512 != read(fd, MBRbuffer, 512)) {
  3647. if (what == try_only)
  3648. return 1;
  3649. fdisk_fatal(unable_to_read);
  3650. }
  3651. #else
  3652. if ((fd = open(disk_device, O_RDONLY)) < 0)
  3653. return 1;
  3654. if (512 != read(fd, MBRbuffer, 512))
  3655. return 1;
  3656. #endif
  3657. get_geometry();
  3658. update_units();
  3659. #ifdef CONFIG_FEATURE_SUN_LABEL
  3660. if (check_sun_label())
  3661. return 0;
  3662. #endif
  3663. #ifdef CONFIG_FEATURE_SGI_LABEL
  3664. if (check_sgi_label())
  3665. return 0;
  3666. #endif
  3667. #ifdef CONFIG_FEATURE_AIX_LABEL
  3668. if (check_aix_label())
  3669. return 0;
  3670. #endif
  3671. #ifdef CONFIG_FEATURE_OSF_LABEL
  3672. if (check_osf_label()) {
  3673. possibly_osf_label = 1;
  3674. if (!valid_part_table_flag(MBRbuffer)) {
  3675. current_label_type = label_osf;
  3676. return 0;
  3677. }
  3678. printf(_("This disk has both DOS and BSD magic.\n"
  3679. "Give the 'b' command to go to BSD mode.\n"));
  3680. }
  3681. #endif
  3682. #ifdef CONFIG_FEATURE_FDISK_WRITABLE
  3683. got_dos_table:
  3684. #endif
  3685. if (!valid_part_table_flag(MBRbuffer)) {
  3686. #ifndef CONFIG_FEATURE_FDISK_WRITABLE
  3687. return -1;
  3688. #else
  3689. switch (what) {
  3690. case fdisk:
  3691. fprintf(stderr,
  3692. _("Device contains neither a valid DOS "
  3693. "partition table, nor Sun, SGI or OSF "
  3694. "disklabel\n"));
  3695. #ifdef __sparc__
  3696. #ifdef CONFIG_FEATURE_SUN_LABEL
  3697. create_sunlabel();
  3698. #endif
  3699. #else
  3700. create_doslabel();
  3701. #endif
  3702. return 0;
  3703. case try_only:
  3704. return -1;
  3705. case create_empty_dos:
  3706. #ifdef CONFIG_FEATURE_SUN_LABEL
  3707. case create_empty_sun:
  3708. #endif
  3709. break;
  3710. default:
  3711. fprintf(stderr, _("Internal error\n"));
  3712. exit(1);
  3713. }
  3714. #endif /* CONFIG_FEATURE_FDISK_WRITABLE */
  3715. }
  3716. #ifdef CONFIG_FEATURE_FDISK_WRITABLE
  3717. warn_cylinders();
  3718. #endif
  3719. warn_geometry();
  3720. for (i = 0; i < 4; i++) {
  3721. struct pte *pe = &ptes[i];
  3722. if (IS_EXTENDED(pe->part_table->sys_ind)) {
  3723. if (partitions != 4)
  3724. fprintf(stderr, _("Ignoring extra extended "
  3725. "partition %d\n"), i + 1);
  3726. else
  3727. read_extended(i);
  3728. }
  3729. }
  3730. for (i = 3; i < partitions; i++) {
  3731. struct pte *pe = &ptes[i];
  3732. if (!valid_part_table_flag(pe->sectorbuffer)) {
  3733. fprintf(stderr,
  3734. _("Warning: invalid flag 0x%04x of partition "
  3735. "table %d will be corrected by w(rite)\n"),
  3736. part_table_flag(pe->sectorbuffer), i + 1);
  3737. #ifdef CONFIG_FEATURE_FDISK_WRITABLE
  3738. pe->changed = 1;
  3739. #endif
  3740. }
  3741. }
  3742. return 0;
  3743. }
  3744. #ifdef CONFIG_FEATURE_FDISK_WRITABLE
  3745. /*
  3746. * Print the message MESG, then read an integer between LOW and HIGH (inclusive).
  3747. * If the user hits Enter, DFLT is returned.
  3748. * Answers like +10 are interpreted as offsets from BASE.
  3749. *
  3750. * There is no default if DFLT is not between LOW and HIGH.
  3751. */
  3752. static uint
  3753. read_int(uint low, uint dflt, uint high, uint base, char *mesg)
  3754. {
  3755. uint i;
  3756. int default_ok = 1;
  3757. static char *ms = NULL;
  3758. static int mslen = 0;
  3759. if (!ms || strlen(mesg)+100 > mslen) {
  3760. mslen = strlen(mesg)+200;
  3761. ms = xrealloc(ms,mslen);
  3762. }
  3763. if (dflt < low || dflt > high)
  3764. default_ok = 0;
  3765. if (default_ok)
  3766. snprintf(ms, mslen, _("%s (%u-%u, default %u): "),
  3767. mesg, low, high, dflt);
  3768. else
  3769. snprintf(ms, mslen, "%s (%u-%u): ", mesg, low, high);
  3770. while (1) {
  3771. int use_default = default_ok;
  3772. /* ask question and read answer */
  3773. while (read_chars(ms) != '\n' && !isdigit(*line_ptr)
  3774. && *line_ptr != '-' && *line_ptr != '+')
  3775. continue;
  3776. if (*line_ptr == '+' || *line_ptr == '-') {
  3777. int minus = (*line_ptr == '-');
  3778. int absolute = 0;
  3779. i = atoi(line_ptr+1);
  3780. while (isdigit(*++line_ptr))
  3781. use_default = 0;
  3782. switch (*line_ptr) {
  3783. case 'c':
  3784. case 'C':
  3785. if (!display_in_cyl_units)
  3786. i *= heads * sectors;
  3787. break;
  3788. case 'K':
  3789. absolute = 1024;
  3790. break;
  3791. case 'k':
  3792. absolute = 1000;
  3793. break;
  3794. case 'm':
  3795. case 'M':
  3796. absolute = 1000000;
  3797. break;
  3798. case 'g':
  3799. case 'G':
  3800. absolute = 1000000000;
  3801. break;
  3802. default:
  3803. break;
  3804. }
  3805. if (absolute) {
  3806. unsigned long long bytes;
  3807. unsigned long unit;
  3808. bytes = (unsigned long long) i * absolute;
  3809. unit = sector_size * units_per_sector;
  3810. bytes += unit/2; /* round */
  3811. bytes /= unit;
  3812. i = bytes;
  3813. }
  3814. if (minus)
  3815. i = -i;
  3816. i += base;
  3817. } else {
  3818. i = atoi(line_ptr);
  3819. while (isdigit(*line_ptr)) {
  3820. line_ptr++;
  3821. use_default = 0;
  3822. }
  3823. }
  3824. if (use_default)
  3825. printf(_("Using default value %u\n"), i = dflt);
  3826. if (i >= low && i <= high)
  3827. break;
  3828. else
  3829. printf(_("Value out of range.\n"));
  3830. }
  3831. return i;
  3832. }
  3833. static int
  3834. get_partition(int warn, int max)
  3835. {
  3836. struct pte *pe;
  3837. int i;
  3838. i = read_int(1, 0, max, 0, _("Partition number")) - 1;
  3839. pe = &ptes[i];
  3840. if (warn) {
  3841. if (
  3842. (
  3843. label_sun != current_label_type &&
  3844. label_sgi != current_label_type &&
  3845. !pe->part_table->sys_ind
  3846. )
  3847. #ifdef CONFIG_FEATURE_SUN_LABEL
  3848. || (
  3849. label_sun == current_label_type &&
  3850. (
  3851. !sunlabel->partitions[i].num_sectors
  3852. || !sunlabel->infos[i].id
  3853. )
  3854. )
  3855. #endif
  3856. #ifdef CONFIG_FEATURE_SGI_LABEL
  3857. || (
  3858. label_sgi == current_label_type &&
  3859. !sgi_get_num_sectors(i)
  3860. )
  3861. #endif
  3862. ){
  3863. fprintf(stderr,
  3864. _("Warning: partition %d has empty type\n"),
  3865. i+1
  3866. );
  3867. }
  3868. }
  3869. return i;
  3870. }
  3871. static int
  3872. get_existing_partition(int warn, int max)
  3873. {
  3874. int pno = -1;
  3875. int i;
  3876. for (i = 0; i < max; i++) {
  3877. struct pte *pe = &ptes[i];
  3878. struct partition *p = pe->part_table;
  3879. if (p && !is_cleared_partition(p)) {
  3880. if (pno >= 0)
  3881. goto not_unique;
  3882. pno = i;
  3883. }
  3884. }
  3885. if (pno >= 0) {
  3886. printf(_("Selected partition %d\n"), pno+1);
  3887. return pno;
  3888. }
  3889. printf(_("No partition is defined yet!\n"));
  3890. return -1;
  3891. not_unique:
  3892. return get_partition(warn, max);
  3893. }
  3894. static int
  3895. get_nonexisting_partition(int warn, int max)
  3896. {
  3897. int pno = -1;
  3898. int i;
  3899. for (i = 0; i < max; i++) {
  3900. struct pte *pe = &ptes[i];
  3901. struct partition *p = pe->part_table;
  3902. if (p && is_cleared_partition(p)) {
  3903. if (pno >= 0)
  3904. goto not_unique;
  3905. pno = i;
  3906. }
  3907. }
  3908. if (pno >= 0) {
  3909. printf(_("Selected partition %d\n"), pno+1);
  3910. return pno;
  3911. }
  3912. printf(_("All primary partitions have been defined already!\n"));
  3913. return -1;
  3914. not_unique:
  3915. return get_partition(warn, max);
  3916. }
  3917. void change_units(void)
  3918. {
  3919. display_in_cyl_units = !display_in_cyl_units;
  3920. update_units();
  3921. printf(_("Changing display/entry units to %s\n"),
  3922. str_units(PLURAL));
  3923. }
  3924. static void
  3925. toggle_active(int i)
  3926. {
  3927. struct pte *pe = &ptes[i];
  3928. struct partition *p = pe->part_table;
  3929. if (IS_EXTENDED(p->sys_ind) && !p->boot_ind)
  3930. fprintf(stderr,
  3931. _("WARNING: Partition %d is an extended partition\n"),
  3932. i + 1);
  3933. p->boot_ind = (p->boot_ind ? 0 : ACTIVE_FLAG);
  3934. pe->changed = 1;
  3935. }
  3936. static void
  3937. toggle_dos_compatibility_flag(void)
  3938. {
  3939. dos_compatible_flag = ~dos_compatible_flag;
  3940. if (dos_compatible_flag) {
  3941. sector_offset = sectors;
  3942. printf(_("DOS Compatibility flag is set\n"));
  3943. }
  3944. else {
  3945. sector_offset = 1;
  3946. printf(_("DOS Compatibility flag is not set\n"));
  3947. }
  3948. }
  3949. static void
  3950. delete_partition(int i)
  3951. {
  3952. struct pte *pe = &ptes[i];
  3953. struct partition *p = pe->part_table;
  3954. struct partition *q = pe->ext_pointer;
  3955. /* Note that for the fifth partition (i == 4) we don't actually
  3956. * decrement partitions.
  3957. */
  3958. if (warn_geometry())
  3959. return; /* C/H/S not set */
  3960. pe->changed = 1;
  3961. #ifdef CONFIG_FEATURE_SUN_LABEL
  3962. if (label_sun == current_label_type) {
  3963. sun_delete_partition(i);
  3964. return;
  3965. }
  3966. #endif
  3967. #ifdef CONFIG_FEATURE_SGI_LABEL
  3968. if (label_sgi == current_label_type) {
  3969. sgi_delete_partition(i);
  3970. return;
  3971. }
  3972. #endif
  3973. if (i < 4) {
  3974. if (IS_EXTENDED(p->sys_ind) && i == ext_index) {
  3975. partitions = 4;
  3976. ptes[ext_index].ext_pointer = NULL;
  3977. extended_offset = 0;
  3978. }
  3979. clear_partition(p);
  3980. return;
  3981. }
  3982. if (!q->sys_ind && i > 4) {
  3983. /* the last one in the chain - just delete */
  3984. --partitions;
  3985. --i;
  3986. clear_partition(ptes[i].ext_pointer);
  3987. ptes[i].changed = 1;
  3988. } else {
  3989. /* not the last one - further ones will be moved down */
  3990. if (i > 4) {
  3991. /* delete this link in the chain */
  3992. p = ptes[i-1].ext_pointer;
  3993. *p = *q;
  3994. set_start_sect(p, get_start_sect(q));
  3995. set_nr_sects(p, get_nr_sects(q));
  3996. ptes[i-1].changed = 1;
  3997. } else if (partitions > 5) { /* 5 will be moved to 4 */
  3998. /* the first logical in a longer chain */
  3999. pe = &ptes[5];
  4000. if (pe->part_table) /* prevent SEGFAULT */
  4001. set_start_sect(pe->part_table,
  4002. get_partition_start(pe) -
  4003. extended_offset);
  4004. pe->offset = extended_offset;
  4005. pe->changed = 1;
  4006. }
  4007. if (partitions > 5) {
  4008. partitions--;
  4009. while (i < partitions) {
  4010. ptes[i] = ptes[i+1];
  4011. i++;
  4012. }
  4013. } else
  4014. /* the only logical: clear only */
  4015. clear_partition(ptes[i].part_table);
  4016. }
  4017. }
  4018. static void
  4019. change_sysid(void)
  4020. {
  4021. int i, sys, origsys;
  4022. struct partition *p;
  4023. #ifdef CONFIG_FEATURE_SGI_LABEL
  4024. /* If sgi_label then don't use get_existing_partition,
  4025. let the user select a partition, since get_existing_partition()
  4026. only works for Linux like partition tables. */
  4027. if (label_sgi != current_label_type) {
  4028. i = get_existing_partition(0, partitions);
  4029. } else {
  4030. i = get_partition(0, partitions);
  4031. }
  4032. #else
  4033. i = get_existing_partition(0, partitions);
  4034. #endif
  4035. if (i == -1)
  4036. return;
  4037. p = ptes[i].part_table;
  4038. origsys = sys = get_sysid(i);
  4039. /* if changing types T to 0 is allowed, then
  4040. the reverse change must be allowed, too */
  4041. if (!sys && label_sgi != current_label_type &&
  4042. label_sun != current_label_type && !get_nr_sects(p))
  4043. {
  4044. printf(_("Partition %d does not exist yet!\n"), i + 1);
  4045. }else{
  4046. while (1) {
  4047. sys = read_hex (get_sys_types());
  4048. if (!sys && label_sgi != current_label_type &&
  4049. label_sun != current_label_type)
  4050. {
  4051. printf(_("Type 0 means free space to many systems\n"
  4052. "(but not to Linux). Having partitions of\n"
  4053. "type 0 is probably unwise. You can delete\n"
  4054. "a partition using the `d' command.\n"));
  4055. /* break; */
  4056. }
  4057. if (label_sun != current_label_type && label_sgi != current_label_type) {
  4058. if (IS_EXTENDED(sys) != IS_EXTENDED(p->sys_ind)) {
  4059. printf(_("You cannot change a partition into"
  4060. " an extended one or vice versa\n"
  4061. "Delete it first.\n"));
  4062. break;
  4063. }
  4064. }
  4065. if (sys < 256) {
  4066. #ifdef CONFIG_FEATURE_SUN_LABEL
  4067. if (label_sun == current_label_type && i == 2 && sys != WHOLE_DISK)
  4068. printf(_("Consider leaving partition 3 "
  4069. "as Whole disk (5),\n"
  4070. "as SunOS/Solaris expects it and "
  4071. "even Linux likes it.\n\n"));
  4072. #endif
  4073. #ifdef CONFIG_FEATURE_SGI_LABEL
  4074. if (label_sgi == current_label_type &&
  4075. (
  4076. (i == 10 && sys != ENTIRE_DISK) ||
  4077. (i == 8 && sys != 0)
  4078. )
  4079. ){
  4080. printf(_("Consider leaving partition 9 "
  4081. "as volume header (0),\nand "
  4082. "partition 11 as entire volume (6)"
  4083. "as IRIX expects it.\n\n"));
  4084. }
  4085. #endif
  4086. if (sys == origsys)
  4087. break;
  4088. #ifdef CONFIG_FEATURE_SUN_LABEL
  4089. if (label_sun == current_label_type) {
  4090. sun_change_sysid(i, sys);
  4091. } else
  4092. #endif
  4093. #ifdef CONFIG_FEATURE_SGI_LABEL
  4094. if (label_sgi == current_label_type) {
  4095. sgi_change_sysid(i, sys);
  4096. } else
  4097. #endif
  4098. p->sys_ind = sys;
  4099. printf(_("Changed system type of partition %d "
  4100. "to %x (%s)\n"), i + 1, sys,
  4101. partition_type(sys));
  4102. ptes[i].changed = 1;
  4103. if (is_dos_partition(origsys) ||
  4104. is_dos_partition(sys))
  4105. dos_changed = 1;
  4106. break;
  4107. }
  4108. }
  4109. }
  4110. }
  4111. #endif /* CONFIG_FEATURE_FDISK_WRITABLE */
  4112. /* check_consistency() and long2chs() added Sat Mar 6 12:28:16 1993,
  4113. * faith@cs.unc.edu, based on code fragments from pfdisk by Gordon W. Ross,
  4114. * Jan. 1990 (version 1.2.1 by Gordon W. Ross Aug. 1990; Modified by S.
  4115. * Lubkin Oct. 1991). */
  4116. static void
  4117. long2chs(ulong ls, uint *c, uint *h, uint *s)
  4118. {
  4119. int spc = heads * sectors;
  4120. *c = ls / spc;
  4121. ls = ls % spc;
  4122. *h = ls / sectors;
  4123. *s = ls % sectors + 1; /* sectors count from 1 */
  4124. }
  4125. static void
  4126. check_consistency(const struct partition *p, int partition)
  4127. {
  4128. uint pbc, pbh, pbs; /* physical beginning c, h, s */
  4129. uint pec, peh, pes; /* physical ending c, h, s */
  4130. uint lbc, lbh, lbs; /* logical beginning c, h, s */
  4131. uint lec, leh, les; /* logical ending c, h, s */
  4132. if (!heads || !sectors || (partition >= 4))
  4133. return; /* do not check extended partitions */
  4134. /* physical beginning c, h, s */
  4135. pbc = (p->cyl & 0xff) | ((p->sector << 2) & 0x300);
  4136. pbh = p->head;
  4137. pbs = p->sector & 0x3f;
  4138. /* physical ending c, h, s */
  4139. pec = (p->end_cyl & 0xff) | ((p->end_sector << 2) & 0x300);
  4140. peh = p->end_head;
  4141. pes = p->end_sector & 0x3f;
  4142. /* compute logical beginning (c, h, s) */
  4143. long2chs(get_start_sect(p), &lbc, &lbh, &lbs);
  4144. /* compute logical ending (c, h, s) */
  4145. long2chs(get_start_sect(p) + get_nr_sects(p) - 1, &lec, &leh, &les);
  4146. /* Same physical / logical beginning? */
  4147. if (cylinders <= 1024 && (pbc != lbc || pbh != lbh || pbs != lbs)) {
  4148. printf(_("Partition %d has different physical/logical "
  4149. "beginnings (non-Linux?):\n"), partition + 1);
  4150. printf(_(" phys=(%d, %d, %d) "), pbc, pbh, pbs);
  4151. printf(_("logical=(%d, %d, %d)\n"),lbc, lbh, lbs);
  4152. }
  4153. /* Same physical / logical ending? */
  4154. if (cylinders <= 1024 && (pec != lec || peh != leh || pes != les)) {
  4155. printf(_("Partition %d has different physical/logical "
  4156. "endings:\n"), partition + 1);
  4157. printf(_(" phys=(%d, %d, %d) "), pec, peh, pes);
  4158. printf(_("logical=(%d, %d, %d)\n"),lec, leh, les);
  4159. }
  4160. #if 0
  4161. /* Beginning on cylinder boundary? */
  4162. if (pbh != !pbc || pbs != 1) {
  4163. printf(_("Partition %i does not start on cylinder "
  4164. "boundary:\n"), partition + 1);
  4165. printf(_(" phys=(%d, %d, %d) "), pbc, pbh, pbs);
  4166. printf(_("should be (%d, %d, 1)\n"), pbc, !pbc);
  4167. }
  4168. #endif
  4169. /* Ending on cylinder boundary? */
  4170. if (peh != (heads - 1) || pes != sectors) {
  4171. printf(_("Partition %i does not end on cylinder boundary.\n"),
  4172. partition + 1);
  4173. #if 0
  4174. printf(_(" phys=(%d, %d, %d) "), pec, peh, pes);
  4175. printf(_("should be (%d, %d, %d)\n"),
  4176. pec, heads - 1, sectors);
  4177. #endif
  4178. }
  4179. }
  4180. static void
  4181. list_disk_geometry(void)
  4182. {
  4183. long long bytes = (total_number_of_sectors << 9);
  4184. long megabytes = bytes/1000000;
  4185. if (megabytes < 10000)
  4186. printf(_("\nDisk %s: %ld MB, %lld bytes\n"),
  4187. disk_device, megabytes, bytes);
  4188. else
  4189. printf(_("\nDisk %s: %ld.%ld GB, %lld bytes\n"),
  4190. disk_device, megabytes/1000, (megabytes/100)%10, bytes);
  4191. printf(_("%d heads, %d sectors/track, %d cylinders"),
  4192. heads, sectors, cylinders);
  4193. if (units_per_sector == 1)
  4194. printf(_(", total %llu sectors"),
  4195. total_number_of_sectors / (sector_size/512));
  4196. printf(_("\nUnits = %s of %d * %d = %d bytes\n\n"),
  4197. str_units(PLURAL),
  4198. units_per_sector, sector_size, units_per_sector * sector_size);
  4199. }
  4200. /*
  4201. * Check whether partition entries are ordered by their starting positions.
  4202. * Return 0 if OK. Return i if partition i should have been earlier.
  4203. * Two separate checks: primary and logical partitions.
  4204. */
  4205. static int
  4206. wrong_p_order(int *prev)
  4207. {
  4208. const struct pte *pe;
  4209. const struct partition *p;
  4210. off_t last_p_start_pos = 0, p_start_pos;
  4211. int i, last_i = 0;
  4212. for (i = 0 ; i < partitions; i++) {
  4213. if (i == 4) {
  4214. last_i = 4;
  4215. last_p_start_pos = 0;
  4216. }
  4217. pe = &ptes[i];
  4218. if ((p = pe->part_table)->sys_ind) {
  4219. p_start_pos = get_partition_start(pe);
  4220. if (last_p_start_pos > p_start_pos) {
  4221. if (prev)
  4222. *prev = last_i;
  4223. return i;
  4224. }
  4225. last_p_start_pos = p_start_pos;
  4226. last_i = i;
  4227. }
  4228. }
  4229. return 0;
  4230. }
  4231. #ifdef CONFIG_FEATURE_FDISK_ADVANCED
  4232. /*
  4233. * Fix the chain of logicals.
  4234. * extended_offset is unchanged, the set of sectors used is unchanged
  4235. * The chain is sorted so that sectors increase, and so that
  4236. * starting sectors increase.
  4237. *
  4238. * After this it may still be that cfdisk doesnt like the table.
  4239. * (This is because cfdisk considers expanded parts, from link to
  4240. * end of partition, and these may still overlap.)
  4241. * Now
  4242. * sfdisk /dev/hda > ohda; sfdisk /dev/hda < ohda
  4243. * may help.
  4244. */
  4245. static void
  4246. fix_chain_of_logicals(void)
  4247. {
  4248. int j, oj, ojj, sj, sjj;
  4249. struct partition *pj,*pjj,tmp;
  4250. /* Stage 1: sort sectors but leave sector of part 4 */
  4251. /* (Its sector is the global extended_offset.) */
  4252. stage1:
  4253. for (j = 5; j < partitions-1; j++) {
  4254. oj = ptes[j].offset;
  4255. ojj = ptes[j+1].offset;
  4256. if (oj > ojj) {
  4257. ptes[j].offset = ojj;
  4258. ptes[j+1].offset = oj;
  4259. pj = ptes[j].part_table;
  4260. set_start_sect(pj, get_start_sect(pj)+oj-ojj);
  4261. pjj = ptes[j+1].part_table;
  4262. set_start_sect(pjj, get_start_sect(pjj)+ojj-oj);
  4263. set_start_sect(ptes[j-1].ext_pointer,
  4264. ojj-extended_offset);
  4265. set_start_sect(ptes[j].ext_pointer,
  4266. oj-extended_offset);
  4267. goto stage1;
  4268. }
  4269. }
  4270. /* Stage 2: sort starting sectors */
  4271. stage2:
  4272. for (j = 4; j < partitions-1; j++) {
  4273. pj = ptes[j].part_table;
  4274. pjj = ptes[j+1].part_table;
  4275. sj = get_start_sect(pj);
  4276. sjj = get_start_sect(pjj);
  4277. oj = ptes[j].offset;
  4278. ojj = ptes[j+1].offset;
  4279. if (oj+sj > ojj+sjj) {
  4280. tmp = *pj;
  4281. *pj = *pjj;
  4282. *pjj = tmp;
  4283. set_start_sect(pj, ojj+sjj-oj);
  4284. set_start_sect(pjj, oj+sj-ojj);
  4285. goto stage2;
  4286. }
  4287. }
  4288. /* Probably something was changed */
  4289. for (j = 4; j < partitions; j++)
  4290. ptes[j].changed = 1;
  4291. }
  4292. static void
  4293. fix_partition_table_order(void)
  4294. {
  4295. struct pte *pei, *pek;
  4296. int i,k;
  4297. if (!wrong_p_order(NULL)) {
  4298. printf(_("Nothing to do. Ordering is correct already.\n\n"));
  4299. return;
  4300. }
  4301. while ((i = wrong_p_order(&k)) != 0 && i < 4) {
  4302. /* partition i should have come earlier, move it */
  4303. /* We have to move data in the MBR */
  4304. struct partition *pi, *pk, *pe, pbuf;
  4305. pei = &ptes[i];
  4306. pek = &ptes[k];
  4307. pe = pei->ext_pointer;
  4308. pei->ext_pointer = pek->ext_pointer;
  4309. pek->ext_pointer = pe;
  4310. pi = pei->part_table;
  4311. pk = pek->part_table;
  4312. memmove(&pbuf, pi, sizeof(struct partition));
  4313. memmove(pi, pk, sizeof(struct partition));
  4314. memmove(pk, &pbuf, sizeof(struct partition));
  4315. pei->changed = pek->changed = 1;
  4316. }
  4317. if (i)
  4318. fix_chain_of_logicals();
  4319. printf("Done.\n");
  4320. }
  4321. #endif
  4322. static void
  4323. list_table(int xtra)
  4324. {
  4325. const struct partition *p;
  4326. int i, w;
  4327. #ifdef CONFIG_FEATURE_SUN_LABEL
  4328. if (label_sun == current_label_type) {
  4329. sun_list_table(xtra);
  4330. return;
  4331. }
  4332. #endif
  4333. #ifdef CONFIG_FEATURE_SGI_LABEL
  4334. if (label_sgi == current_label_type) {
  4335. sgi_list_table(xtra);
  4336. return;
  4337. }
  4338. #endif
  4339. list_disk_geometry();
  4340. #ifdef CONFIG_FEATURE_OSF_LABEL
  4341. if (label_osf == current_label_type) {
  4342. xbsd_print_disklabel(xtra);
  4343. return;
  4344. }
  4345. #endif
  4346. /* Heuristic: we list partition 3 of /dev/foo as /dev/foo3,
  4347. but if the device name ends in a digit, say /dev/foo1,
  4348. then the partition is called /dev/foo1p3. */
  4349. w = strlen(disk_device);
  4350. if (w && isdigit(disk_device[w-1]))
  4351. w++;
  4352. if (w < 5)
  4353. w = 5;
  4354. printf(_("%*s Boot Start End Blocks Id System\n"),
  4355. w+1, _("Device"));
  4356. for (i = 0; i < partitions; i++) {
  4357. const struct pte *pe = &ptes[i];
  4358. p = pe->part_table;
  4359. if (p && !is_cleared_partition(p)) {
  4360. off_t psects = get_nr_sects(p);
  4361. off_t pblocks = psects;
  4362. unsigned int podd = 0;
  4363. if (sector_size < 1024) {
  4364. pblocks /= (1024 / sector_size);
  4365. podd = psects % (1024 / sector_size);
  4366. }
  4367. if (sector_size > 1024)
  4368. pblocks *= (sector_size / 1024);
  4369. printf(
  4370. "%s %c %11llu %11llu %11llu%c %2x %s\n",
  4371. partname(disk_device, i+1, w+2),
  4372. /* boot flag */ !p->boot_ind ? ' ' : p->boot_ind == ACTIVE_FLAG
  4373. ? '*' : '?',
  4374. /* start */ (unsigned long long) cround(get_partition_start(pe)),
  4375. /* end */ (unsigned long long) cround(get_partition_start(pe) + psects
  4376. - (psects ? 1 : 0)),
  4377. /* odd flag on end */ (unsigned long long) pblocks, podd ? '+' : ' ',
  4378. /* type id */ p->sys_ind,
  4379. /* type name */ partition_type(p->sys_ind));
  4380. check_consistency(p, i);
  4381. }
  4382. }
  4383. /* Is partition table in disk order? It need not be, but... */
  4384. /* partition table entries are not checked for correct order if this
  4385. is a sgi, sun or aix labeled disk... */
  4386. if (label_dos == current_label_type && wrong_p_order(NULL)) {
  4387. /* FIXME */
  4388. printf(_("\nPartition table entries are not in disk order\n"));
  4389. }
  4390. }
  4391. #ifdef CONFIG_FEATURE_FDISK_ADVANCED
  4392. static void
  4393. x_list_table(int extend)
  4394. {
  4395. const struct pte *pe;
  4396. const struct partition *p;
  4397. int i;
  4398. printf(_("\nDisk %s: %d heads, %d sectors, %d cylinders\n\n"),
  4399. disk_device, heads, sectors, cylinders);
  4400. printf(_("Nr AF Hd Sec Cyl Hd Sec Cyl Start Size ID\n"));
  4401. for (i = 0 ; i < partitions; i++) {
  4402. pe = &ptes[i];
  4403. p = (extend ? pe->ext_pointer : pe->part_table);
  4404. if (p != NULL) {
  4405. printf("%2d %02x%4d%4d%5d%4d%4d%5d%11u%11u %02x\n",
  4406. i + 1, p->boot_ind, p->head,
  4407. sector(p->sector),
  4408. cylinder(p->sector, p->cyl), p->end_head,
  4409. sector(p->end_sector),
  4410. cylinder(p->end_sector, p->end_cyl),
  4411. get_start_sect(p), get_nr_sects(p), p->sys_ind);
  4412. if (p->sys_ind)
  4413. check_consistency(p, i);
  4414. }
  4415. }
  4416. }
  4417. #endif
  4418. #ifdef CONFIG_FEATURE_FDISK_WRITABLE
  4419. static void
  4420. fill_bounds(off_t *first, off_t *last)
  4421. {
  4422. int i;
  4423. const struct pte *pe = &ptes[0];
  4424. const struct partition *p;
  4425. for (i = 0; i < partitions; pe++,i++) {
  4426. p = pe->part_table;
  4427. if (!p->sys_ind || IS_EXTENDED(p->sys_ind)) {
  4428. first[i] = 0xffffffff;
  4429. last[i] = 0;
  4430. } else {
  4431. first[i] = get_partition_start(pe);
  4432. last[i] = first[i] + get_nr_sects(p) - 1;
  4433. }
  4434. }
  4435. }
  4436. static void
  4437. check(int n, uint h, uint s, uint c, off_t start)
  4438. {
  4439. off_t total, real_s, real_c;
  4440. real_s = sector(s) - 1;
  4441. real_c = cylinder(s, c);
  4442. total = (real_c * sectors + real_s) * heads + h;
  4443. if (!total)
  4444. fprintf(stderr, _("Warning: partition %d contains sector 0\n"), n);
  4445. if (h >= heads)
  4446. fprintf(stderr,
  4447. _("Partition %d: head %d greater than maximum %d\n"),
  4448. n, h + 1, heads);
  4449. if (real_s >= sectors)
  4450. fprintf(stderr, _("Partition %d: sector %d greater than "
  4451. "maximum %d\n"), n, s, sectors);
  4452. if (real_c >= cylinders)
  4453. fprintf(stderr, _("Partitions %d: cylinder %llu greater than "
  4454. "maximum %d\n"), n, (unsigned long long)real_c + 1, cylinders);
  4455. if (cylinders <= 1024 && start != total)
  4456. fprintf(stderr,
  4457. _("Partition %d: previous sectors %llu disagrees with "
  4458. "total %llu\n"), n, (unsigned long long)start, (unsigned long long)total);
  4459. }
  4460. static void
  4461. verify(void)
  4462. {
  4463. int i, j;
  4464. uint total = 1;
  4465. off_t first[partitions], last[partitions];
  4466. struct partition *p;
  4467. if (warn_geometry())
  4468. return;
  4469. #ifdef CONFIG_FEATURE_SUN_LABEL
  4470. if (label_sun == current_label_type) {
  4471. verify_sun();
  4472. return;
  4473. }
  4474. #endif
  4475. #ifdef CONFIG_FEATURE_SGI_LABEL
  4476. if (label_sgi == current_label_type) {
  4477. verify_sgi(1);
  4478. return;
  4479. }
  4480. #endif
  4481. fill_bounds(first, last);
  4482. for (i = 0; i < partitions; i++) {
  4483. struct pte *pe = &ptes[i];
  4484. p = pe->part_table;
  4485. if (p->sys_ind && !IS_EXTENDED(p->sys_ind)) {
  4486. check_consistency(p, i);
  4487. if (get_partition_start(pe) < first[i])
  4488. printf(_("Warning: bad start-of-data in "
  4489. "partition %d\n"), i + 1);
  4490. check(i + 1, p->end_head, p->end_sector, p->end_cyl,
  4491. last[i]);
  4492. total += last[i] + 1 - first[i];
  4493. for (j = 0; j < i; j++)
  4494. if ((first[i] >= first[j] && first[i] <= last[j])
  4495. || ((last[i] <= last[j] && last[i] >= first[j]))) {
  4496. printf(_("Warning: partition %d overlaps "
  4497. "partition %d.\n"), j + 1, i + 1);
  4498. total += first[i] >= first[j] ?
  4499. first[i] : first[j];
  4500. total -= last[i] <= last[j] ?
  4501. last[i] : last[j];
  4502. }
  4503. }
  4504. }
  4505. if (extended_offset) {
  4506. struct pte *pex = &ptes[ext_index];
  4507. off_t e_last = get_start_sect(pex->part_table) +
  4508. get_nr_sects(pex->part_table) - 1;
  4509. for (i = 4; i < partitions; i++) {
  4510. total++;
  4511. p = ptes[i].part_table;
  4512. if (!p->sys_ind) {
  4513. if (i != 4 || i + 1 < partitions)
  4514. printf(_("Warning: partition %d "
  4515. "is empty\n"), i + 1);
  4516. }
  4517. else if (first[i] < extended_offset ||
  4518. last[i] > e_last)
  4519. printf(_("Logical partition %d not entirely in "
  4520. "partition %d\n"), i + 1, ext_index + 1);
  4521. }
  4522. }
  4523. if (total > heads * sectors * cylinders)
  4524. printf(_("Total allocated sectors %d greater than the maximum "
  4525. "%d\n"), total, heads * sectors * cylinders);
  4526. else if ((total = heads * sectors * cylinders - total) != 0)
  4527. printf(_("%d unallocated sectors\n"), total);
  4528. }
  4529. static void
  4530. add_partition(int n, int sys)
  4531. {
  4532. char mesg[256]; /* 48 does not suffice in Japanese */
  4533. int i, readed = 0;
  4534. struct partition *p = ptes[n].part_table;
  4535. struct partition *q = ptes[ext_index].part_table;
  4536. long long llimit;
  4537. off_t start, stop = 0, limit, temp,
  4538. first[partitions], last[partitions];
  4539. if (p && p->sys_ind) {
  4540. printf(_("Partition %d is already defined. Delete "
  4541. "it before re-adding it.\n"), n + 1);
  4542. return;
  4543. }
  4544. fill_bounds(first, last);
  4545. if (n < 4) {
  4546. start = sector_offset;
  4547. if (display_in_cyl_units || !total_number_of_sectors)
  4548. llimit = heads * sectors * cylinders - 1;
  4549. else
  4550. llimit = total_number_of_sectors - 1;
  4551. limit = llimit;
  4552. if (limit != llimit)
  4553. limit = 0x7fffffff;
  4554. if (extended_offset) {
  4555. first[ext_index] = extended_offset;
  4556. last[ext_index] = get_start_sect(q) +
  4557. get_nr_sects(q) - 1;
  4558. }
  4559. } else {
  4560. start = extended_offset + sector_offset;
  4561. limit = get_start_sect(q) + get_nr_sects(q) - 1;
  4562. }
  4563. if (display_in_cyl_units)
  4564. for (i = 0; i < partitions; i++)
  4565. first[i] = (cround(first[i]) - 1) * units_per_sector;
  4566. snprintf(mesg, sizeof(mesg), _("First %s"), str_units(SINGULAR));
  4567. do {
  4568. temp = start;
  4569. for (i = 0; i < partitions; i++) {
  4570. int lastplusoff;
  4571. if (start == ptes[i].offset)
  4572. start += sector_offset;
  4573. lastplusoff = last[i] + ((n < 4) ? 0 : sector_offset);
  4574. if (start >= first[i] && start <= lastplusoff)
  4575. start = lastplusoff + 1;
  4576. }
  4577. if (start > limit)
  4578. break;
  4579. if (start >= temp+units_per_sector && readed) {
  4580. printf(_("Sector %llu is already allocated\n"), (unsigned long long)temp);
  4581. temp = start;
  4582. readed = 0;
  4583. }
  4584. if (!readed && start == temp) {
  4585. off_t saved_start;
  4586. saved_start = start;
  4587. start = read_int(cround(saved_start), cround(saved_start), cround(limit),
  4588. 0, mesg);
  4589. if (display_in_cyl_units) {
  4590. start = (start - 1) * units_per_sector;
  4591. if (start < saved_start) start = saved_start;
  4592. }
  4593. readed = 1;
  4594. }
  4595. } while (start != temp || !readed);
  4596. if (n > 4) { /* NOT for fifth partition */
  4597. struct pte *pe = &ptes[n];
  4598. pe->offset = start - sector_offset;
  4599. if (pe->offset == extended_offset) { /* must be corrected */
  4600. pe->offset++;
  4601. if (sector_offset == 1)
  4602. start++;
  4603. }
  4604. }
  4605. for (i = 0; i < partitions; i++) {
  4606. struct pte *pe = &ptes[i];
  4607. if (start < pe->offset && limit >= pe->offset)
  4608. limit = pe->offset - 1;
  4609. if (start < first[i] && limit >= first[i])
  4610. limit = first[i] - 1;
  4611. }
  4612. if (start > limit) {
  4613. printf(_("No free sectors available\n"));
  4614. if (n > 4)
  4615. partitions--;
  4616. return;
  4617. }
  4618. if (cround(start) == cround(limit)) {
  4619. stop = limit;
  4620. } else {
  4621. snprintf(mesg, sizeof(mesg),
  4622. _("Last %s or +size or +sizeM or +sizeK"),
  4623. str_units(SINGULAR));
  4624. stop = read_int(cround(start), cround(limit), cround(limit),
  4625. cround(start), mesg);
  4626. if (display_in_cyl_units) {
  4627. stop = stop * units_per_sector - 1;
  4628. if (stop >limit)
  4629. stop = limit;
  4630. }
  4631. }
  4632. set_partition(n, 0, start, stop, sys);
  4633. if (n > 4)
  4634. set_partition(n - 1, 1, ptes[n].offset, stop, EXTENDED);
  4635. if (IS_EXTENDED(sys)) {
  4636. struct pte *pe4 = &ptes[4];
  4637. struct pte *pen = &ptes[n];
  4638. ext_index = n;
  4639. pen->ext_pointer = p;
  4640. pe4->offset = extended_offset = start;
  4641. pe4->sectorbuffer = xcalloc(1, sector_size);
  4642. pe4->part_table = pt_offset(pe4->sectorbuffer, 0);
  4643. pe4->ext_pointer = pe4->part_table + 1;
  4644. pe4->changed = 1;
  4645. partitions = 5;
  4646. }
  4647. }
  4648. static void
  4649. add_logical(void)
  4650. {
  4651. if (partitions > 5 || ptes[4].part_table->sys_ind) {
  4652. struct pte *pe = &ptes[partitions];
  4653. pe->sectorbuffer = xcalloc(1, sector_size);
  4654. pe->part_table = pt_offset(pe->sectorbuffer, 0);
  4655. pe->ext_pointer = pe->part_table + 1;
  4656. pe->offset = 0;
  4657. pe->changed = 1;
  4658. partitions++;
  4659. }
  4660. add_partition(partitions - 1, LINUX_NATIVE);
  4661. }
  4662. static void
  4663. new_partition(void)
  4664. {
  4665. int i, free_primary = 0;
  4666. if (warn_geometry())
  4667. return;
  4668. #ifdef CONFIG_FEATURE_SUN_LABEL
  4669. if (label_sun == current_label_type) {
  4670. add_sun_partition(get_partition(0, partitions), LINUX_NATIVE);
  4671. return;
  4672. }
  4673. #endif
  4674. #ifdef CONFIG_FEATURE_SGI_LABEL
  4675. if (label_sgi == current_label_type) {
  4676. sgi_add_partition(get_partition(0, partitions), LINUX_NATIVE);
  4677. return;
  4678. }
  4679. #endif
  4680. #ifdef CONFIG_FEATURE_AIX_LABEL
  4681. if (label_aix == current_label_type) {
  4682. printf(_("\tSorry - this fdisk cannot handle AIX disk labels."
  4683. "\n\tIf you want to add DOS-type partitions, create"
  4684. "\n\ta new empty DOS partition table first. (Use o.)"
  4685. "\n\tWARNING: "
  4686. "This will destroy the present disk contents.\n"));
  4687. return;
  4688. }
  4689. #endif
  4690. for (i = 0; i < 4; i++)
  4691. free_primary += !ptes[i].part_table->sys_ind;
  4692. if (!free_primary && partitions >= MAXIMUM_PARTS) {
  4693. printf(_("The maximum number of partitions has been created\n"));
  4694. return;
  4695. }
  4696. if (!free_primary) {
  4697. if (extended_offset)
  4698. add_logical();
  4699. else
  4700. printf(_("You must delete some partition and add "
  4701. "an extended partition first\n"));
  4702. } else {
  4703. char c, line[LINE_LENGTH];
  4704. snprintf(line, sizeof(line), "%s\n %s\n p primary "
  4705. "partition (1-4)\n",
  4706. "Command action", (extended_offset ?
  4707. "l logical (5 or over)" : "e extended"));
  4708. while (1) {
  4709. if ((c = read_char(line)) == 'p' || c == 'P') {
  4710. i = get_nonexisting_partition(0, 4);
  4711. if (i >= 0)
  4712. add_partition(i, LINUX_NATIVE);
  4713. return;
  4714. }
  4715. else if (c == 'l' && extended_offset) {
  4716. add_logical();
  4717. return;
  4718. }
  4719. else if (c == 'e' && !extended_offset) {
  4720. i = get_nonexisting_partition(0, 4);
  4721. if (i >= 0)
  4722. add_partition(i, EXTENDED);
  4723. return;
  4724. }
  4725. else
  4726. printf(_("Invalid partition number "
  4727. "for type `%c'\n"), c);
  4728. }
  4729. }
  4730. }
  4731. static void
  4732. write_table(void)
  4733. {
  4734. int i;
  4735. if (label_dos == current_label_type) {
  4736. for (i = 0; i < 3; i++)
  4737. if (ptes[i].changed)
  4738. ptes[3].changed = 1;
  4739. for (i = 3; i < partitions; i++) {
  4740. struct pte *pe = &ptes[i];
  4741. if (pe->changed) {
  4742. write_part_table_flag(pe->sectorbuffer);
  4743. write_sector(pe->offset, pe->sectorbuffer);
  4744. }
  4745. }
  4746. }
  4747. #ifdef CONFIG_FEATURE_SGI_LABEL
  4748. else if (label_sgi == current_label_type) {
  4749. /* no test on change? the printf below might be mistaken */
  4750. sgi_write_table();
  4751. }
  4752. #endif
  4753. #ifdef CONFIG_FEATURE_SUN_LABEL
  4754. else if (label_sun == current_label_type) {
  4755. int needw = 0;
  4756. for (i = 0; i < 8; i++)
  4757. if (ptes[i].changed)
  4758. needw = 1;
  4759. if (needw)
  4760. sun_write_table();
  4761. }
  4762. #endif
  4763. printf(_("The partition table has been altered!\n\n"));
  4764. reread_partition_table(1);
  4765. }
  4766. static void
  4767. reread_partition_table(int leave)
  4768. {
  4769. int error = 0;
  4770. int i;
  4771. printf(_("Calling ioctl() to re-read partition table.\n"));
  4772. sync();
  4773. sleep(2);
  4774. if ((i = ioctl(fd, BLKRRPART)) != 0) {
  4775. error = errno;
  4776. } else {
  4777. /* some kernel versions (1.2.x) seem to have trouble
  4778. rereading the partition table, but if asked to do it
  4779. twice, the second time works. - biro@yggdrasil.com */
  4780. sync();
  4781. sleep(2);
  4782. if ((i = ioctl(fd, BLKRRPART)) != 0)
  4783. error = errno;
  4784. }
  4785. if (i) {
  4786. printf(_("\nWARNING: Re-reading the partition table "
  4787. "failed with error %d: %s.\n"
  4788. "The kernel still uses the old table.\n"
  4789. "The new table will be used "
  4790. "at the next reboot.\n"),
  4791. error, strerror(error));
  4792. }
  4793. if (dos_changed)
  4794. printf(
  4795. _("\nWARNING: If you have created or modified any DOS 6.x\n"
  4796. "partitions, please see the fdisk manual page for additional\n"
  4797. "information.\n"));
  4798. if (leave) {
  4799. close(fd);
  4800. printf(_("Syncing disks.\n"));
  4801. sync();
  4802. sleep(4); /* for sync() */
  4803. exit(!!i);
  4804. }
  4805. }
  4806. #endif /* CONFIG_FEATURE_FDISK_WRITABLE */
  4807. #ifdef CONFIG_FEATURE_FDISK_ADVANCED
  4808. #define MAX_PER_LINE 16
  4809. static void
  4810. print_buffer(char *pbuffer)
  4811. {
  4812. int i,l;
  4813. for (i = 0, l = 0; i < sector_size; i++, l++) {
  4814. if (l == 0)
  4815. printf("0x%03X:", i);
  4816. printf(" %02X", (unsigned char) pbuffer[i]);
  4817. if (l == MAX_PER_LINE - 1) {
  4818. printf("\n");
  4819. l = -1;
  4820. }
  4821. }
  4822. if (l > 0)
  4823. printf("\n");
  4824. printf("\n");
  4825. }
  4826. static void
  4827. print_raw(void)
  4828. {
  4829. int i;
  4830. printf(_("Device: %s\n"), disk_device);
  4831. #if defined(CONFIG_FEATURE_SGI_LABEL) || defined(CONFIG_FEATURE_SUN_LABEL)
  4832. if (label_sun == current_label_type || label_sgi == current_label_type)
  4833. print_buffer(MBRbuffer);
  4834. else
  4835. #endif
  4836. for (i = 3; i < partitions; i++)
  4837. print_buffer(ptes[i].sectorbuffer);
  4838. }
  4839. static void
  4840. move_begin(int i)
  4841. {
  4842. struct pte *pe = &ptes[i];
  4843. struct partition *p = pe->part_table;
  4844. off_t new, first;
  4845. if (warn_geometry())
  4846. return;
  4847. if (!p->sys_ind || !get_nr_sects(p) || IS_EXTENDED(p->sys_ind)) {
  4848. printf(_("Partition %d has no data area\n"), i + 1);
  4849. return;
  4850. }
  4851. first = get_partition_start(pe);
  4852. new = read_int(first, first, first + get_nr_sects(p) - 1, first,
  4853. _("New beginning of data")) - pe->offset;
  4854. if (new != get_nr_sects(p)) {
  4855. first = get_nr_sects(p) + get_start_sect(p) - new;
  4856. set_nr_sects(p, first);
  4857. set_start_sect(p, new);
  4858. pe->changed = 1;
  4859. }
  4860. }
  4861. static void
  4862. xselect(void)
  4863. {
  4864. char c;
  4865. while (1) {
  4866. putchar('\n');
  4867. c = tolower(read_char(_("Expert command (m for help): ")));
  4868. switch (c) {
  4869. case 'a':
  4870. #ifdef CONFIG_FEATURE_SUN_LABEL
  4871. if (label_sun == current_label_type)
  4872. sun_set_alt_cyl();
  4873. #endif
  4874. break;
  4875. case 'b':
  4876. if (label_dos == current_label_type)
  4877. move_begin(get_partition(0, partitions));
  4878. break;
  4879. case 'c':
  4880. user_cylinders = cylinders =
  4881. read_int(1, cylinders, 1048576, 0,
  4882. _("Number of cylinders"));
  4883. #ifdef CONFIG_FEATURE_SUN_LABEL
  4884. if (label_sun == current_label_type)
  4885. sun_set_ncyl(cylinders);
  4886. #endif
  4887. if (label_dos == current_label_type)
  4888. warn_cylinders();
  4889. break;
  4890. case 'd':
  4891. print_raw();
  4892. break;
  4893. case 'e':
  4894. #ifdef CONFIG_FEATURE_SGI_LABEL
  4895. if (label_sgi == current_label_type)
  4896. sgi_set_xcyl();
  4897. else
  4898. #endif
  4899. #ifdef CONFIG_FEATURE_SUN_LABEL
  4900. if (label_sun == current_label_type)
  4901. sun_set_xcyl();
  4902. else
  4903. #endif
  4904. if (label_dos == current_label_type)
  4905. x_list_table(1);
  4906. break;
  4907. case 'f':
  4908. if (label_dos == current_label_type)
  4909. fix_partition_table_order();
  4910. break;
  4911. case 'g':
  4912. #ifdef CONFIG_FEATURE_SGI_LABEL
  4913. create_sgilabel();
  4914. #endif
  4915. break;
  4916. case 'h':
  4917. user_heads = heads = read_int(1, heads, 256, 0,
  4918. _("Number of heads"));
  4919. update_units();
  4920. break;
  4921. case 'i':
  4922. #ifdef CONFIG_FEATURE_SUN_LABEL
  4923. if (label_sun == current_label_type)
  4924. sun_set_ilfact();
  4925. #endif
  4926. break;
  4927. case 'o':
  4928. #ifdef CONFIG_FEATURE_SUN_LABEL
  4929. if (label_sun == current_label_type)
  4930. sun_set_rspeed();
  4931. #endif
  4932. break;
  4933. case 'p':
  4934. #ifdef CONFIG_FEATURE_SUN_LABEL
  4935. if (label_sun == current_label_type)
  4936. list_table(1);
  4937. else
  4938. #endif
  4939. x_list_table(0);
  4940. break;
  4941. case 'q':
  4942. close(fd);
  4943. printf("\n");
  4944. exit(0);
  4945. case 'r':
  4946. return;
  4947. case 's':
  4948. user_sectors = sectors = read_int(1, sectors, 63, 0,
  4949. _("Number of sectors"));
  4950. if (dos_compatible_flag) {
  4951. sector_offset = sectors;
  4952. fprintf(stderr, _("Warning: setting "
  4953. "sector offset for DOS "
  4954. "compatiblity\n"));
  4955. }
  4956. update_units();
  4957. break;
  4958. case 'v':
  4959. verify();
  4960. break;
  4961. case 'w':
  4962. write_table(); /* does not return */
  4963. break;
  4964. case 'y':
  4965. #ifdef CONFIG_FEATURE_SUN_LABEL
  4966. if (label_sun == current_label_type)
  4967. sun_set_pcylcount();
  4968. #endif
  4969. break;
  4970. default:
  4971. xmenu();
  4972. }
  4973. }
  4974. }
  4975. #endif /* ADVANCED mode */
  4976. static int
  4977. is_ide_cdrom_or_tape(const char *device)
  4978. {
  4979. FILE *procf;
  4980. char buf[100];
  4981. struct stat statbuf;
  4982. int is_ide = 0;
  4983. /* No device was given explicitly, and we are trying some
  4984. likely things. But opening /dev/hdc may produce errors like
  4985. "hdc: tray open or drive not ready"
  4986. if it happens to be a CD-ROM drive. It even happens that
  4987. the process hangs on the attempt to read a music CD.
  4988. So try to be careful. This only works since 2.1.73. */
  4989. if (strncmp("/dev/hd", device, 7))
  4990. return 0;
  4991. snprintf(buf, sizeof(buf), "/proc/ide/%s/media", device+5);
  4992. procf = fopen(buf, "r");
  4993. if (procf != NULL && fgets(buf, sizeof(buf), procf))
  4994. is_ide = (!strncmp(buf, "cdrom", 5) ||
  4995. !strncmp(buf, "tape", 4));
  4996. else
  4997. /* Now when this proc file does not exist, skip the
  4998. device when it is read-only. */
  4999. if (stat(device, &statbuf) == 0)
  5000. is_ide = ((statbuf.st_mode & 0222) == 0);
  5001. if (procf)
  5002. fclose(procf);
  5003. return is_ide;
  5004. }
  5005. static void
  5006. try(const char *device, int user_specified)
  5007. {
  5008. int gb;
  5009. disk_device = device;
  5010. if (setjmp(listingbuf))
  5011. return;
  5012. if (!user_specified)
  5013. if (is_ide_cdrom_or_tape(device))
  5014. return;
  5015. if ((fd = open(disk_device, type_open)) >= 0) {
  5016. gb = get_boot(try_only);
  5017. if (gb > 0) { /* I/O error */
  5018. close(fd);
  5019. } else if (gb < 0) { /* no DOS signature */
  5020. list_disk_geometry();
  5021. if (label_aix == current_label_type){
  5022. return;
  5023. }
  5024. #ifdef CONFIG_FEATURE_OSF_LABEL
  5025. if (btrydev(device) < 0)
  5026. #endif
  5027. fprintf(stderr,
  5028. _("Disk %s doesn't contain a valid "
  5029. "partition table\n"), device);
  5030. close(fd);
  5031. } else {
  5032. close(fd);
  5033. list_table(0);
  5034. #ifdef CONFIG_FEATURE_FDISK_WRITABLE
  5035. if (label_sun != current_label_type && partitions > 4){
  5036. delete_partition(ext_index);
  5037. }
  5038. #endif
  5039. }
  5040. } else {
  5041. /* Ignore other errors, since we try IDE
  5042. and SCSI hard disks which may not be
  5043. installed on the system. */
  5044. if (errno == EACCES) {
  5045. fprintf(stderr, _("Cannot open %s\n"), device);
  5046. return;
  5047. }
  5048. }
  5049. }
  5050. /* for fdisk -l: try all things in /proc/partitions
  5051. that look like a partition name (do not end in a digit) */
  5052. static void
  5053. tryprocpt(void)
  5054. {
  5055. FILE *procpt;
  5056. char line[100], ptname[100], devname[120], *s;
  5057. int ma, mi, sz;
  5058. procpt = bb_wfopen(PROC_PARTITIONS, "r");
  5059. while (fgets(line, sizeof(line), procpt)) {
  5060. if (sscanf(line, " %d %d %d %[^\n ]",
  5061. &ma, &mi, &sz, ptname) != 4)
  5062. continue;
  5063. for (s = ptname; *s; s++);
  5064. if (isdigit(s[-1]))
  5065. continue;
  5066. sprintf(devname, "/dev/%s", ptname);
  5067. try(devname, 0);
  5068. }
  5069. #ifdef CONFIG_FEATURE_CLEAN_UP
  5070. fclose(procpt);
  5071. #endif
  5072. }
  5073. #ifdef CONFIG_FEATURE_FDISK_WRITABLE
  5074. static void
  5075. unknown_command(int c)
  5076. {
  5077. printf(_("%c: unknown command\n"), c);
  5078. }
  5079. #endif
  5080. int fdisk_main(int argc, char **argv)
  5081. {
  5082. int c;
  5083. #ifdef CONFIG_FEATURE_FDISK_WRITABLE
  5084. int optl = 0;
  5085. #endif
  5086. #ifdef CONFIG_FEATURE_FDISK_BLKSIZE
  5087. int opts = 0;
  5088. #endif
  5089. /*
  5090. * Calls:
  5091. * fdisk -v
  5092. * fdisk -l [-b sectorsize] [-u] device ...
  5093. * fdisk -s [partition] ...
  5094. * fdisk [-b sectorsize] [-u] device
  5095. *
  5096. * Options -C, -H, -S set the geometry.
  5097. *
  5098. */
  5099. while ((c = getopt(argc, argv, "b:C:H:lS:uvV"
  5100. #ifdef CONFIG_FEATURE_FDISK_BLKSIZE
  5101. "s"
  5102. #endif
  5103. )) != -1) {
  5104. switch (c) {
  5105. case 'b':
  5106. /* Ugly: this sector size is really per device,
  5107. so cannot be combined with multiple disks,
  5108. and te same goes for the C/H/S options.
  5109. */
  5110. sector_size = atoi(optarg);
  5111. if (sector_size != 512 && sector_size != 1024 &&
  5112. sector_size != 2048)
  5113. bb_show_usage();
  5114. sector_offset = 2;
  5115. user_set_sector_size = 1;
  5116. break;
  5117. case 'C':
  5118. user_cylinders = atoi(optarg);
  5119. break;
  5120. case 'H':
  5121. user_heads = atoi(optarg);
  5122. if (user_heads <= 0 || user_heads >= 256)
  5123. user_heads = 0;
  5124. break;
  5125. case 'S':
  5126. user_sectors = atoi(optarg);
  5127. if (user_sectors <= 0 || user_sectors >= 64)
  5128. user_sectors = 0;
  5129. break;
  5130. case 'l':
  5131. #ifdef CONFIG_FEATURE_FDISK_WRITABLE
  5132. optl = 1;
  5133. #endif
  5134. break;
  5135. #ifdef CONFIG_FEATURE_FDISK_BLKSIZE
  5136. case 's':
  5137. opts = 1;
  5138. break;
  5139. #endif
  5140. case 'u':
  5141. display_in_cyl_units = 0;
  5142. break;
  5143. case 'V':
  5144. case 'v':
  5145. printf("fdisk v" UTIL_LINUX_VERSION "\n");
  5146. return 0;
  5147. default:
  5148. bb_show_usage();
  5149. }
  5150. }
  5151. #if 0
  5152. printf(_("This kernel finds the sector size itself - "
  5153. "-b option ignored\n"));
  5154. #else
  5155. if (user_set_sector_size && argc-optind != 1)
  5156. printf(_("Warning: the -b (set sector size) option should"
  5157. " be used with one specified device\n"));
  5158. #endif
  5159. #ifdef CONFIG_FEATURE_FDISK_WRITABLE
  5160. if (optl) {
  5161. nowarn = 1;
  5162. #endif
  5163. type_open = O_RDONLY;
  5164. if (argc > optind) {
  5165. int k;
  5166. #if __GNUC__
  5167. /* avoid gcc warning:
  5168. variable `k' might be clobbered by `longjmp' */
  5169. (void)&k;
  5170. #endif
  5171. listing = 1;
  5172. for (k = optind; k < argc; k++)
  5173. try(argv[k], 1);
  5174. } else {
  5175. /* we no longer have default device names */
  5176. /* but, we can use /proc/partitions instead */
  5177. tryprocpt();
  5178. }
  5179. return 0;
  5180. #ifdef CONFIG_FEATURE_FDISK_WRITABLE
  5181. }
  5182. #endif
  5183. #ifdef CONFIG_FEATURE_FDISK_BLKSIZE
  5184. if (opts) {
  5185. long size;
  5186. int j;
  5187. nowarn = 1;
  5188. type_open = O_RDONLY;
  5189. opts = argc - optind;
  5190. if (opts <= 0)
  5191. bb_show_usage();
  5192. for (j = optind; j < argc; j++) {
  5193. disk_device = argv[j];
  5194. if ((fd = open(disk_device, type_open)) < 0)
  5195. fdisk_fatal(unable_to_open);
  5196. if (ioctl(fd, BLKGETSIZE, &size))
  5197. fdisk_fatal(ioctl_error);
  5198. close(fd);
  5199. if (opts == 1)
  5200. printf("%ld\n", size/2);
  5201. else
  5202. printf("%s: %ld\n", argv[j], size/2);
  5203. }
  5204. return 0;
  5205. }
  5206. #endif
  5207. #ifdef CONFIG_FEATURE_FDISK_WRITABLE
  5208. if (argc-optind == 1)
  5209. disk_device = argv[optind];
  5210. else
  5211. bb_show_usage();
  5212. get_boot(fdisk);
  5213. #ifdef CONFIG_FEATURE_OSF_LABEL
  5214. if (label_osf == current_label_type) {
  5215. /* OSF label, and no DOS label */
  5216. printf(_("Detected an OSF/1 disklabel on %s, entering "
  5217. "disklabel mode.\n"),
  5218. disk_device);
  5219. bselect();
  5220. /*Why do we do this? It seems to be counter-intuitive*/
  5221. current_label_type = label_dos;
  5222. /* If we return we may want to make an empty DOS label? */
  5223. }
  5224. #endif
  5225. while (1) {
  5226. putchar('\n');
  5227. c = tolower(read_char(_("Command (m for help): ")));
  5228. switch (c) {
  5229. case 'a':
  5230. if (label_dos == current_label_type)
  5231. toggle_active(get_partition(1, partitions));
  5232. #ifdef CONFIG_FEATURE_SUN_LABEL
  5233. else if (label_sun == current_label_type)
  5234. toggle_sunflags(get_partition(1, partitions),
  5235. 0x01);
  5236. #endif
  5237. #ifdef CONFIG_FEATURE_SGI_LABEL
  5238. else if (label_sgi == current_label_type)
  5239. sgi_set_bootpartition(
  5240. get_partition(1, partitions));
  5241. #endif
  5242. else
  5243. unknown_command(c);
  5244. break;
  5245. case 'b':
  5246. #ifdef CONFIG_FEATURE_SGI_LABEL
  5247. if (label_sgi == current_label_type) {
  5248. printf(_("\nThe current boot file is: %s\n"),
  5249. sgi_get_bootfile());
  5250. if (read_chars(_("Please enter the name of the "
  5251. "new boot file: ")) == '\n')
  5252. printf(_("Boot file unchanged\n"));
  5253. else
  5254. sgi_set_bootfile(line_ptr);
  5255. } else
  5256. #endif
  5257. #ifdef CONFIG_FEATURE_OSF_LABEL
  5258. bselect();
  5259. #endif
  5260. break;
  5261. case 'c':
  5262. if (label_dos == current_label_type)
  5263. toggle_dos_compatibility_flag();
  5264. #ifdef CONFIG_FEATURE_SUN_LABEL
  5265. else if (label_sun == current_label_type)
  5266. toggle_sunflags(get_partition(1, partitions),
  5267. 0x10);
  5268. #endif
  5269. #ifdef CONFIG_FEATURE_SGI_LABEL
  5270. else if (label_sgi == current_label_type)
  5271. sgi_set_swappartition(
  5272. get_partition(1, partitions));
  5273. #endif
  5274. else
  5275. unknown_command(c);
  5276. break;
  5277. case 'd':
  5278. {
  5279. int j;
  5280. #ifdef CONFIG_FEATURE_SGI_LABEL
  5281. /* If sgi_label then don't use get_existing_partition,
  5282. let the user select a partition, since
  5283. get_existing_partition() only works for Linux-like
  5284. partition tables */
  5285. if (label_sgi != current_label_type) {
  5286. j = get_existing_partition(1, partitions);
  5287. } else {
  5288. j = get_partition(1, partitions);
  5289. }
  5290. #else
  5291. j = get_existing_partition(1, partitions);
  5292. #endif
  5293. if (j >= 0)
  5294. delete_partition(j);
  5295. }
  5296. break;
  5297. case 'i':
  5298. #ifdef CONFIG_FEATURE_SGI_LABEL
  5299. if (label_sgi == current_label_type)
  5300. create_sgiinfo();
  5301. else
  5302. #endif
  5303. unknown_command(c);
  5304. case 'l':
  5305. list_types(get_sys_types());
  5306. break;
  5307. case 'm':
  5308. menu();
  5309. break;
  5310. case 'n':
  5311. new_partition();
  5312. break;
  5313. case 'o':
  5314. create_doslabel();
  5315. break;
  5316. case 'p':
  5317. list_table(0);
  5318. break;
  5319. case 'q':
  5320. close(fd);
  5321. printf("\n");
  5322. return 0;
  5323. case 's':
  5324. #ifdef CONFIG_FEATURE_SUN_LABEL
  5325. create_sunlabel();
  5326. #endif
  5327. break;
  5328. case 't':
  5329. change_sysid();
  5330. break;
  5331. case 'u':
  5332. change_units();
  5333. break;
  5334. case 'v':
  5335. verify();
  5336. break;
  5337. case 'w':
  5338. write_table(); /* does not return */
  5339. break;
  5340. #ifdef CONFIG_FEATURE_FDISK_ADVANCED
  5341. case 'x':
  5342. #ifdef CONFIG_FEATURE_SGI_LABEL
  5343. if (label_sgi == current_label_type) {
  5344. fprintf(stderr,
  5345. _("\n\tSorry, no experts menu for SGI "
  5346. "partition tables available.\n\n"));
  5347. } else
  5348. #endif
  5349. xselect();
  5350. break;
  5351. #endif
  5352. default:
  5353. unknown_command(c);
  5354. menu();
  5355. }
  5356. }
  5357. return 0;
  5358. #endif /* CONFIG_FEATURE_FDISK_WRITABLE */
  5359. }