1 Star 0 Fork 16

jackzhao166/grub2

forked from OpenCloudOS Stream/grub2 
加入 Gitee
与超过 1200万 开发者一起发现、参与优秀开源项目,私有仓库也完全免费 :)
免费加入
文件
该仓库未声明开源许可证文件(LICENSE),使用请关注具体项目描述及其代码上游依赖。
克隆/下载
0061-Support-UEFI-networking-protocols.patch 140.44 KB
一键复制 编辑 原始数据 按行查看 历史
nilusyi 提交于 2024-04-07 16:45 . update patches
12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646164716481649165016511652165316541655165616571658165916601661166216631664166516661667166816691670167116721673167416751676167716781679168016811682168316841685168616871688168916901691169216931694169516961697169816991700170117021703170417051706170717081709171017111712171317141715171617171718171917201721172217231724172517261727172817291730173117321733173417351736173717381739174017411742174317441745174617471748174917501751175217531754175517561757175817591760176117621763176417651766176717681769177017711772177317741775177617771778177917801781178217831784178517861787178817891790179117921793179417951796179717981799180018011802180318041805180618071808180918101811181218131814181518161817181818191820182118221823182418251826182718281829183018311832183318341835183618371838183918401841184218431844184518461847184818491850185118521853185418551856185718581859186018611862186318641865186618671868186918701871187218731874187518761877187818791880188118821883188418851886188718881889189018911892189318941895189618971898189919001901190219031904190519061907190819091910191119121913191419151916191719181919192019211922192319241925192619271928192919301931193219331934193519361937193819391940194119421943194419451946194719481949195019511952195319541955195619571958195919601961196219631964196519661967196819691970197119721973197419751976197719781979198019811982198319841985198619871988198919901991199219931994199519961997199819992000200120022003200420052006200720082009201020112012201320142015201620172018201920202021202220232024202520262027202820292030203120322033203420352036203720382039204020412042204320442045204620472048204920502051205220532054205520562057205820592060206120622063206420652066206720682069207020712072207320742075207620772078207920802081208220832084208520862087208820892090209120922093209420952096209720982099210021012102210321042105210621072108210921102111211221132114211521162117211821192120212121222123212421252126212721282129213021312132213321342135213621372138213921402141214221432144214521462147214821492150215121522153215421552156215721582159216021612162216321642165216621672168216921702171217221732174217521762177217821792180218121822183218421852186218721882189219021912192219321942195219621972198219922002201220222032204220522062207220822092210221122122213221422152216221722182219222022212222222322242225222622272228222922302231223222332234223522362237223822392240224122422243224422452246224722482249225022512252225322542255225622572258225922602261226222632264226522662267226822692270227122722273227422752276227722782279228022812282228322842285228622872288228922902291229222932294229522962297229822992300230123022303230423052306230723082309231023112312231323142315231623172318231923202321232223232324232523262327232823292330233123322333233423352336233723382339234023412342234323442345234623472348234923502351235223532354235523562357235823592360236123622363236423652366236723682369237023712372237323742375237623772378237923802381238223832384238523862387238823892390239123922393239423952396239723982399240024012402240324042405240624072408240924102411241224132414241524162417241824192420242124222423242424252426242724282429243024312432243324342435243624372438243924402441244224432444244524462447244824492450245124522453245424552456245724582459246024612462246324642465246624672468246924702471247224732474247524762477247824792480248124822483248424852486248724882489249024912492249324942495249624972498249925002501250225032504250525062507250825092510251125122513251425152516251725182519252025212522252325242525252625272528252925302531253225332534253525362537253825392540254125422543254425452546254725482549255025512552255325542555255625572558255925602561256225632564256525662567256825692570257125722573257425752576257725782579258025812582258325842585258625872588258925902591259225932594259525962597259825992600260126022603260426052606260726082609261026112612261326142615261626172618261926202621262226232624262526262627262826292630263126322633263426352636263726382639264026412642264326442645264626472648264926502651265226532654265526562657265826592660266126622663266426652666266726682669267026712672267326742675267626772678267926802681268226832684268526862687268826892690269126922693269426952696269726982699270027012702270327042705270627072708270927102711271227132714271527162717271827192720272127222723272427252726272727282729273027312732273327342735273627372738273927402741274227432744274527462747274827492750275127522753275427552756275727582759276027612762276327642765276627672768276927702771277227732774277527762777277827792780278127822783278427852786278727882789279027912792279327942795279627972798279928002801280228032804280528062807280828092810281128122813281428152816281728182819282028212822282328242825282628272828282928302831283228332834283528362837283828392840284128422843284428452846284728482849285028512852285328542855285628572858285928602861286228632864286528662867286828692870287128722873287428752876287728782879288028812882288328842885288628872888288928902891289228932894289528962897289828992900290129022903290429052906290729082909291029112912291329142915291629172918291929202921292229232924292529262927292829292930293129322933293429352936293729382939294029412942294329442945294629472948294929502951295229532954295529562957295829592960296129622963296429652966296729682969297029712972297329742975297629772978297929802981298229832984298529862987298829892990299129922993299429952996299729982999300030013002300330043005300630073008300930103011301230133014301530163017301830193020302130223023302430253026302730283029303030313032303330343035303630373038303930403041304230433044304530463047304830493050305130523053305430553056305730583059306030613062306330643065306630673068306930703071307230733074307530763077307830793080308130823083308430853086308730883089309030913092309330943095309630973098309931003101310231033104310531063107310831093110311131123113311431153116311731183119312031213122312331243125312631273128312931303131313231333134313531363137313831393140314131423143314431453146314731483149315031513152315331543155315631573158315931603161316231633164316531663167316831693170317131723173317431753176317731783179318031813182318331843185318631873188318931903191319231933194319531963197319831993200320132023203320432053206320732083209321032113212321332143215321632173218321932203221322232233224322532263227322832293230323132323233323432353236323732383239324032413242324332443245324632473248324932503251325232533254325532563257325832593260326132623263326432653266326732683269327032713272327332743275327632773278327932803281328232833284328532863287328832893290329132923293329432953296329732983299330033013302330333043305330633073308330933103311331233133314331533163317331833193320332133223323332433253326332733283329333033313332333333343335333633373338333933403341334233433344334533463347334833493350335133523353335433553356335733583359336033613362336333643365336633673368336933703371337233733374337533763377337833793380338133823383338433853386338733883389339033913392339333943395339633973398339934003401340234033404340534063407340834093410341134123413341434153416341734183419342034213422342334243425342634273428342934303431343234333434343534363437343834393440344134423443344434453446344734483449345034513452345334543455345634573458345934603461346234633464346534663467346834693470347134723473347434753476347734783479348034813482348334843485348634873488348934903491349234933494349534963497349834993500350135023503350435053506350735083509351035113512351335143515351635173518351935203521352235233524352535263527352835293530353135323533353435353536353735383539354035413542354335443545354635473548354935503551355235533554355535563557355835593560356135623563356435653566356735683569357035713572357335743575357635773578357935803581358235833584358535863587358835893590359135923593359435953596359735983599360036013602360336043605360636073608360936103611361236133614361536163617361836193620362136223623362436253626362736283629363036313632363336343635363636373638363936403641364236433644364536463647364836493650365136523653365436553656365736583659366036613662366336643665366636673668366936703671367236733674367536763677367836793680368136823683368436853686368736883689369036913692369336943695369636973698369937003701370237033704370537063707370837093710371137123713371437153716371737183719372037213722372337243725372637273728372937303731373237333734373537363737373837393740374137423743374437453746374737483749375037513752375337543755375637573758375937603761376237633764376537663767376837693770377137723773377437753776377737783779378037813782378337843785378637873788378937903791379237933794379537963797379837993800380138023803380438053806380738083809381038113812381338143815381638173818381938203821382238233824382538263827382838293830383138323833383438353836383738383839384038413842384338443845384638473848384938503851385238533854385538563857385838593860386138623863386438653866386738683869387038713872387338743875387638773878387938803881388238833884388538863887388838893890389138923893389438953896389738983899390039013902390339043905390639073908390939103911391239133914391539163917391839193920392139223923392439253926392739283929393039313932393339343935393639373938393939403941394239433944394539463947394839493950395139523953395439553956395739583959396039613962396339643965396639673968396939703971397239733974397539763977397839793980398139823983398439853986398739883989399039913992399339943995399639973998399940004001400240034004400540064007400840094010401140124013401440154016401740184019402040214022402340244025402640274028402940304031403240334034403540364037403840394040404140424043404440454046404740484049405040514052405340544055405640574058405940604061406240634064406540664067406840694070407140724073407440754076407740784079408040814082408340844085408640874088408940904091409240934094409540964097409840994100410141024103410441054106410741084109411041114112411341144115411641174118411941204121412241234124412541264127412841294130413141324133413441354136413741384139414041414142414341444145414641474148414941504151415241534154415541564157415841594160416141624163416441654166416741684169417041714172417341744175417641774178417941804181418241834184418541864187418841894190419141924193419441954196419741984199420042014202420342044205420642074208420942104211421242134214421542164217421842194220422142224223422442254226422742284229423042314232423342344235423642374238423942404241424242434244424542464247424842494250425142524253425442554256425742584259426042614262426342644265426642674268426942704271427242734274427542764277427842794280428142824283428442854286428742884289429042914292429342944295429642974298429943004301430243034304430543064307430843094310431143124313431443154316431743184319432043214322432343244325432643274328432943304331433243334334433543364337433843394340434143424343434443454346434743484349435043514352435343544355435643574358435943604361436243634364436543664367436843694370437143724373437443754376437743784379438043814382438343844385438643874388438943904391439243934394439543964397439843994400440144024403440444054406440744084409441044114412441344144415441644174418441944204421442244234424442544264427442844294430443144324433443444354436443744384439444044414442444344444445444644474448444944504451445244534454445544564457445844594460446144624463446444654466446744684469447044714472447344744475447644774478447944804481448244834484448544864487448844894490449144924493449444954496449744984499450045014502450345044505450645074508450945104511451245134514451545164517451845194520452145224523452445254526452745284529453045314532453345344535453645374538453945404541454245434544454545464547454845494550455145524553455445554556455745584559456045614562456345644565456645674568456945704571457245734574457545764577457845794580458145824583458445854586458745884589459045914592459345944595459645974598459946004601460246034604460546064607460846094610461146124613461446154616461746184619462046214622462346244625462646274628462946304631463246334634463546364637463846394640464146424643464446454646464746484649465046514652465346544655465646574658465946604661466246634664466546664667466846694670467146724673467446754676467746784679468046814682468346844685468646874688468946904691469246934694469546964697469846994700470147024703470447054706470747084709471047114712471347144715471647174718471947204721472247234724472547264727472847294730473147324733473447354736473747384739474047414742474347444745474647474748474947504751475247534754475547564757475847594760476147624763476447654766476747684769477047714772477347744775477647774778477947804781478247834784478547864787478847894790479147924793479447954796479747984799480048014802480348044805480648074808480948104811481248134814481548164817481848194820482148224823482448254826482748284829483048314832483348344835483648374838483948404841484248434844484548464847484848494850485148524853485448554856485748584859486048614862486348644865486648674868486948704871487248734874487548764877487848794880488148824883488448854886488748884889489048914892489348944895489648974898489949004901490249034904490549064907490849094910491149124913491449154916491749184919492049214922492349244925492649274928492949304931493249334934493549364937493849394940494149424943494449454946494749484949495049514952495349544955495649574958495949604961
From c42d93573bf925d6788aade07c8a0e9b26b73cf1 Mon Sep 17 00:00:00 2001
From: Michael Chang <mchang@suse.com>
Date: Wed, 22 Feb 2017 14:27:50 +0800
Subject: [PATCH 061/272] Support UEFI networking protocols
References: fate#320130, bsc#1015589, bsc#1076132
Patch-Mainline: no
V1:
* Add preliminary support of UEFI networking protocols
* Support UEFI HTTPS Boot
V2:
* Workaround http data access in firmware
* Fix DNS device path parsing for efinet device
* Relaxed UEFI Protocol requirement
* Support Intel OPA (Omni-Path Architecture) PXE Boot
V3:
* Fix bufio in calculating address of next_buf
* Check HTTP respond code
* Use HEAD request method to test before GET
* Finish HTTP transaction in one go
* Fix bsc#1076132
V4:
* Add fs_ prefix with upstream commit
ad4bfeec5 Change fs functions to add fs_ prefix
V5:
* Use overflow checking primitives where the arithmetic expression for
buffer allocations may include unvalidated data
* Use grub_calloc for overflow check and return NULL when it would
occur.
V6:
* Don't force grub_print_error if no best route found as boot process
could be interrupted by logged error. The default interface will be
used as fallback in this case
---
grub-core/Makefile.core.def | 6 +
grub-core/io/bufio.c | 2 +-
grub-core/kern/efi/efi.c | 96 +-
grub-core/net/drivers/efi/efinet.c | 29 +-
grub-core/net/efi/dhcp.c | 399 ++++++++
grub-core/net/efi/http.c | 424 ++++++++
grub-core/net/efi/ip4_config.c | 409 ++++++++
grub-core/net/efi/ip6_config.c | 430 +++++++++
grub-core/net/efi/net.c | 1440 ++++++++++++++++++++++++++++
grub-core/net/efi/pxe.c | 424 ++++++++
grub-core/net/net.c | 74 ++
include/grub/efi/api.h | 197 +++-
include/grub/efi/dhcp.h | 343 +++++++
include/grub/efi/http.h | 215 +++++
include/grub/net/efi.h | 144 +++
15 files changed, 4586 insertions(+), 46 deletions(-)
create mode 100644 grub-core/net/efi/dhcp.c
create mode 100644 grub-core/net/efi/http.c
create mode 100644 grub-core/net/efi/ip4_config.c
create mode 100644 grub-core/net/efi/ip6_config.c
create mode 100644 grub-core/net/efi/net.c
create mode 100644 grub-core/net/efi/pxe.c
create mode 100644 include/grub/efi/dhcp.h
create mode 100644 include/grub/efi/http.h
create mode 100644 include/grub/net/efi.h
diff --git a/grub-core/Makefile.core.def b/grub-core/Makefile.core.def
index 43b0f50af..8b446516e 100644
--- a/grub-core/Makefile.core.def
+++ b/grub-core/Makefile.core.def
@@ -2358,6 +2358,12 @@ module = {
common = net/ethernet.c;
common = net/arp.c;
common = net/netbuff.c;
+ efi = net/efi/net.c;
+ efi = net/efi/http.c;
+ efi = net/efi/pxe.c;
+ efi = net/efi/ip4_config.c;
+ efi = net/efi/ip6_config.c;
+ efi = net/efi/dhcp.c;
};
module = {
diff --git a/grub-core/io/bufio.c b/grub-core/io/bufio.c
index a458c3aca..163773153 100644
--- a/grub-core/io/bufio.c
+++ b/grub-core/io/bufio.c
@@ -139,7 +139,7 @@ grub_bufio_read (grub_file_t file, char *buf, grub_size_t len)
return res;
/* Need to read some more. */
- next_buf = (file->offset + res + len - 1) & ~((grub_off_t) bufio->block_size - 1);
+ next_buf = (grub_divmod64 (file->offset + res + len - 1, bufio->block_size, NULL)) * bufio->block_size;
/* Now read between file->offset + res and bufio->buffer_at. */
if (file->offset + res < next_buf)
{
diff --git a/grub-core/kern/efi/efi.c b/grub-core/kern/efi/efi.c
index b93ae3aba..b674816aa 100644
--- a/grub-core/kern/efi/efi.c
+++ b/grub-core/kern/efi/efi.c
@@ -770,7 +770,7 @@ grub_efi_print_device_path (grub_efi_device_path_t *dp)
{
grub_efi_ipv4_device_path_t *ipv4
= (grub_efi_ipv4_device_path_t *) dp;
- grub_printf ("/IPv4(%u.%u.%u.%u,%u.%u.%u.%u,%u,%u,%x,%x)",
+ grub_printf ("/IPv4(%u.%u.%u.%u,%u.%u.%u.%u,%u,%u,%x,%x",
(unsigned) ipv4->local_ip_address[0],
(unsigned) ipv4->local_ip_address[1],
(unsigned) ipv4->local_ip_address[2],
@@ -783,33 +783,60 @@ grub_efi_print_device_path (grub_efi_device_path_t *dp)
(unsigned) ipv4->remote_port,
(unsigned) ipv4->protocol,
(unsigned) ipv4->static_ip_address);
+ if (len == sizeof (*ipv4))
+ {
+ grub_printf (",%u.%u.%u.%u,%u.%u.%u.%u",
+ (unsigned) ipv4->gateway_ip_address[0],
+ (unsigned) ipv4->gateway_ip_address[1],
+ (unsigned) ipv4->gateway_ip_address[2],
+ (unsigned) ipv4->gateway_ip_address[3],
+ (unsigned) ipv4->subnet_mask[0],
+ (unsigned) ipv4->subnet_mask[1],
+ (unsigned) ipv4->subnet_mask[2],
+ (unsigned) ipv4->subnet_mask[3]);
+ }
+ grub_printf (")");
}
break;
case GRUB_EFI_IPV6_DEVICE_PATH_SUBTYPE:
{
grub_efi_ipv6_device_path_t *ipv6
= (grub_efi_ipv6_device_path_t *) dp;
- grub_printf ("/IPv6(%x:%x:%x:%x:%x:%x:%x:%x,%x:%x:%x:%x:%x:%x:%x:%x,%u,%u,%x,%x)",
- (unsigned) ipv6->local_ip_address[0],
- (unsigned) ipv6->local_ip_address[1],
- (unsigned) ipv6->local_ip_address[2],
- (unsigned) ipv6->local_ip_address[3],
- (unsigned) ipv6->local_ip_address[4],
- (unsigned) ipv6->local_ip_address[5],
- (unsigned) ipv6->local_ip_address[6],
- (unsigned) ipv6->local_ip_address[7],
- (unsigned) ipv6->remote_ip_address[0],
- (unsigned) ipv6->remote_ip_address[1],
- (unsigned) ipv6->remote_ip_address[2],
- (unsigned) ipv6->remote_ip_address[3],
- (unsigned) ipv6->remote_ip_address[4],
- (unsigned) ipv6->remote_ip_address[5],
- (unsigned) ipv6->remote_ip_address[6],
- (unsigned) ipv6->remote_ip_address[7],
+ grub_printf ("/IPv6(%02x:%02x:%02x:%02x:%02x:%02x:%02x:%02x,%02x:%02x:%02x:%02x:%02x:%02x:%02x:%02x,%u,%u,%x,%x",
+ (unsigned) grub_be_to_cpu16 (ipv6->local_ip_address[0]),
+ (unsigned) grub_be_to_cpu16 (ipv6->local_ip_address[1]),
+ (unsigned) grub_be_to_cpu16 (ipv6->local_ip_address[2]),
+ (unsigned) grub_be_to_cpu16 (ipv6->local_ip_address[3]),
+ (unsigned) grub_be_to_cpu16 (ipv6->local_ip_address[4]),
+ (unsigned) grub_be_to_cpu16 (ipv6->local_ip_address[5]),
+ (unsigned) grub_be_to_cpu16 (ipv6->local_ip_address[6]),
+ (unsigned) grub_be_to_cpu16 (ipv6->local_ip_address[7]),
+ (unsigned) grub_be_to_cpu16 (ipv6->remote_ip_address[0]),
+ (unsigned) grub_be_to_cpu16 (ipv6->remote_ip_address[1]),
+ (unsigned) grub_be_to_cpu16 (ipv6->remote_ip_address[2]),
+ (unsigned) grub_be_to_cpu16 (ipv6->remote_ip_address[3]),
+ (unsigned) grub_be_to_cpu16 (ipv6->remote_ip_address[4]),
+ (unsigned) grub_be_to_cpu16 (ipv6->remote_ip_address[5]),
+ (unsigned) grub_be_to_cpu16 (ipv6->remote_ip_address[6]),
+ (unsigned) grub_be_to_cpu16 (ipv6->remote_ip_address[7]),
(unsigned) ipv6->local_port,
(unsigned) ipv6->remote_port,
(unsigned) ipv6->protocol,
(unsigned) ipv6->static_ip_address);
+ if (len == sizeof (*ipv6))
+ {
+ grub_printf (",%u,%02x:%02x:%02x:%02x:%02x:%02x:%02x:%02x",
+ (unsigned) ipv6->prefix_length,
+ (unsigned) grub_be_to_cpu16 (ipv6->gateway_ip_address[0]),
+ (unsigned) grub_be_to_cpu16 (ipv6->gateway_ip_address[1]),
+ (unsigned) grub_be_to_cpu16 (ipv6->gateway_ip_address[2]),
+ (unsigned) grub_be_to_cpu16 (ipv6->gateway_ip_address[3]),
+ (unsigned) grub_be_to_cpu16 (ipv6->gateway_ip_address[4]),
+ (unsigned) grub_be_to_cpu16 (ipv6->gateway_ip_address[5]),
+ (unsigned) grub_be_to_cpu16 (ipv6->gateway_ip_address[6]),
+ (unsigned) grub_be_to_cpu16 (ipv6->gateway_ip_address[7]));
+ }
+ grub_printf (")");
}
break;
case GRUB_EFI_INFINIBAND_DEVICE_PATH_SUBTYPE:
@@ -856,6 +883,39 @@ grub_efi_print_device_path (grub_efi_device_path_t *dp)
dump_vendor_path ("Messaging",
(grub_efi_vendor_device_path_t *) dp);
break;
+ case GRUB_EFI_URI_DEVICE_PATH_SUBTYPE:
+ {
+ grub_efi_uri_device_path_t *uri
+ = (grub_efi_uri_device_path_t *) dp;
+ grub_printf ("/URI(%s)", uri->uri);
+ }
+ break;
+ case GRUB_EFI_DNS_DEVICE_PATH_SUBTYPE:
+ {
+ grub_efi_dns_device_path_t *dns
+ = (grub_efi_dns_device_path_t *) dp;
+ if (dns->is_ipv6)
+ {
+ grub_printf ("/DNS(%02x:%02x:%02x:%02x:%02x:%02x:%02x:%02x)",
+ (grub_uint16_t)(grub_be_to_cpu32(dns->dns_server_ip[0].addr[0]) >> 16),
+ (grub_uint16_t)(grub_be_to_cpu32(dns->dns_server_ip[0].addr[0])),
+ (grub_uint16_t)(grub_be_to_cpu32(dns->dns_server_ip[0].addr[1]) >> 16),
+ (grub_uint16_t)(grub_be_to_cpu32(dns->dns_server_ip[0].addr[1])),
+ (grub_uint16_t)(grub_be_to_cpu32(dns->dns_server_ip[0].addr[2]) >> 16),
+ (grub_uint16_t)(grub_be_to_cpu32(dns->dns_server_ip[0].addr[2])),
+ (grub_uint16_t)(grub_be_to_cpu32(dns->dns_server_ip[0].addr[3]) >> 16),
+ (grub_uint16_t)(grub_be_to_cpu32(dns->dns_server_ip[0].addr[3])));
+ }
+ else
+ {
+ grub_printf ("/DNS(%d.%d.%d.%d)",
+ dns->dns_server_ip[0].v4.addr[0],
+ dns->dns_server_ip[0].v4.addr[1],
+ dns->dns_server_ip[0].v4.addr[2],
+ dns->dns_server_ip[0].v4.addr[3]);
+ }
+ }
+ break;
default:
grub_printf ("/UnknownMessaging(%x)", (unsigned) subtype);
break;
diff --git a/grub-core/net/drivers/efi/efinet.c b/grub-core/net/drivers/efi/efinet.c
index cc02008f5..f6353ed27 100644
--- a/grub-core/net/drivers/efi/efinet.c
+++ b/grub-core/net/drivers/efi/efinet.c
@@ -24,6 +24,7 @@
#include <grub/efi/efi.h>
#include <grub/i18n.h>
#include <grub/net/netbuff.h>
+#include <grub/env.h>
GRUB_MOD_LICENSE ("GPLv3+");
@@ -345,7 +346,7 @@ grub_efinet_findcards (void)
}
static grub_efi_handle_t
-grub_efi_locate_device_path (grub_efi_guid_t *protocol, grub_efi_device_path_t *device_path,
+grub_efi_locate_device_path (grub_guid_t *protocol, grub_efi_device_path_t *device_path,
grub_efi_device_path_t **r_device_path)
{
grub_efi_handle_t handle;
@@ -498,6 +499,17 @@ grub_efinet_create_dhcp_ack_from_device_path (grub_efi_device_path_t *dp, int *u
ldp = grub_efi_find_last_device_path (ddp);
+ /* Skip the DNS Device */
+ if (GRUB_EFI_DEVICE_PATH_TYPE (ldp) == GRUB_EFI_MESSAGING_DEVICE_PATH_TYPE
+ && GRUB_EFI_DEVICE_PATH_SUBTYPE (ldp) == GRUB_EFI_DNS_DEVICE_PATH_SUBTYPE)
+ {
+ ldp->type = GRUB_EFI_END_DEVICE_PATH_TYPE;
+ ldp->subtype = GRUB_EFI_END_ENTIRE_DEVICE_PATH_SUBTYPE;
+ ldp->length = sizeof (*ldp);
+
+ ldp = grub_efi_find_last_device_path (ddp);
+ }
+
if (GRUB_EFI_DEVICE_PATH_TYPE (ldp) != GRUB_EFI_MESSAGING_DEVICE_PATH_TYPE
|| (GRUB_EFI_DEVICE_PATH_SUBTYPE (ldp) != GRUB_EFI_IPV4_DEVICE_PATH_SUBTYPE
&& GRUB_EFI_DEVICE_PATH_SUBTYPE (ldp) != GRUB_EFI_IPV6_DEVICE_PATH_SUBTYPE))
@@ -765,6 +777,7 @@ grub_efi_net_config_real (grub_efi_handle_t hnd, char **device,
if (GRUB_EFI_DEVICE_PATH_TYPE (ldp) != GRUB_EFI_MESSAGING_DEVICE_PATH_TYPE
|| (GRUB_EFI_DEVICE_PATH_SUBTYPE (ldp) != GRUB_EFI_IPV4_DEVICE_PATH_SUBTYPE
&& GRUB_EFI_DEVICE_PATH_SUBTYPE (ldp) != GRUB_EFI_IPV6_DEVICE_PATH_SUBTYPE
+ && GRUB_EFI_DEVICE_PATH_SUBTYPE (ldp) != GRUB_EFI_DNS_DEVICE_PATH_SUBTYPE
&& GRUB_EFI_DEVICE_PATH_SUBTYPE (ldp) != GRUB_EFI_URI_DEVICE_PATH_SUBTYPE))
continue;
dup_dp = grub_efi_duplicate_device_path (dp);
@@ -779,6 +792,15 @@ grub_efi_net_config_real (grub_efi_handle_t hnd, char **device,
dup_ldp->length = sizeof (*dup_ldp);
}
+ dup_ldp = grub_efi_find_last_device_path (dup_dp);
+ if (GRUB_EFI_DEVICE_PATH_SUBTYPE (dup_ldp) == GRUB_EFI_DNS_DEVICE_PATH_SUBTYPE)
+ {
+ dup_ldp = grub_efi_find_last_device_path (dup_dp);
+ dup_ldp->type = GRUB_EFI_END_DEVICE_PATH_TYPE;
+ dup_ldp->subtype = GRUB_EFI_END_ENTIRE_DEVICE_PATH_SUBTYPE;
+ dup_ldp->length = sizeof (*dup_ldp);
+ }
+
dup_ldp = grub_efi_find_last_device_path (dup_dp);
dup_ldp->type = GRUB_EFI_END_DEVICE_PATH_TYPE;
dup_ldp->subtype = GRUB_EFI_END_ENTIRE_DEVICE_PATH_SUBTYPE;
@@ -860,6 +882,9 @@ grub_efi_net_config_real (grub_efi_handle_t hnd, char **device,
GRUB_MOD_INIT(efinet)
{
+ if (grub_efi_net_config)
+ return;
+
grub_efinet_findcards ();
grub_efi_net_config = grub_efi_net_config_real;
}
@@ -871,5 +896,7 @@ GRUB_MOD_FINI(efinet)
FOR_NET_CARDS_SAFE (card, next)
if (card->driver == &efidriver)
grub_net_card_unregister (card);
+
+ grub_efi_net_config = NULL;
}
diff --git a/grub-core/net/efi/dhcp.c b/grub-core/net/efi/dhcp.c
new file mode 100644
index 000000000..77ef932bf
--- /dev/null
+++ b/grub-core/net/efi/dhcp.c
@@ -0,0 +1,399 @@
+#include <grub/mm.h>
+#include <grub/command.h>
+#include <grub/efi/api.h>
+#include <grub/efi/efi.h>
+#include <grub/misc.h>
+#include <grub/net/efi.h>
+#include <grub/charset.h>
+
+#ifdef GRUB_EFI_NET_DEBUG
+static void
+dhcp4_mode_print (grub_efi_dhcp4_mode_data_t *mode)
+{
+ switch (mode->state)
+ {
+ case GRUB_EFI_DHCP4_STOPPED:
+ grub_printf ("STATE: STOPPED\n");
+ break;
+ case GRUB_EFI_DHCP4_INIT:
+ grub_printf ("STATE: INIT\n");
+ break;
+ case GRUB_EFI_DHCP4_SELECTING:
+ grub_printf ("STATE: SELECTING\n");
+ break;
+ case GRUB_EFI_DHCP4_REQUESTING:
+ grub_printf ("STATE: REQUESTING\n");
+ break;
+ case GRUB_EFI_DHCP4_BOUND:
+ grub_printf ("STATE: BOUND\n");
+ break;
+ case GRUB_EFI_DHCP4_RENEWING:
+ grub_printf ("STATE: RENEWING\n");
+ break;
+ case GRUB_EFI_DHCP4_REBINDING:
+ grub_printf ("STATE: REBINDING\n");
+ break;
+ case GRUB_EFI_DHCP4_INIT_REBOOT:
+ grub_printf ("STATE: INIT_REBOOT\n");
+ break;
+ case GRUB_EFI_DHCP4_REBOOTING:
+ grub_printf ("STATE: REBOOTING\n");
+ break;
+ default:
+ grub_printf ("STATE: UNKNOWN\n");
+ break;
+ }
+
+ grub_printf ("CLIENT_ADDRESS: %u.%u.%u.%u\n",
+ mode->client_address[0],
+ mode->client_address[1],
+ mode->client_address[2],
+ mode->client_address[3]);
+ grub_printf ("SERVER_ADDRESS: %u.%u.%u.%u\n",
+ mode->server_address[0],
+ mode->server_address[1],
+ mode->server_address[2],
+ mode->server_address[3]);
+ grub_printf ("SUBNET_MASK: %u.%u.%u.%u\n",
+ mode->subnet_mask[0],
+ mode->subnet_mask[1],
+ mode->subnet_mask[2],
+ mode->subnet_mask[3]);
+ grub_printf ("ROUTER_ADDRESS: %u.%u.%u.%u\n",
+ mode->router_address[0],
+ mode->router_address[1],
+ mode->router_address[2],
+ mode->router_address[3]);
+}
+#endif
+
+static grub_efi_ipv4_address_t *
+grub_efi_dhcp4_parse_dns (grub_efi_dhcp4_protocol_t *dhcp4, grub_efi_dhcp4_packet_t *reply_packet)
+{
+ grub_efi_dhcp4_packet_option_t **option_list;
+ grub_efi_status_t status;
+ grub_efi_uint32_t option_count = 0;
+ grub_efi_uint32_t i;
+
+ status = dhcp4->parse (dhcp4, reply_packet, &option_count, NULL);
+
+ if (status != GRUB_EFI_BUFFER_TOO_SMALL)
+ return NULL;
+
+ option_list = grub_calloc (option_count, sizeof(*option_list));
+ if (!option_list)
+ return NULL;
+
+ status = dhcp4->parse (dhcp4, reply_packet, &option_count, option_list);
+ if (status != GRUB_EFI_SUCCESS)
+ {
+ grub_free (option_list);
+ return NULL;
+ }
+
+ for (i = 0; i < option_count; ++i)
+ {
+ if (option_list[i]->op_code == 6)
+ {
+ grub_efi_ipv4_address_t *dns_address;
+
+ if (((option_list[i]->length & 0x3) != 0) || (option_list[i]->length == 0))
+ continue;
+
+ /* We only contact primary dns */
+ dns_address = grub_malloc (sizeof (*dns_address));
+ if (!dns_address)
+ {
+ grub_free (option_list);
+ return NULL;
+ }
+ grub_memcpy (dns_address, option_list[i]->data, sizeof (dns_address));
+ grub_free (option_list);
+ return dns_address;
+ }
+ }
+
+ grub_free (option_list);
+ return NULL;
+}
+
+#if 0
+/* Somehow this doesn't work ... */
+static grub_err_t
+grub_cmd_efi_bootp (struct grub_command *cmd __attribute__ ((unused)),
+ int argc __attribute__ ((unused)),
+ char **args __attribute__ ((unused)))
+{
+ struct grub_efi_net_device *dev;
+ for (dev = net_devices; dev; dev = dev->next)
+ {
+ grub_efi_pxe_t *pxe = dev->ip4_pxe;
+ grub_efi_pxe_mode_t *mode = pxe->mode;
+ grub_efi_status_t status;
+
+ if (!mode->started)
+ {
+ status = pxe->start (pxe, 0);
+
+ if (status != GRUB_EFI_SUCCESS)
+ grub_printf ("Couldn't start PXE\n");
+ }
+
+ status = pxe->dhcp (pxe, 0);
+ if (status != GRUB_EFI_SUCCESS)
+ {
+ grub_printf ("dhcp4 configure failed, %d\n", (int)status);
+ continue;
+ }
+
+ dev->prefer_ip6 = 0;
+ }
+
+ return GRUB_ERR_NONE;
+}
+#endif
+
+static grub_err_t
+grub_cmd_efi_bootp (struct grub_command *cmd __attribute__ ((unused)),
+ int argc,
+ char **args)
+{
+ struct grub_efi_net_device *netdev;
+
+ for (netdev = net_devices; netdev; netdev = netdev->next)
+ {
+ grub_efi_status_t status;
+ grub_efi_dhcp4_mode_data_t mode;
+ grub_efi_dhcp4_config_data_t config;
+ grub_efi_dhcp4_packet_option_t *options;
+ grub_efi_ipv4_address_t *dns_address;
+ grub_efi_net_ip_manual_address_t net_ip;
+ grub_efi_net_ip_address_t ip_addr;
+ grub_efi_net_interface_t *inf = NULL;
+
+ if (argc > 0 && grub_strcmp (netdev->card_name, args[0]) != 0)
+ continue;
+
+ grub_memset (&config, 0, sizeof(config));
+
+ config.option_count = 1;
+ options = grub_malloc (sizeof(*options) + 2);
+ /* Parameter request list */
+ options->op_code = 55;
+ options->length = 3;
+ /* subnet mask */
+ options->data[0] = 1;
+ /* router */
+ options->data[1] = 3;
+ /* DNS */
+ options->data[2] = 6;
+ config.option_list = &options;
+
+ /* FIXME: What if the dhcp has bounded */
+ status = netdev->dhcp4->configure (netdev->dhcp4, &config);
+ grub_free (options);
+ if (status != GRUB_EFI_SUCCESS)
+ {
+ grub_printf ("dhcp4 configure failed, %d\n", (int)status);
+ continue;
+ }
+
+ status = netdev->dhcp4->start (netdev->dhcp4, NULL);
+ if (status != GRUB_EFI_SUCCESS)
+ {
+ grub_printf ("dhcp4 start failed, %d\n", (int)status);
+ continue;
+ }
+
+ status = netdev->dhcp4->get_mode_data (netdev->dhcp4, &mode);
+ if (status != GRUB_EFI_SUCCESS)
+ {
+ grub_printf ("dhcp4 get mode failed, %d\n", (int)status);
+ continue;
+ }
+
+#ifdef GRUB_EFI_NET_DEBUG
+ dhcp4_mode_print (&mode);
+#endif
+
+ for (inf = netdev->net_interfaces; inf; inf = inf->next)
+ if (inf->prefer_ip6 == 0)
+ break;
+
+ grub_memcpy (net_ip.ip4.address, mode.client_address, sizeof (net_ip.ip4.address));
+ grub_memcpy (net_ip.ip4.subnet_mask, mode.subnet_mask, sizeof (net_ip.ip4.subnet_mask));
+
+ if (!inf)
+ {
+ char *name = grub_xasprintf ("%s:dhcp", netdev->card_name);
+
+ net_ip.is_ip6 = 0;
+ inf = grub_efi_net_create_interface (netdev,
+ name,
+ &net_ip,
+ 1);
+ grub_free (name);
+ }
+ else
+ {
+ efi_net_interface_set_address (inf, &net_ip, 1);
+ }
+
+ grub_memcpy (ip_addr.ip4, mode.router_address, sizeof (ip_addr.ip4));
+ efi_net_interface_set_gateway (inf, &ip_addr);
+
+ dns_address = grub_efi_dhcp4_parse_dns (netdev->dhcp4, mode.reply_packet);
+ if (dns_address)
+ efi_net_interface_set_dns (inf, (grub_efi_net_ip_address_t *)&dns_address);
+
+ }
+
+ return GRUB_ERR_NONE;
+}
+
+
+static grub_err_t
+grub_cmd_efi_bootp6 (struct grub_command *cmd __attribute__ ((unused)),
+ int argc,
+ char **args)
+{
+ struct grub_efi_net_device *dev;
+ grub_efi_uint32_t ia_id;
+
+ for (dev = net_devices, ia_id = 0; dev; dev = dev->next, ia_id++)
+ {
+ grub_efi_dhcp6_config_data_t config;
+ grub_efi_dhcp6_packet_option_t *option_list[1];
+ grub_efi_dhcp6_packet_option_t *opt;
+ grub_efi_status_t status;
+ grub_efi_dhcp6_mode_data_t mode;
+ grub_efi_dhcp6_retransmission_t retrans;
+ grub_efi_net_ip_manual_address_t net_ip;
+ grub_efi_boot_services_t *b = grub_efi_system_table->boot_services;
+ grub_efi_net_interface_t *inf = NULL;
+
+ if (argc > 0 && grub_strcmp (dev->card_name, args[0]) != 0)
+ continue;
+
+ opt = grub_malloc (sizeof(*opt) + 2 * sizeof (grub_efi_uint16_t));
+
+#define GRUB_EFI_DHCP6_OPT_ORO 6
+
+ opt->op_code = grub_cpu_to_be16_compile_time (GRUB_EFI_DHCP6_OPT_ORO);
+ opt->op_len = grub_cpu_to_be16_compile_time (2 * sizeof (grub_efi_uint16_t));
+
+#define GRUB_EFI_DHCP6_OPT_BOOT_FILE_URL 59
+#define GRUB_EFI_DHCP6_OPT_DNS_SERVERS 23
+
+ grub_set_unaligned16 (opt->data, grub_cpu_to_be16_compile_time(GRUB_EFI_DHCP6_OPT_BOOT_FILE_URL));
+ grub_set_unaligned16 (opt->data + 1 * sizeof (grub_efi_uint16_t),
+ grub_cpu_to_be16_compile_time(GRUB_EFI_DHCP6_OPT_DNS_SERVERS));
+
+ option_list[0] = opt;
+ retrans.irt = 4;
+ retrans.mrc = 4;
+ retrans.mrt = 32;
+ retrans.mrd = 60;
+
+ config.dhcp6_callback = NULL;
+ config.callback_context = NULL;
+ config.option_count = 1;
+ config.option_list = option_list;
+ config.ia_descriptor.ia_id = ia_id;
+ config.ia_descriptor.type = GRUB_EFI_DHCP6_IA_TYPE_NA;
+ config.ia_info_event = NULL;
+ config.reconfigure_accept = 0;
+ config.rapid_commit = 0;
+ config.solicit_retransmission = &retrans;
+
+ status = dev->dhcp6->configure (dev->dhcp6, &config);
+ grub_free (opt);
+ if (status != GRUB_EFI_SUCCESS)
+ {
+ grub_printf ("dhcp6 configure failed, %d\n", (int)status);
+ continue;
+ }
+ status = dev->dhcp6->start (dev->dhcp6);
+ if (status != GRUB_EFI_SUCCESS)
+ {
+ grub_printf ("dhcp6 start failed, %d\n", (int)status);
+ continue;
+ }
+
+ status = dev->dhcp6->get_mode_data (dev->dhcp6, &mode, NULL);
+ if (status != GRUB_EFI_SUCCESS)
+ {
+ grub_printf ("dhcp4 get mode failed, %d\n", (int)status);
+ continue;
+ }
+
+ for (inf = dev->net_interfaces; inf; inf = inf->next)
+ if (inf->prefer_ip6 == 1)
+ break;
+
+ grub_memcpy (net_ip.ip6.address, mode.ia->ia_address[0].ip_address, sizeof (net_ip.ip6.address));
+ net_ip.ip6.prefix_length = 64;
+ net_ip.ip6.is_anycast = 0;
+ net_ip.is_ip6 = 1;
+
+ if (!inf)
+ {
+ char *name = grub_xasprintf ("%s:dhcp", dev->card_name);
+
+ inf = grub_efi_net_create_interface (dev,
+ name,
+ &net_ip,
+ 1);
+ grub_free (name);
+ }
+ else
+ {
+ efi_net_interface_set_address (inf, &net_ip, 1);
+ }
+
+ {
+ grub_efi_uint32_t count = 0;
+ grub_efi_dhcp6_packet_option_t **options = NULL;
+ grub_efi_uint32_t i;
+
+ status = dev->dhcp6->parse (dev->dhcp6, mode.ia->reply_packet, &count, NULL);
+
+ if (status == GRUB_EFI_BUFFER_TOO_SMALL && count)
+ {
+ options = grub_calloc (count, sizeof(*options));
+ if (!options)
+ return grub_error (GRUB_ERR_OUT_OF_RANGE, N_("overflow is detected"));
+ status = dev->dhcp6->parse (dev->dhcp6, mode.ia->reply_packet, &count, options);
+ }
+
+ if (status != GRUB_EFI_SUCCESS)
+ {
+ if (options)
+ grub_free (options);
+ continue;
+ }
+
+ for (i = 0; i < count; ++i)
+ {
+ if (options[i]->op_code == grub_cpu_to_be16_compile_time(GRUB_EFI_DHCP6_OPT_DNS_SERVERS))
+ {
+ grub_efi_net_ip_address_t dns;
+ grub_memcpy (dns.ip6, options[i]->data, sizeof(net_ip.ip6));
+ efi_net_interface_set_dns (inf, &dns);
+ break;
+ }
+ }
+
+ if (options)
+ grub_free (options);
+ }
+
+ b->free_pool (mode.client_id);
+ b->free_pool (mode.ia);
+ }
+
+ return GRUB_ERR_NONE;
+}
+
+grub_command_func_t grub_efi_net_bootp = grub_cmd_efi_bootp;
+grub_command_func_t grub_efi_net_bootp6 = grub_cmd_efi_bootp6;
diff --git a/grub-core/net/efi/http.c b/grub-core/net/efi/http.c
new file mode 100644
index 000000000..70affd8ed
--- /dev/null
+++ b/grub-core/net/efi/http.c
@@ -0,0 +1,424 @@
+
+#include <grub/efi/api.h>
+#include <grub/efi/efi.h>
+#include <grub/misc.h>
+#include <grub/net/efi.h>
+#include <grub/charset.h>
+#include <grub/safemath.h>
+
+static void
+http_configure (struct grub_efi_net_device *dev, int prefer_ip6)
+{
+ grub_efi_http_config_data_t http_config;
+ grub_efi_httpv4_access_point_t httpv4_node;
+ grub_efi_httpv6_access_point_t httpv6_node;
+ grub_efi_status_t status;
+
+ grub_efi_http_t *http = dev->http;
+
+ grub_memset (&http_config, 0, sizeof(http_config));
+ http_config.http_version = GRUB_EFI_HTTPVERSION11;
+ http_config.timeout_millisec = 5000;
+
+ if (prefer_ip6)
+ {
+ grub_efi_uintn_t sz;
+ grub_efi_ip6_config_manual_address_t manual_address;
+
+ http_config.local_address_is_ipv6 = 1;
+ sz = sizeof (manual_address);
+ status = dev->ip6_config->get_data (dev->ip6_config,
+ GRUB_EFI_IP6_CONFIG_DATA_TYPE_MANUAL_ADDRESS,
+ &sz, &manual_address);
+
+ if (status == GRUB_EFI_NOT_FOUND)
+ {
+ grub_printf ("The MANUAL ADDRESS is not found\n");
+ }
+
+ /* FIXME: The manual interface would return BUFFER TOO SMALL !!! */
+ if (status != GRUB_EFI_SUCCESS)
+ {
+ grub_printf ("??? %d\n",(int) status);
+ return;
+ }
+
+ grub_memcpy (httpv6_node.local_address, manual_address.address, sizeof (httpv6_node.local_address));
+ httpv6_node.local_port = 0;
+ http_config.access_point.ipv6_node = &httpv6_node;
+ }
+ else
+ {
+ http_config.local_address_is_ipv6 = 0;
+ grub_memset (&httpv4_node, 0, sizeof(httpv4_node));
+ httpv4_node.use_default_address = 1;
+
+ /* Use random port here */
+ /* See TcpBind() in edk2/NetworkPkg/TcpDxe/TcpDispatcher.c */
+ httpv4_node.local_port = 0;
+ http_config.access_point.ipv4_node = &httpv4_node;
+ }
+
+ status = http->configure (http, &http_config);
+
+ if (status == GRUB_EFI_ALREADY_STARTED)
+ {
+ /* XXX: This hangs HTTPS boot */
+#if 0
+ if (http->configure (http, NULL) != GRUB_EFI_SUCCESS)
+ {
+ grub_error (GRUB_ERR_IO, N_("couldn't reset http instance"));
+ grub_print_error ();
+ return;
+ }
+ status = http->configure (http, &http_config);
+#endif
+ return;
+ }
+
+ if (status != GRUB_EFI_SUCCESS)
+ {
+ grub_error (GRUB_ERR_IO, N_("couldn't configure http protocol, reason: %d"), (int)status);
+ grub_print_error ();
+ return ;
+ }
+}
+
+static grub_efi_boolean_t request_callback_done;
+static grub_efi_boolean_t response_callback_done;
+
+static void __grub_efi_api
+grub_efi_http_request_callback (grub_efi_event_t event __attribute__ ((unused)),
+ void *context __attribute__ ((unused)))
+{
+ request_callback_done = 1;
+}
+
+static void __grub_efi_api
+grub_efi_http_response_callback (grub_efi_event_t event __attribute__ ((unused)),
+ void *context __attribute__ ((unused)))
+{
+ response_callback_done = 1;
+}
+
+static grub_err_t
+efihttp_request (grub_efi_http_t *http, char *server, char *name, int use_https, int headeronly, grub_off_t *file_size)
+{
+ grub_efi_http_request_data_t request_data;
+ grub_efi_http_message_t request_message;
+ grub_efi_http_token_t request_token;
+ grub_efi_http_response_data_t response_data;
+ grub_efi_http_message_t response_message;
+ grub_efi_http_token_t response_token;
+ grub_efi_http_header_t request_headers[3];
+
+ grub_efi_status_t status;
+ grub_efi_boot_services_t *b = grub_efi_system_table->boot_services;
+ char *url = NULL;
+
+ request_headers[0].field_name = (grub_efi_char8_t *)"Host";
+ request_headers[0].field_value = (grub_efi_char8_t *)server;
+ request_headers[1].field_name = (grub_efi_char8_t *)"Accept";
+ request_headers[1].field_value = (grub_efi_char8_t *)"*/*";
+ request_headers[2].field_name = (grub_efi_char8_t *)"User-Agent";
+ request_headers[2].field_value = (grub_efi_char8_t *)"UefiHttpBoot/1.0";
+
+ {
+ grub_efi_ipv6_address_t address;
+ const char *rest;
+ grub_efi_char16_t *ucs2_url;
+ grub_size_t url_len, ucs2_url_len;
+ const char *protocol = (use_https == 1) ? "https" : "http";
+ grub_size_t sz;
+
+ if (grub_efi_string_to_ip6_address (server, &address, &rest) && *rest == 0)
+ url = grub_xasprintf ("%s://[%s]%s", protocol, server, name);
+ else
+ url = grub_xasprintf ("%s://%s%s", protocol, server, name);
+
+ if (!url)
+ {
+ return grub_errno;
+ }
+
+ url_len = grub_strlen (url);
+ if (grub_mul (url_len, GRUB_MAX_UTF16_PER_UTF8, &ucs2_url_len) ||
+ grub_add (ucs2_url_len, 1, &sz))
+ return GRUB_ERR_OUT_OF_RANGE;
+
+ ucs2_url = grub_calloc (sz, sizeof (ucs2_url[0]));
+
+ if (!ucs2_url)
+ {
+ grub_free (url);
+ return grub_errno;
+ }
+
+ ucs2_url_len = grub_utf8_to_utf16 (ucs2_url, ucs2_url_len, (grub_uint8_t *)url, url_len, NULL); /* convert string format from ascii to usc2 */
+ ucs2_url[ucs2_url_len] = 0;
+ grub_free (url);
+ request_data.url = ucs2_url;
+ }
+
+ request_data.method = (headeronly > 0) ? GRUB_EFI_HTTPMETHODHEAD : GRUB_EFI_HTTPMETHODGET;
+
+ request_message.data.request = &request_data;
+ request_message.header_count = 3;
+ request_message.headers = request_headers;
+ request_message.body_length = 0;
+ request_message.body = NULL;
+
+ /* request token */
+ request_token.event = NULL;
+ request_token.status = GRUB_EFI_NOT_READY;
+ request_token.message = &request_message;
+
+ request_callback_done = 0;
+ status = b->create_event (
+ GRUB_EFI_EVT_NOTIFY_SIGNAL,
+ GRUB_EFI_TPL_CALLBACK,
+ grub_efi_http_request_callback,
+ NULL,
+ &request_token.event);
+
+ if (status != GRUB_EFI_SUCCESS)
+ {
+ grub_free (request_data.url);
+ return grub_error (GRUB_ERR_IO, "Fail to create an event! status=0x%" PRIxGRUB_SIZE, status);
+ }
+
+ status = http->request (http, &request_token);
+
+ if (status != GRUB_EFI_SUCCESS)
+ {
+ b->close_event (request_token.event);
+ grub_free (request_data.url);
+ return grub_error (GRUB_ERR_IO, "Fail to send a request! status=0x%" PRIxGRUB_SIZE, status);
+ }
+ /* TODO: Add Timeout */
+ while (!request_callback_done)
+ http->poll (http);
+
+ response_data.status_code = GRUB_EFI_HTTP_STATUS_UNSUPPORTED_STATUS;
+ response_message.data.response = &response_data;
+ /* herader_count will be updated by the HTTP driver on response */
+ response_message.header_count = 0;
+ /* headers will be populated by the driver on response */
+ response_message.headers = NULL;
+ /* use zero BodyLength to only receive the response headers */
+ response_message.body_length = 0;
+ response_message.body = NULL;
+ response_token.event = NULL;
+
+ status = b->create_event (
+ GRUB_EFI_EVT_NOTIFY_SIGNAL,
+ GRUB_EFI_TPL_CALLBACK,
+ grub_efi_http_response_callback,
+ NULL,
+ &response_token.event);
+
+ if (status != GRUB_EFI_SUCCESS)
+ {
+ b->close_event (request_token.event);
+ grub_free (request_data.url);
+ return grub_error (GRUB_ERR_IO, "Fail to create an event! status=0x%" PRIxGRUB_SIZE, status);
+ }
+
+ response_token.status = GRUB_EFI_SUCCESS;
+ response_token.message = &response_message;
+
+ /* wait for HTTP response */
+ response_callback_done = 0;
+ status = http->response (http, &response_token);
+
+ if (status != GRUB_EFI_SUCCESS)
+ {
+ b->close_event (response_token.event);
+ b->close_event (request_token.event);
+ grub_free (request_data.url);
+ return grub_error (GRUB_ERR_IO, "Fail to receive a response! status=%d\n", (int)status);
+ }
+
+ /* TODO: Add Timeout */
+ while (!response_callback_done)
+ http->poll (http);
+
+ if (response_message.data.response->status_code != GRUB_EFI_HTTP_STATUS_200_OK)
+ {
+ grub_efi_http_status_code_t status_code = response_message.data.response->status_code;
+
+ if (response_message.headers)
+ b->free_pool (response_message.headers);
+ b->close_event (response_token.event);
+ b->close_event (request_token.event);
+ grub_free (request_data.url);
+ if (status_code == GRUB_EFI_HTTP_STATUS_404_NOT_FOUND)
+ {
+ return grub_error (GRUB_ERR_FILE_NOT_FOUND, _("file `%s' not found"), name);
+ }
+ else
+ {
+ return grub_error (GRUB_ERR_NET_UNKNOWN_ERROR,
+ _("unsupported uefi http status code 0x%x"), status_code);
+ }
+ }
+
+ if (file_size)
+ {
+ int i;
+ /* parse the length of the file from the ContentLength header */
+ for (*file_size = 0, i = 0; i < (int)response_message.header_count; ++i)
+ {
+ if (!grub_strcmp((const char*)response_message.headers[i].field_name, "Content-Length"))
+ {
+ *file_size = grub_strtoul((const char*)response_message.headers[i].field_value, 0, 10);
+ break;
+ }
+ }
+ }
+
+ if (response_message.headers)
+ b->free_pool (response_message.headers);
+ b->close_event (response_token.event);
+ b->close_event (request_token.event);
+ grub_free (request_data.url);
+
+ return GRUB_ERR_NONE;
+}
+
+static grub_ssize_t
+efihttp_read (struct grub_efi_net_device *dev,
+ char *buf,
+ grub_size_t len)
+{
+ grub_efi_http_message_t response_message;
+ grub_efi_http_token_t response_token;
+
+ grub_efi_status_t status;
+ grub_size_t sum = 0;
+ grub_efi_boot_services_t *b = grub_efi_system_table->boot_services;
+ grub_efi_http_t *http = dev->http;
+
+ if (!len)
+ {
+ grub_error (GRUB_ERR_BUG, "Invalid arguments to EFI HTTP Read");
+ return -1;
+ }
+
+ b->create_event (
+ GRUB_EFI_EVT_NOTIFY_SIGNAL,
+ GRUB_EFI_TPL_CALLBACK,
+ grub_efi_http_response_callback,
+ NULL,
+ &response_token.event);
+
+ while (len)
+ {
+ response_message.data.response = NULL;
+ response_message.header_count = 0;
+ response_message.headers = NULL;
+ response_message.body_length = len;
+ response_message.body = buf;
+
+ response_token.message = &response_message;
+ response_token.status = GRUB_EFI_NOT_READY;
+
+ response_callback_done = 0;
+
+ status = http->response (http, &response_token);
+ if (status != GRUB_EFI_SUCCESS)
+ {
+ b->close_event (response_token.event);
+ grub_error (GRUB_ERR_IO, "Error! status=%d\n", (int)status);
+ return -1;
+ }
+
+ while (!response_callback_done)
+ http->poll (http);
+
+ sum += response_message.body_length;
+ buf += response_message.body_length;
+ len -= response_message.body_length;
+ }
+
+ b->close_event (response_token.event);
+
+ return sum;
+}
+
+static grub_err_t
+grub_efihttp_open (struct grub_efi_net_device *dev,
+ int prefer_ip6 __attribute__ ((unused)),
+ grub_file_t file,
+ const char *filename __attribute__ ((unused)),
+ int type)
+{
+ grub_err_t err;
+ grub_off_t size;
+ char *buf;
+
+ err = efihttp_request (dev->http, file->device->net->server, file->device->net->name, type, 1, 0);
+ if (err != GRUB_ERR_NONE)
+ return err;
+
+ err = efihttp_request (dev->http, file->device->net->server, file->device->net->name, type, 0, &size);
+ if (err != GRUB_ERR_NONE)
+ return err;
+
+ buf = grub_malloc (size);
+ efihttp_read (dev, buf, size);
+
+ file->size = size;
+ file->data = buf;
+ file->not_easily_seekable = 0;
+ file->device->net->offset = 0;
+
+ return GRUB_ERR_NONE;
+}
+
+static grub_err_t
+grub_efihttp_close (struct grub_efi_net_device *dev __attribute__ ((unused)),
+ int prefer_ip6 __attribute__ ((unused)),
+ grub_file_t file)
+{
+ if (file->data)
+ grub_free (file->data);
+
+ file->data = 0;
+ file->offset = 0;
+ file->size = 0;
+ file->device->net->offset = 0;
+ return GRUB_ERR_NONE;
+}
+
+static grub_ssize_t
+grub_efihttp_read (struct grub_efi_net_device *dev __attribute__((unused)),
+ int prefer_ip6 __attribute__((unused)),
+ grub_file_t file,
+ char *buf,
+ grub_size_t len)
+{
+ grub_size_t r = len;
+
+ if (!file->data || !buf || !len)
+ return 0;
+
+ if ((file->device->net->offset + len) > file->size)
+ r = file->size - file->device->net->offset;
+
+ if (r)
+ {
+ grub_memcpy (buf, (char *)file->data + file->device->net->offset, r);
+ file->device->net->offset += r;
+ }
+
+ return r;
+}
+
+struct grub_efi_net_io io_http =
+ {
+ .configure = http_configure,
+ .open = grub_efihttp_open,
+ .read = grub_efihttp_read,
+ .close = grub_efihttp_close
+ };
diff --git a/grub-core/net/efi/ip4_config.c b/grub-core/net/efi/ip4_config.c
new file mode 100644
index 000000000..eb78f8fde
--- /dev/null
+++ b/grub-core/net/efi/ip4_config.c
@@ -0,0 +1,409 @@
+
+#include <grub/efi/api.h>
+#include <grub/efi/efi.h>
+#include <grub/misc.h>
+#include <grub/net/efi.h>
+#include <grub/charset.h>
+#include <grub/safemath.h>
+
+char *
+grub_efi_hw_address_to_string (grub_efi_uint32_t hw_address_size, grub_efi_mac_address_t hw_address)
+{
+ char *hw_addr, *p;
+ int s;
+ int i;
+ grub_size_t sz;
+
+ if (grub_mul (hw_address_size, sizeof ("XX:") - 1, &sz) ||
+ grub_add (sz, 1, &sz))
+ return NULL;
+
+ hw_addr = grub_malloc (sz);
+ if (!hw_addr)
+ return NULL;
+
+ p = hw_addr;
+ s = sz;
+ for (i = 0; i < (int)hw_address_size; i++)
+ {
+ grub_snprintf (p, sz, "%02x:", hw_address[i]);
+ p += sizeof ("XX:") - 1;
+ s -= sizeof ("XX:") - 1;
+ }
+
+ hw_addr[sz - 2] = '\0';
+ return hw_addr;
+}
+
+char *
+grub_efi_ip4_address_to_string (grub_efi_ipv4_address_t *address)
+{
+ char *addr;
+
+ addr = grub_malloc (sizeof ("XXX.XXX.XXX.XXX"));
+ if (!addr)
+ return NULL;
+
+ /* FIXME: Use grub_xasprintf ? */
+ grub_snprintf (addr,
+ sizeof ("XXX.XXX.XXX.XXX"),
+ "%u.%u.%u.%u",
+ (*address)[0],
+ (*address)[1],
+ (*address)[2],
+ (*address)[3]);
+
+ return addr;
+}
+
+int
+grub_efi_string_to_ip4_address (const char *val, grub_efi_ipv4_address_t *address, const char **rest)
+{
+ grub_uint32_t newip = 0;
+ int i;
+ const char *ptr = val;
+
+ for (i = 0; i < 4; i++)
+ {
+ unsigned long t;
+ t = grub_strtoul (ptr, &ptr, 0);
+ if (grub_errno)
+ {
+ grub_errno = GRUB_ERR_NONE;
+ return 0;
+ }
+ if (*ptr != '.' && i == 0)
+ {
+ /* XXX: t is in host byte order */
+ newip = t;
+ break;
+ }
+ if (t & ~0xff)
+ return 0;
+ newip <<= 8;
+ newip |= t;
+ if (i != 3 && *ptr != '.')
+ return 0;
+ ptr++;
+ }
+
+ newip = grub_cpu_to_be32 (newip);
+
+ grub_memcpy (address, &newip, sizeof(*address));
+
+ if (rest)
+ *rest = (ptr - 1);
+ return 1;
+}
+
+static grub_efi_ip4_config2_interface_info_t *
+efi_ip4_config_interface_info (grub_efi_ip4_config2_protocol_t *ip4_config)
+{
+ grub_efi_uintn_t sz;
+ grub_efi_status_t status;
+ grub_efi_ip4_config2_interface_info_t *interface_info;
+
+ sz = sizeof (*interface_info) + sizeof (*interface_info->route_table);
+ interface_info = grub_malloc (sz);
+ if (!interface_info)
+ return NULL;
+
+ status = ip4_config->get_data (ip4_config,
+ GRUB_EFI_IP4_CONFIG2_DATA_TYPE_INTERFACEINFO,
+ &sz, interface_info);
+
+ if (status == GRUB_EFI_BUFFER_TOO_SMALL)
+ {
+ grub_free (interface_info);
+ interface_info = grub_malloc (sz);
+ status = ip4_config->get_data (ip4_config,
+ GRUB_EFI_IP4_CONFIG2_DATA_TYPE_INTERFACEINFO,
+ &sz, interface_info);
+ }
+
+ if (status != GRUB_EFI_SUCCESS)
+ {
+ grub_free (interface_info);
+ return NULL;
+ }
+
+ return interface_info;
+}
+
+static grub_efi_ip4_config2_manual_address_t *
+efi_ip4_config_manual_address (grub_efi_ip4_config2_protocol_t *ip4_config)
+{
+ grub_efi_uintn_t sz;
+ grub_efi_status_t status;
+ grub_efi_ip4_config2_manual_address_t *manual_address;
+
+ sz = sizeof (*manual_address);
+ manual_address = grub_malloc (sz);
+ if (!manual_address)
+ return NULL;
+
+ status = ip4_config->get_data (ip4_config,
+ GRUB_EFI_IP4_CONFIG2_DATA_TYPE_MANUAL_ADDRESS,
+ &sz, manual_address);
+
+ if (status != GRUB_EFI_SUCCESS)
+ {
+ grub_free (manual_address);
+ return NULL;
+ }
+
+ return manual_address;
+}
+
+char *
+grub_efi_ip4_interface_name (struct grub_efi_net_device *dev)
+{
+ grub_efi_ip4_config2_interface_info_t *interface_info;
+ char *name;
+
+ interface_info = efi_ip4_config_interface_info (dev->ip4_config);
+
+ if (!interface_info)
+ return NULL;
+
+ name = grub_malloc (GRUB_EFI_IP4_CONFIG2_INTERFACE_INFO_NAME_SIZE
+ * GRUB_MAX_UTF8_PER_UTF16 + 1);
+ *grub_utf16_to_utf8 ((grub_uint8_t *)name, interface_info->name,
+ GRUB_EFI_IP4_CONFIG2_INTERFACE_INFO_NAME_SIZE) = 0;
+ grub_free (interface_info);
+ return name;
+}
+
+static char *
+grub_efi_ip4_interface_hw_address (struct grub_efi_net_device *dev)
+{
+ grub_efi_ip4_config2_interface_info_t *interface_info;
+ char *hw_addr;
+
+ interface_info = efi_ip4_config_interface_info (dev->ip4_config);
+
+ if (!interface_info)
+ return NULL;
+
+ hw_addr = grub_efi_hw_address_to_string (interface_info->hw_address_size, interface_info->hw_address);
+ grub_free (interface_info);
+
+ return hw_addr;
+}
+
+static char *
+grub_efi_ip4_interface_address (struct grub_efi_net_device *dev)
+{
+ grub_efi_ip4_config2_manual_address_t *manual_address;
+ char *addr;
+
+ manual_address = efi_ip4_config_manual_address (dev->ip4_config);
+
+ if (!manual_address)
+ return NULL;
+
+ addr = grub_efi_ip4_address_to_string (&manual_address->address);
+ grub_free (manual_address);
+ return addr;
+}
+
+
+static int
+address_mask_size (grub_efi_ipv4_address_t *address)
+{
+ grub_uint8_t i;
+ grub_uint32_t u32_addr = grub_be_to_cpu32 (grub_get_unaligned32 (address));
+
+ if (u32_addr == 0)
+ return 0;
+
+ for (i = 0; i < 32 ; ++i)
+ {
+ if (u32_addr == ((0xffffffff >> i) << i))
+ return (32 - i);
+ }
+
+ return -1;
+}
+
+static char **
+grub_efi_ip4_interface_route_table (struct grub_efi_net_device *dev)
+{
+ grub_efi_ip4_config2_interface_info_t *interface_info;
+ char **ret;
+ int i, id;
+ grub_size_t sz;
+
+ interface_info = efi_ip4_config_interface_info (dev->ip4_config);
+ if (!interface_info)
+ return NULL;
+
+ if (grub_add (interface_info->route_table_size, 1, &sz))
+ {
+ grub_free (interface_info);
+ return NULL;
+ }
+
+ ret = grub_calloc (sz, sizeof (*ret));
+
+ if (!ret)
+ {
+ grub_free (interface_info);
+ return NULL;
+ }
+
+ id = 0;
+ for (i = 0; i < (int)interface_info->route_table_size; i++)
+ {
+ char *subnet, *gateway, *mask;
+ grub_uint32_t u32_subnet, u32_gateway;
+ int mask_size;
+ grub_efi_ip4_route_table_t *route_table = interface_info->route_table + i;
+ grub_efi_net_interface_t *inf;
+ char *interface_name = NULL;
+
+ for (inf = dev->net_interfaces; inf; inf = inf->next)
+ if (!inf->prefer_ip6)
+ interface_name = inf->name;
+
+ u32_gateway = grub_get_unaligned32 (&route_table->gateway_address);
+ gateway = grub_efi_ip4_address_to_string (&route_table->gateway_address);
+ u32_subnet = grub_get_unaligned32 (&route_table->subnet_address);
+ subnet = grub_efi_ip4_address_to_string (&route_table->subnet_address);
+ mask_size = address_mask_size (&route_table->subnet_mask);
+ mask = grub_efi_ip4_address_to_string (&route_table->subnet_mask);
+ if (u32_subnet && !u32_gateway && interface_name)
+ ret[id++] = grub_xasprintf ("%s:local %s/%d %s", dev->card_name, subnet, mask_size, interface_name);
+ else if (u32_subnet && u32_gateway)
+ ret[id++] = grub_xasprintf ("%s:gw %s/%d gw %s", dev->card_name, subnet, mask_size, gateway);
+ else if (!u32_subnet && u32_gateway)
+ ret[id++] = grub_xasprintf ("%s:default %s/%d gw %s", dev->card_name, subnet, mask_size, gateway);
+ grub_free (subnet);
+ grub_free (gateway);
+ grub_free (mask);
+ }
+
+ ret[id] = NULL;
+ grub_free (interface_info);
+ return ret;
+}
+
+static grub_efi_net_interface_t *
+grub_efi_ip4_interface_match (struct grub_efi_net_device *dev, grub_efi_net_ip_address_t *ip_address)
+{
+ grub_efi_ip4_config2_interface_info_t *interface_info;
+ grub_efi_net_interface_t *inf;
+ int i;
+ grub_efi_ipv4_address_t *address = &ip_address->ip4;
+
+ interface_info = efi_ip4_config_interface_info (dev->ip4_config);
+ if (!interface_info)
+ return NULL;
+
+ for (i = 0; i < (int)interface_info->route_table_size; i++)
+ {
+ grub_efi_ip4_route_table_t *route_table = interface_info->route_table + i;
+ grub_uint32_t u32_address, u32_mask, u32_subnet;
+
+ u32_address = grub_get_unaligned32 (address);
+ u32_subnet = grub_get_unaligned32 (route_table->subnet_address);
+ u32_mask = grub_get_unaligned32 (route_table->subnet_mask);
+
+ /* SKIP Default GATEWAY */
+ if (!u32_subnet && !u32_mask)
+ continue;
+
+ if ((u32_address & u32_mask) == u32_subnet)
+ {
+ for (inf = dev->net_interfaces; inf; inf = inf->next)
+ if (!inf->prefer_ip6)
+ {
+ grub_free (interface_info);
+ return inf;
+ }
+ }
+ }
+
+ grub_free (interface_info);
+ return NULL;
+}
+
+static int
+grub_efi_ip4_interface_set_manual_address (struct grub_efi_net_device *dev,
+ grub_efi_net_ip_manual_address_t *net_ip,
+ int with_subnet)
+{
+ grub_efi_status_t status;
+ grub_efi_ip4_config2_manual_address_t *address = &net_ip->ip4;
+
+ if (!with_subnet)
+ {
+ grub_efi_ip4_config2_manual_address_t *manual_address =
+ efi_ip4_config_manual_address (dev->ip4_config);
+
+ if (manual_address)
+ {
+ grub_memcpy (address->subnet_mask, manual_address->subnet_mask, sizeof(address->subnet_mask));
+ grub_free (manual_address);
+ }
+ else
+ {
+ /* XXX: */
+ address->subnet_mask[0] = 0xff;
+ address->subnet_mask[1] = 0xff;
+ address->subnet_mask[2] = 0xff;
+ address->subnet_mask[3] = 0;
+ }
+ }
+
+ status = dev->ip4_config->set_data (dev->ip4_config,
+ GRUB_EFI_IP4_CONFIG2_DATA_TYPE_MANUAL_ADDRESS,
+ sizeof(*address), address);
+
+ if (status != GRUB_EFI_SUCCESS)
+ return 0;
+
+ return 1;
+}
+
+static int
+grub_efi_ip4_interface_set_gateway (struct grub_efi_net_device *dev,
+ grub_efi_net_ip_address_t *address)
+{
+ grub_efi_status_t status;
+
+ status = dev->ip4_config->set_data (dev->ip4_config,
+ GRUB_EFI_IP4_CONFIG2_DATA_TYPE_GATEWAY,
+ sizeof (address->ip4), &address->ip4);
+
+ if (status != GRUB_EFI_SUCCESS)
+ return 0;
+ return 1;
+}
+
+/* FIXME: Multiple DNS */
+static int
+grub_efi_ip4_interface_set_dns (struct grub_efi_net_device *dev,
+ grub_efi_net_ip_address_t *address)
+{
+ grub_efi_status_t status;
+
+ status = dev->ip4_config->set_data (dev->ip4_config,
+ GRUB_EFI_IP4_CONFIG2_DATA_TYPE_DNSSERVER,
+ sizeof (address->ip4), &address->ip4);
+
+ if (status != GRUB_EFI_SUCCESS)
+ return 0;
+ return 1;
+}
+
+grub_efi_net_ip_config_t *efi_net_ip4_config = &(grub_efi_net_ip_config_t)
+ {
+ .get_hw_address = grub_efi_ip4_interface_hw_address,
+ .get_address = grub_efi_ip4_interface_address,
+ .get_route_table = grub_efi_ip4_interface_route_table,
+ .best_interface = grub_efi_ip4_interface_match,
+ .set_address = grub_efi_ip4_interface_set_manual_address,
+ .set_gateway = grub_efi_ip4_interface_set_gateway,
+ .set_dns = grub_efi_ip4_interface_set_dns
+ };
diff --git a/grub-core/net/efi/ip6_config.c b/grub-core/net/efi/ip6_config.c
new file mode 100644
index 000000000..0382b7df7
--- /dev/null
+++ b/grub-core/net/efi/ip6_config.c
@@ -0,0 +1,430 @@
+#include <grub/efi/api.h>
+#include <grub/efi/efi.h>
+#include <grub/misc.h>
+#include <grub/net/efi.h>
+#include <grub/charset.h>
+#include <grub/safemath.h>
+
+char *
+grub_efi_ip6_address_to_string (grub_efi_pxe_ipv6_address_t *address)
+{
+ char *str = grub_malloc (sizeof ("XXXX:XXXX:XXXX:XXXX:XXXX:XXXX:XXXX:XXXX"));
+ char *p;
+ int i;
+ int squash;
+
+ if (!str)
+ return NULL;
+
+ p = str;
+ squash = 0;
+ for (i = 0; i < 8; ++i)
+ {
+ grub_uint16_t addr;
+
+ if (i == 7)
+ squash = 2;
+
+ addr = grub_get_unaligned16 (address->addr + i * 2);
+
+ if (grub_be_to_cpu16 (addr))
+ {
+ char buf[sizeof ("XXXX")];
+ if (i > 0)
+ *p++ = ':';
+ grub_snprintf (buf, sizeof (buf), "%x", grub_be_to_cpu16 (addr));
+ grub_strcpy (p, buf);
+ p += grub_strlen (buf);
+
+ if (squash == 1)
+ squash = 2;
+ }
+ else
+ {
+ if (squash == 0)
+ {
+ *p++ = ':';
+ squash = 1;
+ }
+ else if (squash == 2)
+ {
+ *p++ = ':';
+ *p++ = '0';
+ }
+ }
+ }
+ *p = '\0';
+ return str;
+}
+
+int
+grub_efi_string_to_ip6_address (const char *val, grub_efi_ipv6_address_t *address, const char **rest)
+{
+ grub_uint16_t newip[8];
+ const char *ptr = val;
+ int word, quaddot = -1;
+ int bracketed = 0;
+
+ if (ptr[0] == '[') {
+ bracketed = 1;
+ ptr++;
+ }
+
+ if (ptr[0] == ':' && ptr[1] != ':')
+ return 0;
+ if (ptr[0] == ':')
+ ptr++;
+
+ for (word = 0; word < 8; word++)
+ {
+ unsigned long t;
+ if (*ptr == ':')
+ {
+ quaddot = word;
+ word--;
+ ptr++;
+ continue;
+ }
+ t = grub_strtoul (ptr, &ptr, 16);
+ if (grub_errno)
+ {
+ grub_errno = GRUB_ERR_NONE;
+ break;
+ }
+ if (t & ~0xffff)
+ return 0;
+ newip[word] = grub_cpu_to_be16 (t);
+ if (*ptr != ':')
+ break;
+ ptr++;
+ }
+ if (quaddot == -1 && word < 7)
+ return 0;
+ if (quaddot != -1)
+ {
+ grub_memmove (&newip[quaddot + 7 - word], &newip[quaddot],
+ (word - quaddot + 1) * sizeof (newip[0]));
+ grub_memset (&newip[quaddot], 0, (7 - word) * sizeof (newip[0]));
+ }
+ grub_memcpy (address, newip, 16);
+ if (bracketed && *ptr == ']') {
+ ptr++;
+ }
+ if (rest)
+ *rest = ptr;
+ return 1;
+}
+
+static grub_efi_ip6_config_interface_info_t *
+efi_ip6_config_interface_info (grub_efi_ip6_config_protocol_t *ip6_config)
+{
+ grub_efi_uintn_t sz;
+ grub_efi_status_t status;
+ grub_efi_ip6_config_interface_info_t *interface_info;
+
+ sz = sizeof (*interface_info) + sizeof (*interface_info->route_table);
+ interface_info = grub_malloc (sz);
+
+ status = ip6_config->get_data (ip6_config,
+ GRUB_EFI_IP6_CONFIG_DATA_TYPE_INTERFACEINFO,
+ &sz, interface_info);
+
+ if (status == GRUB_EFI_BUFFER_TOO_SMALL)
+ {
+ grub_free (interface_info);
+ interface_info = grub_malloc (sz);
+ status = ip6_config->get_data (ip6_config,
+ GRUB_EFI_IP6_CONFIG_DATA_TYPE_INTERFACEINFO,
+ &sz, interface_info);
+ }
+
+ if (status != GRUB_EFI_SUCCESS)
+ {
+ grub_free (interface_info);
+ return NULL;
+ }
+
+ return interface_info;
+}
+
+static grub_efi_ip6_config_manual_address_t *
+efi_ip6_config_manual_address (grub_efi_ip6_config_protocol_t *ip6_config)
+{
+ grub_efi_uintn_t sz;
+ grub_efi_status_t status;
+ grub_efi_ip6_config_manual_address_t *manual_address;
+
+ sz = sizeof (*manual_address);
+ manual_address = grub_malloc (sz);
+ if (!manual_address)
+ return NULL;
+
+ status = ip6_config->get_data (ip6_config,
+ GRUB_EFI_IP6_CONFIG_DATA_TYPE_MANUAL_ADDRESS,
+ &sz, manual_address);
+
+ if (status != GRUB_EFI_SUCCESS)
+ {
+ grub_free (manual_address);
+ return NULL;
+ }
+
+ return manual_address;
+}
+
+char *
+grub_efi_ip6_interface_name (struct grub_efi_net_device *dev)
+{
+ grub_efi_ip6_config_interface_info_t *interface_info;
+ char *name;
+
+ interface_info = efi_ip6_config_interface_info (dev->ip6_config);
+
+ if (!interface_info)
+ return NULL;
+
+ name = grub_malloc (GRUB_EFI_IP4_CONFIG2_INTERFACE_INFO_NAME_SIZE
+ * GRUB_MAX_UTF8_PER_UTF16 + 1);
+ *grub_utf16_to_utf8 ((grub_uint8_t *)name, interface_info->name,
+ GRUB_EFI_IP4_CONFIG2_INTERFACE_INFO_NAME_SIZE) = 0;
+ grub_free (interface_info);
+ return name;
+}
+
+static char *
+grub_efi_ip6_interface_hw_address (struct grub_efi_net_device *dev)
+{
+ grub_efi_ip6_config_interface_info_t *interface_info;
+ char *hw_addr;
+
+ interface_info = efi_ip6_config_interface_info (dev->ip6_config);
+
+ if (!interface_info)
+ return NULL;
+
+ hw_addr = grub_efi_hw_address_to_string (interface_info->hw_address_size, interface_info->hw_address);
+ grub_free (interface_info);
+
+ return hw_addr;
+}
+
+static char *
+grub_efi_ip6_interface_address (struct grub_efi_net_device *dev)
+{
+ grub_efi_ip6_config_manual_address_t *manual_address;
+ char *addr;
+
+ manual_address = efi_ip6_config_manual_address (dev->ip6_config);
+
+ if (!manual_address)
+ return NULL;
+
+ addr = grub_efi_ip6_address_to_string ((grub_efi_pxe_ipv6_address_t *)&manual_address->address);
+ grub_free (manual_address);
+ return addr;
+}
+
+static char **
+grub_efi_ip6_interface_route_table (struct grub_efi_net_device *dev)
+{
+ grub_efi_ip6_config_interface_info_t *interface_info;
+ char **ret;
+ int i, id;
+ grub_size_t sz;
+
+ interface_info = efi_ip6_config_interface_info (dev->ip6_config);
+ if (!interface_info)
+ return NULL;
+
+ if (grub_add (interface_info->route_count, 1, &sz))
+ {
+ grub_free (interface_info);
+ return NULL;
+ }
+
+ ret = grub_calloc (sz, sizeof (*ret));
+
+ if (!ret)
+ {
+ grub_free (interface_info);
+ return NULL;
+ }
+
+ id = 0;
+ for (i = 0; i < (int)interface_info->route_count ; i++)
+ {
+ char *gateway, *destination;
+ grub_uint64_t u64_gateway[2];
+ grub_uint64_t u64_destination[2];
+ grub_efi_ip6_route_table_t *route_table = interface_info->route_table + i;
+ grub_efi_net_interface_t *inf;
+ char *interface_name = NULL;
+
+ gateway = grub_efi_ip6_address_to_string (&route_table->gateway);
+ destination = grub_efi_ip6_address_to_string (&route_table->destination);
+
+ u64_gateway[0] = grub_get_unaligned64 (route_table->gateway.addr);
+ u64_gateway[1] = grub_get_unaligned64 (route_table->gateway.addr + 8);
+ u64_destination[0] = grub_get_unaligned64 (route_table->destination.addr);
+ u64_destination[1] = grub_get_unaligned64 (route_table->destination.addr + 8);
+
+ for (inf = dev->net_interfaces; inf; inf = inf->next)
+ if (inf->prefer_ip6)
+ interface_name = inf->name;
+
+ if ((!u64_gateway[0] && !u64_gateway[1])
+ && (u64_destination[0] || u64_destination[1]))
+ {
+ if (interface_name)
+ {
+ if ((grub_be_to_cpu64 (u64_destination[0]) == 0xfe80000000000000ULL)
+ && (!u64_destination[1])
+ && (route_table->prefix_length == 64))
+ ret[id++] = grub_xasprintf ("%s:link %s/%d %s", dev->card_name, destination, route_table->prefix_length, interface_name);
+ else
+ ret[id++] = grub_xasprintf ("%s:local %s/%d %s", dev->card_name, destination, route_table->prefix_length, interface_name);
+ }
+ }
+ else if ((u64_gateway[0] || u64_gateway[1])
+ && (u64_destination[0] || u64_destination[1]))
+ ret[id++] = grub_xasprintf ("%s:gw %s/%d gw %s", dev->card_name, destination, route_table->prefix_length, gateway);
+ else if ((u64_gateway[0] || u64_gateway[1])
+ && (!u64_destination[0] && !u64_destination[1]))
+ ret[id++] = grub_xasprintf ("%s:default %s/%d gw %s", dev->card_name, destination, route_table->prefix_length, gateway);
+
+ grub_free (gateway);
+ grub_free (destination);
+ }
+
+ ret[id] = NULL;
+ grub_free (interface_info);
+ return ret;
+}
+
+static grub_efi_net_interface_t *
+grub_efi_ip6_interface_match (struct grub_efi_net_device *dev, grub_efi_net_ip_address_t *ip_address)
+{
+ grub_efi_ip6_config_interface_info_t *interface_info;
+ grub_efi_net_interface_t *inf;
+ int i;
+ grub_efi_ipv6_address_t *address = &ip_address->ip6;
+
+ interface_info = efi_ip6_config_interface_info (dev->ip6_config);
+ if (!interface_info)
+ return NULL;
+
+ for (i = 0; i < (int)interface_info->route_count ; i++)
+ {
+ grub_uint64_t u64_addr[2];
+ grub_uint64_t u64_subnet[2];
+ grub_uint64_t u64_mask[2];
+
+ grub_efi_ip6_route_table_t *route_table = interface_info->route_table + i;
+
+ /* SKIP Default GATEWAY */
+ if (route_table->prefix_length == 0)
+ continue;
+
+ u64_addr[0] = grub_get_unaligned64 (address);
+ u64_addr[1] = grub_get_unaligned64 (address + 4);
+ u64_subnet[0] = grub_get_unaligned64 (route_table->destination.addr);
+ u64_subnet[1] = grub_get_unaligned64 (route_table->destination.addr + 8);
+ u64_mask[0] = (route_table->prefix_length <= 64) ?
+ 0xffffffffffffffffULL << (64 - route_table->prefix_length) :
+ 0xffffffffffffffffULL;
+ u64_mask[1] = (route_table->prefix_length <= 64) ?
+ 0 :
+ 0xffffffffffffffffULL << (128 - route_table->prefix_length);
+
+ if (((u64_addr[0] & u64_mask[0]) == u64_subnet[0])
+ && ((u64_addr[1] & u64_mask[1]) == u64_subnet[1]))
+ {
+ for (inf = dev->net_interfaces; inf; inf = inf->next)
+ if (inf->prefer_ip6)
+ {
+ grub_free (interface_info);
+ return inf;
+ }
+ }
+ }
+
+ grub_free (interface_info);
+ return NULL;
+}
+
+static int
+grub_efi_ip6_interface_set_manual_address (struct grub_efi_net_device *dev,
+ grub_efi_net_ip_manual_address_t *net_ip,
+ int with_subnet)
+{
+ grub_efi_status_t status;
+ grub_efi_ip6_config_manual_address_t *address = &net_ip->ip6;
+
+ if (!with_subnet)
+ {
+ grub_efi_ip6_config_manual_address_t *manual_address =
+ efi_ip6_config_manual_address (dev->ip6_config);
+
+ if (manual_address)
+ {
+ address->prefix_length = manual_address->prefix_length;
+ grub_free (manual_address);
+ }
+ else
+ {
+ /* XXX: */
+ address->prefix_length = 64;
+ }
+ }
+
+ status = dev->ip6_config->set_data (dev->ip6_config,
+ GRUB_EFI_IP6_CONFIG_DATA_TYPE_MANUAL_ADDRESS,
+ sizeof(*address), address);
+
+ if (status != GRUB_EFI_SUCCESS)
+ return 0;
+
+ return 1;
+}
+
+static int
+grub_efi_ip6_interface_set_gateway (struct grub_efi_net_device *dev,
+ grub_efi_net_ip_address_t *address)
+{
+ grub_efi_status_t status;
+
+ status = dev->ip6_config->set_data (dev->ip6_config,
+ GRUB_EFI_IP6_CONFIG_DATA_TYPE_GATEWAY,
+ sizeof (address->ip6), &address->ip6);
+
+ if (status != GRUB_EFI_SUCCESS)
+ return 0;
+ return 1;
+}
+
+static int
+grub_efi_ip6_interface_set_dns (struct grub_efi_net_device *dev,
+ grub_efi_net_ip_address_t *address)
+{
+
+ grub_efi_status_t status;
+
+ status = dev->ip6_config->set_data (dev->ip6_config,
+ GRUB_EFI_IP6_CONFIG_DATA_TYPE_DNSSERVER,
+ sizeof (address->ip6), &address->ip6);
+
+ if (status != GRUB_EFI_SUCCESS)
+ return 0;
+ return 1;
+}
+
+grub_efi_net_ip_config_t *efi_net_ip6_config = &(grub_efi_net_ip_config_t)
+ {
+ .get_hw_address = grub_efi_ip6_interface_hw_address,
+ .get_address = grub_efi_ip6_interface_address,
+ .get_route_table = grub_efi_ip6_interface_route_table,
+ .best_interface = grub_efi_ip6_interface_match,
+ .set_address = grub_efi_ip6_interface_set_manual_address,
+ .set_gateway = grub_efi_ip6_interface_set_gateway,
+ .set_dns = grub_efi_ip6_interface_set_dns
+ };
diff --git a/grub-core/net/efi/net.c b/grub-core/net/efi/net.c
new file mode 100644
index 000000000..a7845e2eb
--- /dev/null
+++ b/grub-core/net/efi/net.c
@@ -0,0 +1,1440 @@
+#include <grub/net.h>
+#include <grub/env.h>
+#include <grub/mm.h>
+#include <grub/misc.h>
+#include <grub/dl.h>
+#include <grub/command.h>
+#include <grub/efi/api.h>
+#include <grub/efi/efi.h>
+#include <grub/i18n.h>
+#include <grub/bufio.h>
+#include <grub/efi/http.h>
+#include <grub/efi/dhcp.h>
+#include <grub/net/efi.h>
+#include <grub/charset.h>
+#include <grub/safemath.h>
+
+GRUB_MOD_LICENSE ("GPLv3+");
+
+#define GRUB_EFI_IP6_PREFIX_LENGTH 64
+
+static grub_guid_t ip4_config_guid = GRUB_EFI_IP4_CONFIG2_PROTOCOL_GUID;
+static grub_guid_t ip6_config_guid = GRUB_EFI_IP6_CONFIG_PROTOCOL_GUID;
+static grub_guid_t http_service_binding_guid = GRUB_EFI_HTTP_SERVICE_BINDING_PROTOCOL_GUID;
+static grub_guid_t http_guid = GRUB_EFI_HTTP_PROTOCOL_GUID;
+static grub_guid_t pxe_io_guid = GRUB_EFI_PXE_GUID;
+static grub_guid_t dhcp4_service_binding_guid = GRUB_EFI_DHCP4_SERVICE_BINDING_PROTOCOL_GUID;
+static grub_guid_t dhcp4_guid = GRUB_EFI_DHCP4_PROTOCOL_GUID;
+static grub_guid_t dhcp6_service_binding_guid = GRUB_EFI_DHCP6_SERVICE_BINDING_PROTOCOL_GUID;
+static grub_guid_t dhcp6_guid = GRUB_EFI_DHCP6_PROTOCOL_GUID;
+
+struct grub_efi_net_device *net_devices;
+
+static char *default_server;
+static grub_efi_net_interface_t *net_interface;
+static grub_efi_net_interface_t *net_default_interface;
+
+#define efi_net_interface_configure(inf) inf->io->configure (inf->dev, inf->prefer_ip6)
+#define efi_net_interface_open(inf, file, name) inf->io->open (inf->dev, inf->prefer_ip6, file, name, inf->io_type)
+#define efi_net_interface_read(inf, file, buf, sz) inf->io->read (inf->dev, inf->prefer_ip6, file, buf, sz)
+#define efi_net_interface_close(inf, file) inf->io->close (inf->dev, inf->prefer_ip6, file)
+#define efi_net_interface(m,...) efi_net_interface_ ## m (net_interface, ## __VA_ARGS__)
+
+static grub_efi_handle_t
+grub_efi_locate_device_path (grub_guid_t *protocol, grub_efi_device_path_t *device_path,
+ grub_efi_device_path_t **r_device_path)
+{
+ grub_efi_handle_t handle;
+ grub_efi_status_t status;
+
+ status = grub_efi_system_table->boot_services->locate_device_path (
+ protocol, &device_path, &handle);
+
+ if (status != GRUB_EFI_SUCCESS)
+ return 0;
+
+ if (r_device_path)
+ *r_device_path = device_path;
+
+ return handle;
+}
+
+static int
+url_parse_fields (const char *url, char **proto, char **host, char **path)
+{
+ const char *p, *ps;
+ grub_size_t l;
+
+ *proto = *host = *path = NULL;
+ ps = p = url;
+
+ while ((p = grub_strchr (p, ':')))
+ {
+ if (grub_strlen (p) < sizeof ("://") - 1)
+ break;
+ if (grub_memcmp (p, "://", sizeof ("://") - 1) == 0)
+ {
+ l = p - ps;
+ *proto = grub_malloc (l + 1);
+ if (!*proto)
+ {
+ grub_print_error ();
+ return 0;
+ }
+
+ grub_memcpy (*proto, ps, l);
+ (*proto)[l] = '\0';
+ p += sizeof ("://") - 1;
+ break;
+ }
+ ++p;
+ }
+
+ if (!*proto)
+ {
+ grub_dprintf ("bootp", "url: %s is not valid, protocol not found\n", url);
+ return 0;
+ }
+
+ ps = p;
+ p = grub_strchr (p, '/');
+
+ if (!p)
+ {
+ grub_dprintf ("bootp", "url: %s is not valid, host/path not found\n", url);
+ grub_free (*proto);
+ *proto = NULL;
+ return 0;
+ }
+
+ l = p - ps;
+
+ if (l > 2 && ps[0] == '[' && ps[l - 1] == ']')
+ {
+ *host = grub_malloc (l - 1);
+ if (!*host)
+ {
+ grub_print_error ();
+ grub_free (*proto);
+ *proto = NULL;
+ return 0;
+ }
+ grub_memcpy (*host, ps + 1, l - 2);
+ (*host)[l - 2] = 0;
+ }
+ else
+ {
+ *host = grub_malloc (l + 1);
+ if (!*host)
+ {
+ grub_print_error ();
+ grub_free (*proto);
+ *proto = NULL;
+ return 0;
+ }
+ grub_memcpy (*host, ps, l);
+ (*host)[l] = 0;
+ }
+
+ *path = grub_strdup (p);
+ if (!*path)
+ {
+ grub_print_error ();
+ grub_free (*host);
+ grub_free (*proto);
+ *host = NULL;
+ *proto = NULL;
+ return 0;
+ }
+ return 1;
+}
+
+static void
+url_get_boot_location (const char *url, char **device, char **path, int is_default)
+{
+ char *protocol, *server, *file;
+ char *slash;
+
+ if (!url_parse_fields (url, &protocol, &server, &file))
+ return;
+
+ if ((slash = grub_strrchr (file, '/')))
+ *slash = 0;
+ else
+ *file = 0;
+
+ *device = grub_xasprintf ("%s,%s", protocol, server);
+ *path = grub_strdup(file);
+
+ if (is_default)
+ default_server = server;
+ else
+ grub_free (server);
+
+ grub_free (protocol);
+ grub_free (file);
+}
+
+static void
+pxe_get_boot_location (const struct grub_net_bootp_packet *bp,
+ char **device,
+ char **path,
+ int is_default)
+{
+ char *server = grub_xasprintf ("%d.%d.%d.%d",
+ ((grub_uint8_t *) &bp->server_ip)[0],
+ ((grub_uint8_t *) &bp->server_ip)[1],
+ ((grub_uint8_t *) &bp->server_ip)[2],
+ ((grub_uint8_t *) &bp->server_ip)[3]);
+
+ *device = grub_xasprintf ("tftp,%s", server);
+
+ *path = grub_strndup (bp->boot_file, sizeof (bp->boot_file));
+
+ if (*path)
+ {
+ char *slash;
+ slash = grub_strrchr (*path, '/');
+ if (slash)
+ *slash = 0;
+ else
+ **path = 0;
+ }
+
+ if (is_default)
+ default_server = server;
+ else
+ grub_free (server);
+}
+
+static void
+pxe_get_boot_location_v6 (const struct grub_net_dhcp6_packet *dp,
+ grub_size_t dhcp_size,
+ char **device,
+ char **path)
+{
+
+ struct grub_net_dhcp6_option *dhcp_opt;
+ grub_size_t dhcp_remain_size;
+ *device = *path = 0;
+
+ if (dhcp_size < sizeof (*dp))
+ {
+ grub_error (GRUB_ERR_OUT_OF_RANGE, N_("DHCPv6 packet size too small"));
+ return;
+ }
+
+ dhcp_remain_size = dhcp_size - sizeof (*dp);
+ dhcp_opt = (struct grub_net_dhcp6_option *)dp->dhcp_options;
+
+ while (dhcp_remain_size)
+ {
+ grub_uint16_t code = grub_be_to_cpu16 (dhcp_opt->code);
+ grub_uint16_t len = grub_be_to_cpu16 (dhcp_opt->len);
+ grub_uint16_t option_size = sizeof (*dhcp_opt) + len;
+
+ if (dhcp_remain_size < option_size || code == 0)
+ break;
+
+ if (code == GRUB_NET_DHCP6_OPTION_BOOTFILE_URL)
+ {
+ char *url;
+ grub_size_t sz;
+
+ if (grub_add (len, 1, &sz))
+ return;
+
+ url = grub_malloc (sz);
+ if (!url)
+ return;
+
+ grub_memcpy (url, dhcp_opt->data, len);
+ url[len] = 0;
+
+ url_get_boot_location ((const char *)url, device, path, 1);
+ grub_free (url);
+ break;
+ }
+
+ dhcp_remain_size -= option_size;
+ dhcp_opt = (struct grub_net_dhcp6_option *)((grub_uint8_t *)dhcp_opt + option_size);
+ }
+}
+
+static grub_efi_net_interface_t *
+grub_efi_net_config_from_device_path (grub_efi_device_path_t *dp,
+ struct grub_efi_net_device *netdev,
+ char **device,
+ char **path)
+{
+ grub_efi_net_interface_t *inf = NULL;
+
+ while (1)
+ {
+ grub_efi_uint8_t type = GRUB_EFI_DEVICE_PATH_TYPE (dp);
+ grub_efi_uint8_t subtype = GRUB_EFI_DEVICE_PATH_SUBTYPE (dp);
+ grub_efi_uint16_t len = GRUB_EFI_DEVICE_PATH_LENGTH (dp);
+
+ if (type == GRUB_EFI_MESSAGING_DEVICE_PATH_TYPE)
+ {
+ if (subtype == GRUB_EFI_URI_DEVICE_PATH_SUBTYPE)
+ {
+ grub_efi_uri_device_path_t *uri_dp;
+ uri_dp = (grub_efi_uri_device_path_t *) dp;
+ /* Beware that uri_dp->uri may not be null terminated */
+ url_get_boot_location ((const char *)uri_dp->uri, device, path, 1);
+ }
+ else if (subtype == GRUB_EFI_IPV4_DEVICE_PATH_SUBTYPE)
+ {
+ grub_efi_net_ip_manual_address_t net_ip;
+ grub_efi_ipv4_device_path_t *ipv4 = (grub_efi_ipv4_device_path_t *) dp;
+
+ if (inf)
+ continue;
+ grub_memcpy (net_ip.ip4.address, ipv4->local_ip_address, sizeof (net_ip.ip4.address));
+ grub_memcpy (net_ip.ip4.subnet_mask, ipv4->subnet_mask, sizeof (net_ip.ip4.subnet_mask));
+ net_ip.is_ip6 = 0;
+ inf = grub_efi_net_create_interface (netdev,
+ netdev->card_name,
+ &net_ip,
+ 1);
+ }
+ else if (subtype == GRUB_EFI_IPV6_DEVICE_PATH_SUBTYPE)
+ {
+ grub_efi_net_ip_manual_address_t net_ip;
+ grub_efi_ipv6_device_path_t *ipv6 = (grub_efi_ipv6_device_path_t *) dp;
+
+ if (inf)
+ continue;
+ grub_memcpy (net_ip.ip6.address, ipv6->local_ip_address, sizeof (net_ip.ip6.address));
+ net_ip.ip6.prefix_length = GRUB_EFI_IP6_PREFIX_LENGTH;
+ net_ip.ip6.is_anycast = 0;
+ net_ip.is_ip6 = 1;
+ inf = grub_efi_net_create_interface (netdev,
+ netdev->card_name,
+ &net_ip,
+ 1);
+ }
+ }
+
+ if (GRUB_EFI_END_ENTIRE_DEVICE_PATH (dp))
+ break;
+ dp = (grub_efi_device_path_t *) ((char *) dp + len);
+ }
+
+ return inf;
+}
+
+static grub_efi_net_interface_t *
+grub_efi_net_config_from_handle (grub_efi_handle_t *hnd,
+ struct grub_efi_net_device *netdev,
+ char **device,
+ char **path)
+{
+ grub_efi_pxe_t *pxe = NULL;
+
+ if (hnd == netdev->ip4_pxe_handle)
+ pxe = netdev->ip4_pxe;
+ else if (hnd == netdev->ip6_pxe_handle)
+ pxe = netdev->ip6_pxe;
+
+ if (!pxe)
+ return (grub_efi_net_config_from_device_path (
+ grub_efi_get_device_path (hnd),
+ netdev,
+ device,
+ path));
+
+ if (pxe->mode->using_ipv6)
+ {
+ grub_efi_net_ip_manual_address_t net_ip;
+
+ pxe_get_boot_location_v6 (
+ (const struct grub_net_dhcp6_packet *) &pxe->mode->dhcp_ack,
+ sizeof (pxe->mode->dhcp_ack),
+ device,
+ path);
+
+ grub_memcpy (net_ip.ip6.address, pxe->mode->station_ip.v6.addr, sizeof(net_ip.ip6.address));
+ net_ip.ip6.prefix_length = GRUB_EFI_IP6_PREFIX_LENGTH;
+ net_ip.ip6.is_anycast = 0;
+ net_ip.is_ip6 = 1;
+ return (grub_efi_net_create_interface (netdev,
+ netdev->card_name,
+ &net_ip,
+ 1));
+ }
+ else
+ {
+ grub_efi_net_ip_manual_address_t net_ip;
+
+ pxe_get_boot_location (
+ (const struct grub_net_bootp_packet *) &pxe->mode->dhcp_ack,
+ device,
+ path,
+ 1);
+
+ grub_memcpy (net_ip.ip4.address, pxe->mode->station_ip.v4.addr, sizeof (net_ip.ip4.address));
+ grub_memcpy (net_ip.ip4.subnet_mask, pxe->mode->subnet_mask.v4.addr, sizeof (net_ip.ip4.subnet_mask));
+ net_ip.is_ip6 = 0;
+ return (grub_efi_net_create_interface (netdev,
+ netdev->card_name,
+ &net_ip,
+ 1));
+ }
+}
+
+static const char *
+grub_efi_net_var_get_address (struct grub_env_var *var,
+ const char *val __attribute__ ((unused)))
+{
+ struct grub_efi_net_device *dev;
+
+ for (dev = net_devices; dev; dev = dev->next)
+ {
+ grub_efi_net_interface_t *inf;
+
+ for (inf = dev->net_interfaces; inf; inf = inf->next)
+ {
+ char *var_name;
+
+ var_name = grub_xasprintf ("net_%s_ip", inf->name);
+ if (grub_strcmp (var_name, var->name) == 0)
+ return efi_net_interface_get_address (inf);
+ grub_free (var_name);
+ var_name = grub_xasprintf ("net_%s_mac", inf->name);
+ if (grub_strcmp (var_name, var->name) == 0)
+ return efi_net_interface_get_hw_address (inf);
+ grub_free (var_name);
+ }
+ }
+
+ return NULL;
+}
+
+static char *
+grub_efi_net_var_set_interface (struct grub_env_var *var __attribute__ ((unused)),
+ const char *val)
+{
+ struct grub_efi_net_device *dev;
+ grub_efi_net_interface_t *inf;
+
+ for (dev = net_devices; dev; dev = dev->next)
+ for (inf = dev->net_interfaces; inf; inf = inf->next)
+ if (grub_strcmp (inf->name, val) == 0)
+ {
+ net_default_interface = inf;
+ return grub_strdup (val);
+ }
+
+ return NULL;
+}
+
+static char *
+grub_efi_net_var_set_server (struct grub_env_var *var __attribute__ ((unused)),
+ const char *val)
+{
+ grub_free (default_server);
+ default_server = grub_strdup (val);
+ return grub_strdup (val);
+}
+
+static const char *
+grub_efi_net_var_get_server (struct grub_env_var *var __attribute__ ((unused)),
+ const char *val __attribute__ ((unused)))
+{
+ return default_server ? : "";
+}
+
+static const char *
+grub_efi_net_var_get_ip (struct grub_env_var *var __attribute__ ((unused)),
+ const char *val __attribute__ ((unused)))
+{
+ const char *intf = grub_env_get ("net_default_interface");
+ const char *ret = NULL;
+ if (intf)
+ {
+ char *buf = grub_xasprintf ("net_%s_ip", intf);
+ if (buf)
+ ret = grub_env_get (buf);
+ grub_free (buf);
+ }
+ return ret;
+}
+
+static const char *
+grub_efi_net_var_get_mac (struct grub_env_var *var __attribute__ ((unused)),
+ const char *val __attribute__ ((unused)))
+{
+ const char *intf = grub_env_get ("net_default_interface");
+ const char *ret = NULL;
+ if (intf)
+ {
+ char *buf = grub_xasprintf ("net_%s_mac", intf);
+ if (buf)
+ ret = grub_env_get (buf);
+ grub_free (buf);
+ }
+ return ret;
+}
+
+static void
+grub_efi_net_export_interface_vars (void)
+{
+ struct grub_efi_net_device *dev;
+
+ for (dev = net_devices; dev; dev = dev->next)
+ {
+ grub_efi_net_interface_t *inf;
+
+ for (inf = dev->net_interfaces; inf; inf = inf->next)
+ {
+ char *var;
+
+ var = grub_xasprintf ("net_%s_ip", inf->name);
+ grub_register_variable_hook (var, grub_efi_net_var_get_address, 0);
+ grub_env_export (var);
+ grub_free (var);
+ var = grub_xasprintf ("net_%s_mac", inf->name);
+ grub_register_variable_hook (var, grub_efi_net_var_get_address, 0);
+ grub_env_export (var);
+ grub_free (var);
+ }
+ }
+}
+
+static void
+grub_efi_net_unset_interface_vars (void)
+{
+ struct grub_efi_net_device *dev;
+
+ for (dev = net_devices; dev; dev = dev->next)
+ {
+ grub_efi_net_interface_t *inf;
+
+ for (inf = dev->net_interfaces; inf; inf = inf->next)
+ {
+ char *var;
+
+ var = grub_xasprintf ("net_%s_ip", inf->name);
+ grub_register_variable_hook (var, 0, 0);
+ grub_env_unset (var);
+ grub_free (var);
+ var = grub_xasprintf ("net_%s_mac", inf->name);
+ grub_register_variable_hook (var, 0, 0);
+ grub_env_unset (var);
+ grub_free (var);
+ }
+ }
+}
+
+grub_efi_net_interface_t *
+grub_efi_net_create_interface (struct grub_efi_net_device *dev,
+ const char *interface_name,
+ grub_efi_net_ip_manual_address_t *net_ip,
+ int has_subnet)
+{
+ grub_efi_net_interface_t *inf;
+
+ for (inf = dev->net_interfaces; inf; inf = inf->next)
+ {
+ if (inf->prefer_ip6 == net_ip->is_ip6)
+ break;
+ }
+
+ if (!inf)
+ {
+ inf = grub_malloc (sizeof(*inf));
+ inf->name = grub_strdup (interface_name);
+ inf->prefer_ip6 = net_ip->is_ip6;
+ inf->dev = dev;
+ inf->next = dev->net_interfaces;
+ inf->ip_config = (net_ip->is_ip6) ? efi_net_ip6_config : efi_net_ip4_config ;
+ dev->net_interfaces = inf;
+ }
+ else
+ {
+ grub_free (inf->name);
+ inf->name = grub_strdup (interface_name);
+ }
+
+ if (!efi_net_interface_set_address (inf, net_ip, has_subnet))
+ {
+ grub_error (GRUB_ERR_BUG, N_("Set Address Failed"));
+ return NULL;
+ }
+
+ return inf;
+}
+
+static void
+grub_efi_net_config_real (grub_efi_handle_t hnd, char **device,
+ char **path)
+{
+ grub_efi_handle_t config_hnd;
+
+ struct grub_efi_net_device *netdev;
+ grub_efi_net_interface_t *inf;
+
+ config_hnd = grub_efi_locate_device_path (&ip4_config_guid, grub_efi_get_device_path (hnd), NULL);
+
+ if (!config_hnd)
+ return;
+
+ for (netdev = net_devices; netdev; netdev = netdev->next)
+ if (netdev->handle == config_hnd)
+ break;
+
+ if (!netdev)
+ return;
+
+ if (!(inf = grub_efi_net_config_from_handle (hnd, netdev, device, path)))
+ return;
+
+ grub_env_set ("net_default_interface", inf->name);
+ grub_efi_net_export_interface_vars ();
+}
+
+static grub_err_t
+grub_efi_netfs_dir (grub_device_t device, const char *path __attribute__ ((unused)),
+ grub_fs_dir_hook_t hook __attribute__ ((unused)),
+ void *hook_data __attribute__ ((unused)))
+{
+ if (!device->net)
+ return grub_error (GRUB_ERR_BUG, "invalid net device");
+ return GRUB_ERR_NONE;
+}
+
+static grub_err_t
+grub_efi_netfs_open (struct grub_file *file_out __attribute__ ((unused)),
+ const char *name __attribute__ ((unused)))
+{
+ struct grub_file *file, *bufio;
+
+ file = grub_malloc (sizeof (*file));
+ if (!file)
+ return grub_errno;
+
+ grub_memcpy (file, file_out, sizeof (struct grub_file));
+ file->device->net->name = grub_strdup (name);
+
+ if (!file->device->net->name)
+ {
+ grub_free (file);
+ return grub_errno;
+ }
+
+ efi_net_interface(open, file, name);
+ grub_print_error ();
+
+ bufio = grub_bufio_open (file, 32768);
+ if (!bufio)
+ {
+ grub_free (file->device->net->name);
+ grub_free (file);
+ return grub_errno;
+ }
+ grub_memcpy (file_out, bufio, sizeof (struct grub_file));
+ grub_free (bufio);
+
+ return GRUB_ERR_NONE;
+}
+
+static grub_ssize_t
+grub_efihttp_chunk_read (grub_file_t file, char *buf,
+ grub_size_t len, grub_size_t chunk_size)
+{
+ char *chunk = grub_malloc (chunk_size);
+ grub_size_t sum = 0;
+
+ while (len)
+ {
+ grub_ssize_t rd;
+ grub_size_t sz = (len > chunk_size) ? chunk_size : len;
+
+ rd = efi_net_interface (read, file, chunk, sz);
+
+ if (rd <= 0)
+ return rd;
+
+ if (buf)
+ {
+ grub_memcpy (buf, chunk, rd);
+ buf += rd;
+ }
+ sum += rd;
+ len -= rd;
+ }
+
+ grub_free (chunk);
+ return sum;
+}
+
+static grub_ssize_t
+grub_efi_netfs_read (grub_file_t file __attribute__ ((unused)),
+ char *buf __attribute__ ((unused)), grub_size_t len __attribute__ ((unused)))
+{
+ if (file->offset > file->device->net->offset)
+ {
+ grub_efihttp_chunk_read (file, NULL, file->offset - file->device->net->offset, 10240);
+ }
+ else if (file->offset < file->device->net->offset)
+ {
+ efi_net_interface (close, file);
+ efi_net_interface (open, file, file->device->net->name);
+ if (file->offset)
+ grub_efihttp_chunk_read (file, NULL, file->offset, 10240);
+ }
+
+ return efi_net_interface (read, file, buf, len);
+}
+
+static grub_err_t
+grub_efi_netfs_close (grub_file_t file)
+{
+ efi_net_interface (close, file);
+ return GRUB_ERR_NONE;
+}
+
+static grub_efi_handle_t
+grub_efi_service_binding (grub_efi_handle_t dev, grub_guid_t *service_binding_guid)
+{
+ grub_efi_service_binding_t *service;
+ grub_efi_status_t status;
+ grub_efi_handle_t child_dev = NULL;
+
+ service = grub_efi_open_protocol (dev, service_binding_guid, GRUB_EFI_OPEN_PROTOCOL_GET_PROTOCOL);
+ if (!service)
+ {
+ grub_error (GRUB_ERR_IO, N_("couldn't open efi service binding protocol"));
+ return NULL;
+ }
+
+ status = service->create_child (service, &child_dev);
+ if (status != GRUB_EFI_SUCCESS)
+ {
+ grub_error (GRUB_ERR_IO, N_("Failed to create child device of http service %" PRIxGRUB_SIZE), status);
+ return NULL;
+ }
+
+ return child_dev;
+}
+
+static grub_err_t
+grub_efi_net_parse_address (const char *address,
+ grub_efi_ip4_config2_manual_address_t *ip4,
+ grub_efi_ip6_config_manual_address_t *ip6,
+ int *is_ip6,
+ int *has_cidr)
+{
+ const char *rest;
+
+ if (grub_efi_string_to_ip4_address (address, &ip4->address, &rest))
+ {
+ *is_ip6 = 0;
+ if (*rest == '/')
+ {
+ grub_uint32_t subnet_mask_size;
+
+ subnet_mask_size = grub_strtoul (rest + 1, &rest, 0);
+
+ if (!grub_errno && subnet_mask_size <= 32 && *rest == 0)
+ {
+ grub_uint32_t subnet_mask;
+
+ subnet_mask = grub_cpu_to_be32 ((0xffffffffU << (32 - subnet_mask_size)));
+ grub_memcpy (ip4->subnet_mask, &subnet_mask, sizeof (ip4->subnet_mask));
+ if (has_cidr)
+ *has_cidr = 1;
+ return GRUB_ERR_NONE;
+ }
+ }
+ else if (*rest == 0)
+ {
+ grub_uint32_t subnet_mask = 0xffffffffU;
+ grub_memcpy (ip4->subnet_mask, &subnet_mask, sizeof (ip4->subnet_mask));
+ if (has_cidr)
+ *has_cidr = 0;
+ return GRUB_ERR_NONE;
+ }
+ }
+ else if (grub_efi_string_to_ip6_address (address, &ip6->address, &rest))
+ {
+ *is_ip6 = 1;
+ if (*rest == '/')
+ {
+ grub_efi_uint8_t prefix_length;
+
+ prefix_length = grub_strtoul (rest + 1, &rest, 0);
+ if (!grub_errno && prefix_length <= 128 && *rest == 0)
+ {
+ ip6->prefix_length = prefix_length;
+ ip6->is_anycast = 0;
+ if (has_cidr)
+ *has_cidr = 1;
+ return GRUB_ERR_NONE;
+ }
+ }
+ else if (*rest == 0)
+ {
+ ip6->prefix_length = 128;
+ ip6->is_anycast = 0;
+ if (has_cidr)
+ *has_cidr = 0;
+ return GRUB_ERR_NONE;
+ }
+ }
+
+ return grub_error (GRUB_ERR_NET_BAD_ADDRESS,
+ N_("unrecognised network address `%s'"),
+ address);
+}
+
+static grub_efi_net_interface_t *
+match_route (const char *server)
+{
+ grub_err_t err;
+ grub_efi_ip4_config2_manual_address_t ip4;
+ grub_efi_ip6_config_manual_address_t ip6;
+ grub_efi_net_interface_t *inf;
+ int is_ip6 = 0;
+
+ grub_error_push ();
+ err = grub_efi_net_parse_address (server, &ip4, &ip6, &is_ip6, 0);
+
+ if (err)
+ {
+ grub_dprintf ("efinetfs", "error in matching route : %s\n", grub_errmsg);
+ grub_error_pop ();
+ return NULL;
+ }
+ grub_error_pop ();
+
+ if (is_ip6)
+ {
+ struct grub_efi_net_device *dev;
+ grub_efi_net_ip_address_t addr;
+
+ grub_memcpy (addr.ip6, ip6.address, sizeof(ip6.address));
+
+ for (dev = net_devices; dev; dev = dev->next)
+ if ((inf = efi_net_ip6_config->best_interface (dev, &addr)))
+ return inf;
+ }
+ else
+ {
+ struct grub_efi_net_device *dev;
+ grub_efi_net_ip_address_t addr;
+
+ grub_memcpy (addr.ip4, ip4.address, sizeof(ip4.address));
+
+ for (dev = net_devices; dev; dev = dev->next)
+ if ((inf = efi_net_ip4_config->best_interface (dev, &addr)))
+ return inf;
+ }
+
+ return 0;
+}
+
+static void
+grub_efi_net_add_pxebc_to_cards (void)
+{
+ grub_efi_uintn_t num_handles;
+ grub_efi_handle_t *handles;
+ grub_efi_handle_t *handle;
+
+ handles = grub_efi_locate_handle (GRUB_EFI_BY_PROTOCOL, &pxe_io_guid,
+ 0, &num_handles);
+ if (!handles)
+ return;
+
+ for (handle = handles; num_handles--; handle++)
+ {
+ grub_efi_device_path_t *dp, *ddp, *ldp;
+ grub_efi_pxe_t *pxe;
+ struct grub_efi_net_device *d;
+ int is_ip6 = 0;
+
+ dp = grub_efi_get_device_path (*handle);
+ if (!dp)
+ continue;
+
+ ddp = grub_efi_duplicate_device_path (dp);
+ ldp = grub_efi_find_last_device_path (ddp);
+
+ if (ldp->type == GRUB_EFI_MESSAGING_DEVICE_PATH_TYPE
+ && ldp->subtype == GRUB_EFI_IPV4_DEVICE_PATH_SUBTYPE)
+ {
+ ldp->type = GRUB_EFI_END_DEVICE_PATH_TYPE;
+ ldp->subtype = GRUB_EFI_END_ENTIRE_DEVICE_PATH_SUBTYPE;
+ ldp->length = sizeof (*ldp);
+ }
+ else if (ldp->type == GRUB_EFI_MESSAGING_DEVICE_PATH_TYPE
+ && ldp->subtype == GRUB_EFI_IPV6_DEVICE_PATH_SUBTYPE)
+ {
+ is_ip6 = 1;
+ ldp->type = GRUB_EFI_END_DEVICE_PATH_TYPE;
+ ldp->subtype = GRUB_EFI_END_ENTIRE_DEVICE_PATH_SUBTYPE;
+ ldp->length = sizeof (*ldp);
+ }
+
+ for (d = net_devices; d; d = d->next)
+ if (grub_efi_compare_device_paths (ddp, grub_efi_get_device_path (d->handle)) == 0)
+ break;
+
+ if (!d)
+ {
+ grub_free (ddp);
+ continue;
+ }
+
+ pxe = grub_efi_open_protocol (*handle, &pxe_io_guid,
+ GRUB_EFI_OPEN_PROTOCOL_GET_PROTOCOL);
+
+ if (!pxe)
+ {
+ grub_free (ddp);
+ continue;
+ }
+
+ if (is_ip6)
+ {
+ d->ip6_pxe_handle = *handle;
+ d->ip6_pxe = pxe;
+ }
+ else
+ {
+ d->ip4_pxe_handle = *handle;
+ d->ip4_pxe = pxe;
+ }
+
+ grub_free (ddp);
+ }
+
+ grub_free (handles);
+}
+
+static void
+set_ip_policy_to_static (void)
+{
+ struct grub_efi_net_device *dev;
+
+ for (dev = net_devices; dev; dev = dev->next)
+ {
+ grub_efi_ip4_config2_policy_t ip4_policy = GRUB_EFI_IP4_CONFIG2_POLICY_STATIC;
+
+ if (dev->ip4_config->set_data (dev->ip4_config,
+ GRUB_EFI_IP4_CONFIG2_DATA_TYPE_POLICY,
+ sizeof (ip4_policy), &ip4_policy) != GRUB_EFI_SUCCESS)
+ grub_dprintf ("efinetfs", "could not set GRUB_EFI_IP4_CONFIG2_POLICY_STATIC on dev `%s'", dev->card_name);
+
+ if (dev->ip6_config)
+ {
+ grub_efi_ip6_config_policy_t ip6_policy = GRUB_EFI_IP6_CONFIG_POLICY_MANUAL;
+
+ if (dev->ip6_config->set_data (dev->ip6_config,
+ GRUB_EFI_IP6_CONFIG_DATA_TYPE_POLICY,
+ sizeof (ip6_policy), &ip6_policy) != GRUB_EFI_SUCCESS)
+ grub_dprintf ("efinetfs", "could not set GRUB_EFI_IP6_CONFIG_POLICY_MANUAL on dev `%s'", dev->card_name);
+ }
+ }
+}
+
+/* FIXME: Do not fail if the card did not support any of the protocol (Eg http) */
+static void
+grub_efi_net_find_cards (void)
+{
+ grub_efi_uintn_t num_handles;
+ grub_efi_handle_t *handles;
+ grub_efi_handle_t *handle;
+ int id;
+
+ handles = grub_efi_locate_handle (GRUB_EFI_BY_PROTOCOL, &ip4_config_guid,
+ 0, &num_handles);
+ if (!handles)
+ return;
+
+ for (id = 0, handle = handles; num_handles--; handle++, id++)
+ {
+ grub_efi_device_path_t *dp;
+ grub_efi_ip4_config2_protocol_t *ip4_config;
+ grub_efi_ip6_config_protocol_t *ip6_config;
+ grub_efi_handle_t http_handle;
+ grub_efi_http_t *http;
+ grub_efi_handle_t dhcp4_handle;
+ grub_efi_dhcp4_protocol_t *dhcp4;
+ grub_efi_handle_t dhcp6_handle;
+ grub_efi_dhcp6_protocol_t *dhcp6;
+
+ struct grub_efi_net_device *d;
+
+ dp = grub_efi_get_device_path (*handle);
+ if (!dp)
+ continue;
+
+ ip4_config = grub_efi_open_protocol (*handle, &ip4_config_guid,
+ GRUB_EFI_OPEN_PROTOCOL_GET_PROTOCOL);
+ if (!ip4_config)
+ continue;
+
+ ip6_config = grub_efi_open_protocol (*handle, &ip6_config_guid,
+ GRUB_EFI_OPEN_PROTOCOL_GET_PROTOCOL);
+
+ http_handle = grub_efi_service_binding (*handle, &http_service_binding_guid);
+ grub_errno = GRUB_ERR_NONE;
+ http = (http_handle)
+ ? grub_efi_open_protocol (http_handle, &http_guid, GRUB_EFI_OPEN_PROTOCOL_GET_PROTOCOL)
+ : NULL;
+
+ dhcp4_handle = grub_efi_service_binding (*handle, &dhcp4_service_binding_guid);
+ grub_errno = GRUB_ERR_NONE;
+ dhcp4 = (dhcp4_handle)
+ ? grub_efi_open_protocol (dhcp4_handle, &dhcp4_guid, GRUB_EFI_OPEN_PROTOCOL_GET_PROTOCOL)
+ : NULL;
+
+
+ dhcp6_handle = grub_efi_service_binding (*handle, &dhcp6_service_binding_guid);
+ grub_errno = GRUB_ERR_NONE;
+ dhcp6 = (dhcp6_handle)
+ ? grub_efi_open_protocol (dhcp6_handle, &dhcp6_guid, GRUB_EFI_OPEN_PROTOCOL_GET_PROTOCOL)
+ : NULL;
+
+ d = grub_malloc (sizeof (*d));
+ if (!d)
+ {
+ grub_free (handles);
+ while (net_devices)
+ {
+ d = net_devices->next;
+ grub_free (net_devices);
+ net_devices = d;
+ }
+ return;
+ }
+ d->handle = *handle;
+ d->ip4_config = ip4_config;
+ d->ip6_config = ip6_config;
+ d->http_handle = http_handle;
+ d->http = http;
+ d->dhcp4_handle = dhcp4_handle;
+ d->dhcp4 = dhcp4;
+ d->dhcp6_handle = dhcp6_handle;
+ d->dhcp6 = dhcp6;
+ d->next = net_devices;
+ d->card_name = grub_xasprintf ("efinet%d", id);
+ d->net_interfaces = NULL;
+ net_devices = d;
+ }
+
+ grub_efi_net_add_pxebc_to_cards ();
+ grub_free (handles);
+ set_ip_policy_to_static ();
+}
+
+static void
+listroutes_ip4 (struct grub_efi_net_device *netdev)
+{
+ char **routes;
+
+ routes = NULL;
+
+ if ((routes = efi_net_ip4_config->get_route_table (netdev)))
+ {
+ char **r;
+
+ for (r = routes; *r; ++r)
+ grub_printf ("%s\n", *r);
+ }
+
+ if (routes)
+ {
+ char **r;
+
+ for (r = routes; *r; ++r)
+ grub_free (*r);
+ grub_free (routes);
+ }
+}
+
+static void
+listroutes_ip6 (struct grub_efi_net_device *netdev)
+{
+ char **routes;
+
+ routes = NULL;
+
+ if ((routes = efi_net_ip6_config->get_route_table (netdev)))
+ {
+ char **r;
+
+ for (r = routes; *r; ++r)
+ grub_printf ("%s\n", *r);
+ }
+
+ if (routes)
+ {
+ char **r;
+
+ for (r = routes; *r; ++r)
+ grub_free (*r);
+ grub_free (routes);
+ }
+}
+
+static grub_err_t
+grub_cmd_efi_listroutes (struct grub_command *cmd __attribute__ ((unused)),
+ int argc __attribute__ ((unused)),
+ char **args __attribute__ ((unused)))
+{
+ struct grub_efi_net_device *netdev;
+
+ for (netdev = net_devices; netdev; netdev = netdev->next)
+ {
+ listroutes_ip4 (netdev);
+ listroutes_ip6 (netdev);
+ }
+
+ return GRUB_ERR_NONE;
+}
+static grub_err_t
+grub_cmd_efi_listcards (struct grub_command *cmd __attribute__ ((unused)),
+ int argc __attribute__ ((unused)),
+ char **args __attribute__ ((unused)))
+{
+ struct grub_efi_net_device *dev;
+
+ for (dev = net_devices; dev; dev = dev->next)
+ {
+ char *hw_addr;
+
+ hw_addr = efi_net_ip4_config->get_hw_address (dev);
+
+ if (hw_addr)
+ {
+ grub_printf ("%s %s\n", dev->card_name, hw_addr);
+ grub_free (hw_addr);
+ }
+ }
+
+ return GRUB_ERR_NONE;
+}
+
+static grub_err_t
+grub_cmd_efi_listaddrs (struct grub_command *cmd __attribute__ ((unused)),
+ int argc __attribute__ ((unused)),
+ char **args __attribute__ ((unused)))
+{
+ struct grub_efi_net_device *dev;
+ grub_efi_net_interface_t *inf;
+
+ for (dev = net_devices; dev; dev = dev->next)
+ for (inf = dev->net_interfaces; inf; inf = inf->next)
+ {
+ char *hw_addr = NULL;
+ char *addr = NULL;
+
+ if ((hw_addr = efi_net_interface_get_hw_address (inf))
+ && (addr = efi_net_interface_get_address (inf)))
+ grub_printf ("%s %s %s\n", inf->name, hw_addr, addr);
+
+ if (hw_addr)
+ grub_free (hw_addr);
+ if (addr)
+ grub_free (addr);
+ }
+
+ return GRUB_ERR_NONE;
+}
+
+/* FIXME: support MAC specifying. */
+static grub_err_t
+grub_cmd_efi_addaddr (struct grub_command *cmd __attribute__ ((unused)),
+ int argc, char **args)
+{
+ struct grub_efi_net_device *dev;
+ grub_err_t err;
+ grub_efi_ip4_config2_manual_address_t ip4;
+ grub_efi_ip6_config_manual_address_t ip6;
+ grub_efi_net_ip_manual_address_t net_ip;
+ int is_ip6 = 0;
+ int cidr = 0;
+
+ if (argc != 3)
+ return grub_error (GRUB_ERR_BAD_ARGUMENT, N_("three arguments expected"));
+
+ for (dev = net_devices; dev; dev = dev->next)
+ {
+ if (grub_strcmp (dev->card_name, args[1]) == 0)
+ break;
+ }
+
+ if (!dev)
+ return grub_error (GRUB_ERR_BAD_ARGUMENT, N_("card not found"));
+
+ err = grub_efi_net_parse_address (args[2], &ip4, &ip6, &is_ip6, &cidr);
+
+ if (err)
+ return err;
+
+ net_ip.is_ip6 = is_ip6;
+ if (is_ip6)
+ grub_memcpy (&net_ip.ip6, &ip6, sizeof(net_ip.ip6));
+ else
+ grub_memcpy (&net_ip.ip4, &ip4, sizeof(net_ip.ip4));
+
+ if (!grub_efi_net_create_interface (dev,
+ args[0],
+ &net_ip,
+ cidr))
+ return grub_errno;
+
+ return GRUB_ERR_NONE;
+}
+
+static struct grub_fs grub_efi_netfs;
+
+static grub_net_t
+grub_net_open_real (const char *name __attribute__ ((unused)))
+{
+ grub_size_t protnamelen;
+ const char *protname, *server;
+ grub_net_t ret;
+
+ net_interface = NULL;
+
+ if (grub_strncmp (name, "pxe:", sizeof ("pxe:") - 1) == 0)
+ {
+ protname = "tftp";
+ protnamelen = sizeof ("tftp") - 1;
+ server = name + sizeof ("pxe:") - 1;
+ }
+ else if (grub_strcmp (name, "pxe") == 0)
+ {
+ protname = "tftp";
+ protnamelen = sizeof ("tftp") - 1;
+ server = default_server;
+ }
+ else
+ {
+ const char *comma;
+
+ comma = grub_strchr (name, ',');
+ if (comma)
+ {
+ protnamelen = comma - name;
+ server = comma + 1;
+ protname = name;
+ }
+ else
+ {
+ protnamelen = grub_strlen (name);
+ server = default_server;
+ protname = name;
+ }
+ }
+
+ if (!server)
+ {
+ grub_error (GRUB_ERR_NET_BAD_ADDRESS,
+ N_("no server is specified"));
+ return NULL;
+ }
+
+ /*FIXME: Use DNS translate name to address */
+ net_interface = match_route (server);
+
+ /*XXX: should we check device with default gateway ? */
+ if (!net_interface && !(net_interface = net_default_interface))
+ {
+ grub_error (GRUB_ERR_UNKNOWN_DEVICE, N_("disk `%s' no route found"),
+ name);
+ return NULL;
+ }
+
+ if ((protnamelen == (sizeof ("https") - 1)
+ && grub_memcmp ("https", protname, protnamelen) == 0))
+ {
+ net_interface->io = &io_http;
+ net_interface->io_type = 1;
+ }
+ else if ((protnamelen == (sizeof ("http") - 1)
+ && grub_memcmp ("http", protname, protnamelen) == 0))
+ {
+ net_interface->io = &io_http;
+ net_interface->io_type = 0;
+ }
+ else if (protnamelen == (sizeof ("tftp") - 1)
+ && grub_memcmp ("tftp", protname, protnamelen) == 0)
+ {
+ net_interface->io = &io_pxe;
+ net_interface->io_type = 0;
+ }
+ else
+ {
+ grub_error (GRUB_ERR_UNKNOWN_DEVICE, N_("disk `%s' not found"),
+ name);
+ return NULL;
+ }
+
+ /*XXX: Should we try to avoid doing excess "reconfigure" here ??? */
+ efi_net_interface (configure);
+
+ ret = grub_zalloc (sizeof (*ret));
+ if (!ret)
+ return NULL;
+
+ ret->server = grub_strdup (server);
+ if (!ret->server)
+ {
+ grub_free (ret);
+ return NULL;
+ }
+
+ ret->fs = &grub_efi_netfs;
+ return ret;
+}
+#if 0
+static grub_command_t cmd_efi_lsaddr;
+static grub_command_t cmd_efi_lscards;
+static grub_command_t cmd_efi_lsroutes;
+static grub_command_t cmd_efi_addaddr;
+#endif
+
+static struct grub_fs grub_efi_netfs =
+ {
+ .name = "efi netfs",
+ .fs_dir = grub_efi_netfs_dir,
+ .fs_open = grub_efi_netfs_open,
+ .fs_read = grub_efi_netfs_read,
+ .fs_close = grub_efi_netfs_close,
+ .fs_label = NULL,
+ .fs_uuid = NULL,
+ .fs_mtime = NULL,
+ };
+
+int
+grub_efi_net_boot_from_https (void)
+{
+ grub_efi_loaded_image_t *image = NULL;
+ grub_efi_device_path_t *dp;
+
+ image = grub_efi_get_loaded_image (grub_efi_image_handle);
+ if (!image)
+ return 0;
+
+ dp = grub_efi_get_device_path (image->device_handle);
+
+ while (1)
+ {
+ grub_efi_uint8_t type = GRUB_EFI_DEVICE_PATH_TYPE (dp);
+ grub_efi_uint8_t subtype = GRUB_EFI_DEVICE_PATH_SUBTYPE (dp);
+ grub_efi_uint16_t len = GRUB_EFI_DEVICE_PATH_LENGTH (dp);
+
+ if ((type == GRUB_EFI_MESSAGING_DEVICE_PATH_TYPE)
+ && (subtype == GRUB_EFI_URI_DEVICE_PATH_SUBTYPE))
+ {
+ grub_efi_uri_device_path_t *uri_dp = (grub_efi_uri_device_path_t *) dp;
+ return (grub_strncmp ((const char*)uri_dp->uri, "https://", sizeof ("https://") - 1) == 0) ? 1 : 0;
+ }
+
+ if (GRUB_EFI_END_ENTIRE_DEVICE_PATH (dp))
+ break;
+ dp = (grub_efi_device_path_t *) ((char *) dp + len);
+ }
+
+ return 0;
+}
+
+int
+grub_efi_net_boot_from_opa (void)
+{
+ grub_efi_loaded_image_t *image = NULL;
+ grub_efi_device_path_t *dp;
+
+ image = grub_efi_get_loaded_image (grub_efi_image_handle);
+ if (!image)
+ return 0;
+
+ dp = grub_efi_get_device_path (image->device_handle);
+
+ while (1)
+ {
+ grub_efi_uint8_t type = GRUB_EFI_DEVICE_PATH_TYPE (dp);
+ grub_efi_uint8_t subtype = GRUB_EFI_DEVICE_PATH_SUBTYPE (dp);
+ grub_efi_uint16_t len = GRUB_EFI_DEVICE_PATH_LENGTH (dp);
+
+ if ((type == GRUB_EFI_MESSAGING_DEVICE_PATH_TYPE)
+ && (subtype == GRUB_EFI_MAC_ADDRESS_DEVICE_PATH_SUBTYPE))
+ {
+ grub_efi_mac_address_device_path_t *mac_dp = (grub_efi_mac_address_device_path_t *)dp;
+ return (mac_dp->if_type == 0xC7) ? 1 : 0;
+ }
+
+ if (GRUB_EFI_END_ENTIRE_DEVICE_PATH (dp))
+ break;
+ dp = (grub_efi_device_path_t *) ((char *) dp + len);
+ }
+
+ return 0;
+}
+
+static char *
+grub_env_write_readonly (struct grub_env_var *var __attribute__ ((unused)),
+ const char *val __attribute__ ((unused)))
+{
+ return NULL;
+}
+
+grub_command_func_t grub_efi_net_list_routes = grub_cmd_efi_listroutes;
+grub_command_func_t grub_efi_net_list_cards = grub_cmd_efi_listcards;
+grub_command_func_t grub_efi_net_list_addrs = grub_cmd_efi_listaddrs;
+grub_command_func_t grub_efi_net_add_addr = grub_cmd_efi_addaddr;
+
+int
+grub_efi_net_fs_init ()
+{
+ grub_efi_net_find_cards ();
+ grub_efi_net_config = grub_efi_net_config_real;
+ grub_net_open = grub_net_open_real;
+ grub_register_variable_hook ("net_default_server", grub_efi_net_var_get_server,
+ grub_efi_net_var_set_server);
+ grub_env_export ("net_default_server");
+ grub_register_variable_hook ("pxe_default_server", grub_efi_net_var_get_server,
+ grub_efi_net_var_set_server);
+ grub_env_export ("pxe_default_server");
+ grub_register_variable_hook ("net_default_interface", 0,
+ grub_efi_net_var_set_interface);
+ grub_env_export ("net_default_interface");
+ grub_register_variable_hook ("net_default_ip", grub_efi_net_var_get_ip,
+ 0);
+ grub_env_export ("net_default_ip");
+ grub_register_variable_hook ("net_default_mac", grub_efi_net_var_get_mac,
+ 0);
+ grub_env_export ("net_default_mac");
+
+ grub_env_set ("grub_netfs_type", "efi");
+ grub_register_variable_hook ("grub_netfs_type", 0, grub_env_write_readonly);
+ grub_env_export ("grub_netfs_type");
+
+ return 1;
+}
+
+void
+grub_efi_net_fs_fini (void)
+{
+ grub_env_unset ("grub_netfs_type");
+ grub_efi_net_unset_interface_vars ();
+ grub_register_variable_hook ("net_default_server", 0, 0);
+ grub_env_unset ("net_default_server");
+ grub_register_variable_hook ("net_default_interface", 0, 0);
+ grub_env_unset ("net_default_interface");
+ grub_register_variable_hook ("pxe_default_server", 0, 0);
+ grub_env_unset ("pxe_default_server");
+ grub_register_variable_hook ("net_default_ip", 0, 0);
+ grub_env_unset ("net_default_ip");
+ grub_register_variable_hook ("net_default_mac", 0, 0);
+ grub_env_unset ("net_default_mac");
+ grub_efi_net_config = NULL;
+ grub_net_open = NULL;
+ grub_fs_unregister (&grub_efi_netfs);
+}
diff --git a/grub-core/net/efi/pxe.c b/grub-core/net/efi/pxe.c
new file mode 100644
index 000000000..a8eff6eeb
--- /dev/null
+++ b/grub-core/net/efi/pxe.c
@@ -0,0 +1,424 @@
+
+#include <grub/efi/api.h>
+#include <grub/efi/efi.h>
+#include <grub/misc.h>
+#include <grub/net/efi.h>
+#include <grub/charset.h>
+
+static grub_efi_ip6_config_manual_address_t *
+efi_ip6_config_manual_address (grub_efi_ip6_config_protocol_t *ip6_config)
+{
+ grub_efi_uintn_t sz;
+ grub_efi_status_t status;
+ grub_efi_ip6_config_manual_address_t *manual_address;
+
+ sz = sizeof (*manual_address);
+ manual_address = grub_malloc (sz);
+ if (!manual_address)
+ return NULL;
+
+ status = ip6_config->get_data (ip6_config,
+ GRUB_EFI_IP6_CONFIG_DATA_TYPE_MANUAL_ADDRESS,
+ &sz, manual_address);
+
+ if (status != GRUB_EFI_SUCCESS)
+ {
+ grub_free (manual_address);
+ return NULL;
+ }
+
+ return manual_address;
+}
+
+static grub_efi_ip4_config2_manual_address_t *
+efi_ip4_config_manual_address (grub_efi_ip4_config2_protocol_t *ip4_config)
+{
+ grub_efi_uintn_t sz;
+ grub_efi_status_t status;
+ grub_efi_ip4_config2_manual_address_t *manual_address;
+
+ sz = sizeof (*manual_address);
+ manual_address = grub_malloc (sz);
+ if (!manual_address)
+ return NULL;
+
+ status = ip4_config->get_data (ip4_config,
+ GRUB_EFI_IP4_CONFIG2_DATA_TYPE_MANUAL_ADDRESS,
+ &sz, manual_address);
+
+ if (status != GRUB_EFI_SUCCESS)
+ {
+ grub_free (manual_address);
+ return NULL;
+ }
+
+ return manual_address;
+}
+
+static void
+pxe_configure (struct grub_efi_net_device *dev, int prefer_ip6)
+{
+ grub_efi_pxe_t *pxe = (prefer_ip6) ? dev->ip6_pxe : dev->ip4_pxe;
+
+ grub_efi_pxe_mode_t *mode = pxe->mode;
+
+ if (!mode->started)
+ {
+ grub_efi_status_t status;
+ status = pxe->start (pxe, prefer_ip6);
+
+ if (status != GRUB_EFI_SUCCESS)
+ grub_printf ("Couldn't start PXE\n");
+ }
+
+#if 0
+ grub_printf ("PXE STARTED: %u\n", mode->started);
+ grub_printf ("PXE USING IPV6: %u\n", mode->using_ipv6);
+#endif
+
+ if (mode->using_ipv6)
+ {
+ grub_efi_ip6_config_manual_address_t *manual_address;
+ manual_address = efi_ip6_config_manual_address (dev->ip6_config);
+
+ if (manual_address &&
+ grub_memcmp (manual_address->address, mode->station_ip.v6.addr, sizeof (manual_address->address)) != 0)
+ {
+ grub_efi_status_t status;
+ grub_efi_pxe_ip_address_t station_ip;
+
+ grub_memcpy (station_ip.v6.addr, manual_address->address, sizeof (station_ip.v6.addr));
+ status = pxe->set_station_ip (pxe, &station_ip, NULL);
+
+ if (status != GRUB_EFI_SUCCESS)
+ grub_printf ("Couldn't set station ip\n");
+
+ grub_free (manual_address);
+ }
+ }
+ else
+ {
+ grub_efi_ip4_config2_manual_address_t *manual_address;
+ manual_address = efi_ip4_config_manual_address (dev->ip4_config);
+
+ if (manual_address &&
+ grub_memcmp (manual_address->address, mode->station_ip.v4.addr, sizeof (manual_address->address)) != 0)
+ {
+ grub_efi_status_t status;
+ grub_efi_pxe_ip_address_t station_ip;
+ grub_efi_pxe_ip_address_t subnet_mask;
+
+ grub_memcpy (station_ip.v4.addr, manual_address->address, sizeof (station_ip.v4.addr));
+ grub_memcpy (subnet_mask.v4.addr, manual_address->subnet_mask, sizeof (subnet_mask.v4.addr));
+
+ status = pxe->set_station_ip (pxe, &station_ip, &subnet_mask);
+
+ if (status != GRUB_EFI_SUCCESS)
+ grub_printf ("Couldn't set station ip\n");
+
+ grub_free (manual_address);
+ }
+ }
+
+#if 0
+ if (mode->using_ipv6)
+ {
+ grub_printf ("PXE STATION IP: %02x%02x:%02x%02x:%02x%02x:%02x%02x:%02x%02x:%02x%02x:%02x%02x:%02x%02x\n",
+ mode->station_ip.v6.addr[0],
+ mode->station_ip.v6.addr[1],
+ mode->station_ip.v6.addr[2],
+ mode->station_ip.v6.addr[3],
+ mode->station_ip.v6.addr[4],
+ mode->station_ip.v6.addr[5],
+ mode->station_ip.v6.addr[6],
+ mode->station_ip.v6.addr[7],
+ mode->station_ip.v6.addr[8],
+ mode->station_ip.v6.addr[9],
+ mode->station_ip.v6.addr[10],
+ mode->station_ip.v6.addr[11],
+ mode->station_ip.v6.addr[12],
+ mode->station_ip.v6.addr[13],
+ mode->station_ip.v6.addr[14],
+ mode->station_ip.v6.addr[15]);
+ }
+ else
+ {
+ grub_printf ("PXE STATION IP: %d.%d.%d.%d\n",
+ mode->station_ip.v4.addr[0],
+ mode->station_ip.v4.addr[1],
+ mode->station_ip.v4.addr[2],
+ mode->station_ip.v4.addr[3]);
+ grub_printf ("PXE SUBNET MASK: %d.%d.%d.%d\n",
+ mode->subnet_mask.v4.addr[0],
+ mode->subnet_mask.v4.addr[1],
+ mode->subnet_mask.v4.addr[2],
+ mode->subnet_mask.v4.addr[3]);
+ }
+#endif
+
+ /* TODO: Set The Station IP to the IP2 Config */
+}
+
+static int
+parse_ip6 (const char *val, grub_uint64_t *ip, const char **rest)
+{
+ grub_uint16_t newip[8];
+ const char *ptr = val;
+ int word, quaddot = -1;
+ int bracketed = 0;
+
+ if (ptr[0] == '[') {
+ bracketed = 1;
+ ptr++;
+ }
+
+ if (ptr[0] == ':' && ptr[1] != ':')
+ return 0;
+ if (ptr[0] == ':')
+ ptr++;
+
+ for (word = 0; word < 8; word++)
+ {
+ unsigned long t;
+ if (*ptr == ':')
+ {
+ quaddot = word;
+ word--;
+ ptr++;
+ continue;
+ }
+ t = grub_strtoul (ptr, &ptr, 16);
+ if (grub_errno)
+ {
+ grub_errno = GRUB_ERR_NONE;
+ break;
+ }
+ if (t & ~0xffff)
+ return 0;
+ newip[word] = grub_cpu_to_be16 (t);
+ if (*ptr != ':')
+ break;
+ ptr++;
+ }
+ if (quaddot == -1 && word < 7)
+ return 0;
+ if (quaddot != -1)
+ {
+ grub_memmove (&newip[quaddot + 7 - word], &newip[quaddot],
+ (word - quaddot + 1) * sizeof (newip[0]));
+ grub_memset (&newip[quaddot], 0, (7 - word) * sizeof (newip[0]));
+ }
+ grub_memcpy (ip, newip, 16);
+ if (bracketed && *ptr == ']') {
+ ptr++;
+ }
+ if (rest)
+ *rest = ptr;
+ return 1;
+}
+
+static grub_err_t
+pxe_open (struct grub_efi_net_device *dev,
+ int prefer_ip6,
+ grub_file_t file,
+ const char *filename,
+ int type __attribute__((unused)))
+{
+ int i;
+ const char *p;
+ grub_efi_status_t status;
+ grub_efi_pxe_ip_address_t server_ip;
+ grub_efi_uint64_t file_size = 0;
+ grub_efi_pxe_t *pxe = (prefer_ip6) ? dev->ip6_pxe : dev->ip4_pxe;
+
+ if (pxe->mode->using_ipv6)
+ {
+ const char *rest;
+ grub_uint64_t ip6[2];
+ if (parse_ip6 (file->device->net->server, ip6, &rest) && *rest == 0)
+ grub_memcpy (server_ip.v6.addr, ip6, sizeof (server_ip.v6.addr));
+ /* TODO: ERROR Handling Here */
+#if 0
+ grub_printf ("PXE SERVER IP: %02x%02x:%02x%02x:%02x%02x:%02x%02x:%02x%02x:%02x%02x:%02x%02x:%02x%02x\n",
+ server_ip.v6.addr[0],
+ server_ip.v6.addr[1],
+ server_ip.v6.addr[2],
+ server_ip.v6.addr[3],
+ server_ip.v6.addr[4],
+ server_ip.v6.addr[5],
+ server_ip.v6.addr[6],
+ server_ip.v6.addr[7],
+ server_ip.v6.addr[8],
+ server_ip.v6.addr[9],
+ server_ip.v6.addr[10],
+ server_ip.v6.addr[11],
+ server_ip.v6.addr[12],
+ server_ip.v6.addr[13],
+ server_ip.v6.addr[14],
+ server_ip.v6.addr[15]);
+#endif
+ }
+ else
+ {
+ for (i = 0, p = file->device->net->server; i < 4; ++i, ++p)
+ server_ip.v4.addr[i] = grub_strtoul (p, &p, 10);
+ }
+
+ status = pxe->mtftp (
+ pxe,
+ GRUB_EFI_PXE_BASE_CODE_TFTP_GET_FILE_SIZE,
+ NULL,
+ 0,
+ &file_size,
+ NULL,
+ &server_ip,
+ (grub_efi_char8_t *)filename,
+ NULL,
+ 0);
+
+ if (status != GRUB_EFI_SUCCESS)
+ return grub_error (GRUB_ERR_IO, "Couldn't get file size");
+
+ file->size = (grub_off_t)file_size;
+ file->not_easily_seekable = 0;
+ file->data = 0;
+ file->device->net->offset = 0;
+
+ return GRUB_ERR_NONE;
+}
+
+static grub_err_t
+pxe_close (struct grub_efi_net_device *dev __attribute__((unused)),
+ int prefer_ip6 __attribute__((unused)),
+ grub_file_t file __attribute__((unused)))
+{
+ file->offset = 0;
+ file->size = 0;
+ file->device->net->offset = 0;
+
+ if (file->data)
+ {
+ grub_free (file->data);
+ file->data = NULL;
+ }
+
+ return GRUB_ERR_NONE;
+}
+
+static grub_ssize_t
+pxe_read (struct grub_efi_net_device *dev,
+ int prefer_ip6,
+ grub_file_t file,
+ char *buf,
+ grub_size_t len)
+{
+ int i;
+ const char *p;
+ grub_efi_status_t status;
+ grub_efi_pxe_t *pxe = (prefer_ip6) ? dev->ip6_pxe : dev->ip4_pxe;
+ grub_efi_uint64_t bufsz = len;
+ grub_efi_pxe_ip_address_t server_ip;
+ char *buf2 = NULL;
+
+ if (file->data)
+ {
+ /* TODO: RANGE Check for offset and file size */
+ grub_memcpy (buf, (char*)file->data + file->device->net->offset, len);
+ file->device->net->offset += len;
+ return len;
+ }
+
+ if (file->device->net->offset)
+ {
+ grub_error (GRUB_ERR_BUG, "No Offet Read Possible");
+ grub_print_error ();
+ return 0;
+ }
+
+ if (pxe->mode->using_ipv6)
+ {
+ const char *rest;
+ grub_uint64_t ip6[2];
+ if (parse_ip6 (file->device->net->server, ip6, &rest) && *rest == 0)
+ grub_memcpy (server_ip.v6.addr, ip6, sizeof (server_ip.v6.addr));
+ /* TODO: ERROR Handling Here */
+ }
+ else
+ {
+ for (i = 0, p = file->device->net->server; i < 4; ++i, ++p)
+ server_ip.v4.addr[i] = grub_strtoul (p, &p, 10);
+ }
+
+ status = pxe->mtftp (
+ pxe,
+ GRUB_EFI_PXE_BASE_CODE_TFTP_READ_FILE,
+ buf,
+ 0,
+ &bufsz,
+ NULL,
+ &server_ip,
+ (grub_efi_char8_t *)file->device->net->name,
+ NULL,
+ 0);
+
+ if (bufsz != file->size)
+ {
+ grub_error (GRUB_ERR_BUG, "Short read should not happen here");
+ grub_print_error ();
+ return 0;
+ }
+
+ if (status == GRUB_EFI_BUFFER_TOO_SMALL)
+ {
+
+ buf2 = grub_malloc (bufsz);
+
+ if (!buf2)
+ {
+ grub_error (GRUB_ERR_OUT_OF_MEMORY, "ERROR OUT OF MEMORY");
+ grub_print_error ();
+ return 0;
+ }
+
+ status = pxe->mtftp (
+ pxe,
+ GRUB_EFI_PXE_BASE_CODE_TFTP_READ_FILE,
+ buf2,
+ 0,
+ &bufsz,
+ NULL,
+ &server_ip,
+ (grub_efi_char8_t *)file->device->net->name,
+ NULL,
+ 0);
+ }
+
+ if (status != GRUB_EFI_SUCCESS)
+ {
+ if (buf2)
+ grub_free (buf2);
+
+ grub_error (GRUB_ERR_IO, "Failed to Read File");
+ grub_print_error ();
+ return 0;
+ }
+
+ if (buf2)
+ grub_memcpy (buf, buf2, len);
+
+ file->device->net->offset = len;
+
+ if (buf2)
+ file->data = buf2;
+
+ return len;
+}
+
+struct grub_efi_net_io io_pxe =
+ {
+ .configure = pxe_configure,
+ .open = pxe_open,
+ .read = pxe_read,
+ .close = pxe_close
+ };
+
diff --git a/grub-core/net/net.c b/grub-core/net/net.c
index 8cad4fb6d..22221dc6a 100644
--- a/grub-core/net/net.c
+++ b/grub-core/net/net.c
@@ -32,6 +32,9 @@
#include <grub/loader.h>
#include <grub/bufio.h>
#include <grub/kernel.h>
+#ifdef GRUB_MACHINE_EFI
+#include <grub/net/efi.h>
+#endif
GRUB_MOD_LICENSE ("GPLv3+");
@@ -2014,8 +2017,49 @@ static grub_command_t cmd_addaddr, cmd_deladdr, cmd_addroute, cmd_delroute;
static grub_command_t cmd_setvlan, cmd_lsroutes, cmd_lscards;
static grub_command_t cmd_lsaddr, cmd_slaac;
+#ifdef GRUB_MACHINE_EFI
+
+static enum {
+ INIT_MODE_NONE,
+ INIT_MODE_GRUB,
+ INIT_MODE_EFI
+} init_mode;
+
+static grub_command_t cmd_bootp, cmd_bootp6;
+
+#endif
+
GRUB_MOD_INIT(net)
{
+#ifdef GRUB_MACHINE_EFI
+ if (grub_net_open)
+ return;
+
+ if ((grub_efi_net_boot_from_https () || grub_efi_net_boot_from_opa ())
+ && grub_efi_net_fs_init ())
+ {
+ cmd_lsroutes = grub_register_command ("net_ls_routes", grub_efi_net_list_routes,
+ "", N_("list network routes"));
+ cmd_lscards = grub_register_command ("net_ls_cards", grub_efi_net_list_cards,
+ "", N_("list network cards"));
+ cmd_lsaddr = grub_register_command ("net_ls_addr", grub_efi_net_list_addrs,
+ "", N_("list network addresses"));
+ cmd_addaddr = grub_register_command ("net_add_addr", grub_efi_net_add_addr,
+ /* TRANSLATORS: HWADDRESS stands for
+ "hardware address". */
+ N_("SHORTNAME CARD ADDRESS [HWADDRESS]"),
+ N_("Add a network address."));
+ cmd_bootp = grub_register_command ("net_bootp", grub_efi_net_bootp,
+ N_("[CARD]"),
+ N_("perform a bootp autoconfiguration"));
+ cmd_bootp6 = grub_register_command ("net_bootp6", grub_efi_net_bootp6,
+ N_("[CARD]"),
+ N_("perform a bootp autoconfiguration"));
+ init_mode = INIT_MODE_EFI;
+ return;
+ }
+#endif
+
grub_register_variable_hook ("net_default_server", defserver_get_env,
defserver_set_env);
grub_env_export ("net_default_server");
@@ -2066,10 +2110,37 @@ GRUB_MOD_INIT(net)
grub_net_restore_hw,
GRUB_LOADER_PREBOOT_HOOK_PRIO_DISK);
grub_net_poll_cards_idle = grub_net_poll_cards_idle_real;
+
+#ifdef GRUB_MACHINE_EFI
+ grub_env_set ("grub_netfs_type", "grub");
+ grub_register_variable_hook ("grub_netfs_type", 0, grub_env_write_readonly);
+ grub_env_export ("grub_netfs_type");
+ init_mode = INIT_MODE_GRUB;
+#endif
+
}
GRUB_MOD_FINI(net)
{
+
+#ifdef GRUB_MACHINE_EFI
+ if (init_mode == INIT_MODE_NONE)
+ return;
+
+ if (init_mode == INIT_MODE_EFI)
+ {
+ grub_unregister_command (cmd_lsroutes);
+ grub_unregister_command (cmd_lscards);
+ grub_unregister_command (cmd_lsaddr);
+ grub_unregister_command (cmd_addaddr);
+ grub_unregister_command (cmd_bootp);
+ grub_unregister_command (cmd_bootp6);
+ grub_efi_net_fs_fini ();
+ init_mode = INIT_MODE_NONE;
+ return;
+ }
+#endif
+
grub_register_variable_hook ("net_default_server", 0, 0);
grub_register_variable_hook ("pxe_default_server", 0, 0);
@@ -2088,4 +2159,7 @@ GRUB_MOD_FINI(net)
grub_net_fini_hw (0);
grub_loader_unregister_preboot_hook (fini_hnd);
grub_net_poll_cards_idle = NULL;
+#ifdef GRUB_MACHINE_EFI
+ init_mode = INIT_MODE_NONE;
+#endif
}
diff --git a/include/grub/efi/api.h b/include/grub/efi/api.h
index 87dcfc0a5..7947cf592 100644
--- a/include/grub/efi/api.h
+++ b/include/grub/efi/api.h
@@ -653,6 +653,23 @@ typedef grub_uint16_t grub_efi_ipv6_address_t[8];
typedef grub_uint8_t grub_efi_ip_address_t[8] __attribute__ ((aligned(4)));
typedef grub_efi_uint64_t grub_efi_physical_address_t;
typedef grub_efi_uint64_t grub_efi_virtual_address_t;
+typedef struct {
+ grub_uint8_t addr[4];
+} grub_efi_pxe_ipv4_address_t;
+
+typedef struct {
+ grub_uint8_t addr[16];
+} grub_efi_pxe_ipv6_address_t;
+
+typedef struct {
+ grub_uint8_t addr[32];
+} grub_efi_pxe_mac_address_t;
+
+typedef union {
+ grub_uint32_t addr[4];
+ grub_efi_pxe_ipv4_address_t v4;
+ grub_efi_pxe_ipv6_address_t v6;
+} grub_efi_pxe_ip_address_t;
/* XXX although the spec does not specify the padding, this actually
must have the padding! */
@@ -902,6 +919,8 @@ struct grub_efi_ipv6_device_path
grub_efi_uint16_t remote_port;
grub_efi_uint16_t protocol;
grub_efi_uint8_t static_ip_address;
+ grub_efi_uint8_t prefix_length;
+ grub_efi_ipv6_address_t gateway_ip_address;
} GRUB_PACKED;
typedef struct grub_efi_ipv6_device_path grub_efi_ipv6_device_path_t;
@@ -960,6 +979,15 @@ struct grub_efi_uri_device_path
} GRUB_PACKED;
typedef struct grub_efi_uri_device_path grub_efi_uri_device_path_t;
+#define GRUB_EFI_DNS_DEVICE_PATH_SUBTYPE 31
+struct grub_efi_dns_device_path
+{
+ grub_efi_device_path_t header;
+ grub_efi_uint8_t is_ipv6;
+ grub_efi_pxe_ip_address_t dns_server_ip[0];
+} GRUB_PACKED;
+typedef struct grub_efi_dns_device_path grub_efi_dns_device_path_t;
+
#define GRUB_EFI_VENDOR_MESSAGING_DEVICE_PATH_SUBTYPE 10
/* Media Device Path. */
@@ -1042,6 +1070,23 @@ struct grub_efi_bios_device_path
} GRUB_PACKED;
typedef struct grub_efi_bios_device_path grub_efi_bios_device_path_t;
+/* Service Binding definitions */
+struct grub_efi_service_binding;
+
+typedef grub_efi_status_t
+(__grub_efi_api *grub_efi_service_binding_create_child) (struct grub_efi_service_binding *this,
+ grub_efi_handle_t *child_handle);
+
+typedef grub_efi_status_t
+(__grub_efi_api *grub_efi_service_binding_destroy_child) (struct grub_efi_service_binding *this,
+ grub_efi_handle_t *child_handle);
+
+typedef struct grub_efi_service_binding
+{
+ grub_efi_service_binding_create_child create_child;
+ grub_efi_service_binding_destroy_child destroy_child;
+} grub_efi_service_binding_t;
+
struct grub_efi_open_protocol_information_entry
{
grub_efi_handle_t agent_handle;
@@ -1544,23 +1589,28 @@ typedef struct grub_efi_simple_text_output_interface grub_efi_simple_text_output
typedef grub_uint8_t grub_efi_pxe_packet_t[1472];
-typedef struct {
- grub_uint8_t addr[4];
-} grub_efi_pxe_ipv4_address_t;
+typedef grub_efi_uint16_t grub_efi_pxe_base_code_udp_port_t;
-typedef struct {
- grub_uint8_t addr[16];
-} grub_efi_pxe_ipv6_address_t;
+typedef enum {
+ GRUB_EFI_PXE_BASE_CODE_TFTP_FIRST,
+ GRUB_EFI_PXE_BASE_CODE_TFTP_GET_FILE_SIZE,
+ GRUB_EFI_PXE_BASE_CODE_TFTP_READ_FILE,
+ GRUB_EFI_PXE_BASE_CODE_TFTP_WRITE_FILE,
+ GRUB_EFI_PXE_BASE_CODE_TFTP_READ_DIRECTORY,
+ GRUB_EFI_PXE_BASE_CODE_MTFTP_GET_FILE_SIZE,
+ GRUB_EFI_PXE_BASE_CODE_MTFTP_READ_FILE,
+ GRUB_EFI_PXE_BASE_CODE_MTFTP_READ_DIRECTORY,
+ GRUB_EFI_PXE_BASE_CODE_MTFTP_LAST
+} grub_efi_pxe_base_code_tftp_opcode_t;
-typedef struct {
- grub_uint8_t addr[32];
-} grub_efi_pxe_mac_address_t;
-typedef union {
- grub_uint32_t addr[4];
- grub_efi_pxe_ipv4_address_t v4;
- grub_efi_pxe_ipv6_address_t v6;
-} grub_efi_pxe_ip_address_t;
+typedef struct {
+ grub_efi_ip_address_t mcast_ip;
+ grub_efi_pxe_base_code_udp_port_t c_port;
+ grub_efi_pxe_base_code_udp_port_t s_port;
+ grub_efi_uint16_t listen_timeout;
+ grub_efi_uint16_t transmit_timeout;
+} grub_efi_pxe_base_code_mtftp_info_t;
#define GRUB_EFI_PXE_BASE_CODE_MAX_IPCNT 8
typedef struct {
@@ -1610,18 +1660,32 @@ typedef struct grub_efi_pxe_mode
typedef struct grub_efi_pxe
{
grub_uint64_t rev;
- void *start;
- void *stop;
- void *dhcp;
- void *discover;
- void *mftp;
- void *udpwrite;
- void *udpread;
- void *setipfilter;
- void *arp;
- void *setparams;
- void *setstationip;
- void *setpackets;
+ grub_efi_status_t (__grub_efi_api *start) (struct grub_efi_pxe *this, grub_efi_boolean_t use_ipv6);
+ void (__grub_efi_api *stop) (void);
+ grub_efi_status_t (__grub_efi_api *dhcp) (struct grub_efi_pxe *this,
+ grub_efi_boolean_t sort_offers);
+ void (__grub_efi_api *discover) (void);
+ grub_efi_status_t (__grub_efi_api *mtftp) (struct grub_efi_pxe *this,
+ grub_efi_pxe_base_code_tftp_opcode_t operation,
+ void *buffer_ptr,
+ grub_efi_boolean_t overwrite,
+ grub_efi_uint64_t *buffer_size,
+ grub_efi_uintn_t *block_size,
+ grub_efi_pxe_ip_address_t *server_ip,
+ //grub_efi_ip_address_t *server_ip,
+ grub_efi_char8_t *filename,
+ grub_efi_pxe_base_code_mtftp_info_t *info,
+ grub_efi_boolean_t dont_use_buffer);
+ void (__grub_efi_api *udpwrite) (void);
+ void (__grub_efi_api *udpread) (void);
+ void (__grub_efi_api *setipfilter) (void);
+ void (__grub_efi_api *arp) (void);
+ void (__grub_efi_api *setparams) (void);
+ grub_efi_status_t (__grub_efi_api *set_station_ip) (struct grub_efi_pxe *this,
+ grub_efi_pxe_ip_address_t *new_station_ip,
+ grub_efi_pxe_ip_address_t *new_subnet_mask);
+ //void (*setstationip) (void);
+ void (__grub_efi_api *setpackets) (void);
struct grub_efi_pxe_mode *mode;
} grub_efi_pxe_t;
@@ -1921,6 +1985,44 @@ struct grub_efi_ip4_config2_protocol
};
typedef struct grub_efi_ip4_config2_protocol grub_efi_ip4_config2_protocol_t;
+struct grub_efi_ip4_route_table {
+ grub_efi_ipv4_address_t subnet_address;
+ grub_efi_ipv4_address_t subnet_mask;
+ grub_efi_ipv4_address_t gateway_address;
+};
+
+typedef struct grub_efi_ip4_route_table grub_efi_ip4_route_table_t;
+
+#define GRUB_EFI_IP4_CONFIG2_INTERFACE_INFO_NAME_SIZE 32
+
+struct grub_efi_ip4_config2_interface_info {
+ grub_efi_char16_t name[GRUB_EFI_IP4_CONFIG2_INTERFACE_INFO_NAME_SIZE];
+ grub_efi_uint8_t if_type;
+ grub_efi_uint32_t hw_address_size;
+ grub_efi_mac_address_t hw_address;
+ grub_efi_ipv4_address_t station_address;
+ grub_efi_ipv4_address_t subnet_mask;
+ grub_efi_uint32_t route_table_size;
+ grub_efi_ip4_route_table_t *route_table;
+};
+
+typedef struct grub_efi_ip4_config2_interface_info grub_efi_ip4_config2_interface_info_t;
+
+enum grub_efi_ip4_config2_policy {
+ GRUB_EFI_IP4_CONFIG2_POLICY_STATIC,
+ GRUB_EFI_IP4_CONFIG2_POLICY_DHCP,
+ GRUB_EFI_IP4_CONFIG2_POLICY_MAX
+};
+
+typedef enum grub_efi_ip4_config2_policy grub_efi_ip4_config2_policy_t;
+
+struct grub_efi_ip4_config2_manual_address {
+ grub_efi_ipv4_address_t address;
+ grub_efi_ipv4_address_t subnet_mask;
+};
+
+typedef struct grub_efi_ip4_config2_manual_address grub_efi_ip4_config2_manual_address_t;
+
enum grub_efi_ip6_config_data_type {
GRUB_EFI_IP6_CONFIG_DATA_TYPE_INTERFACEINFO,
GRUB_EFI_IP6_CONFIG_DATA_TYPE_ALT_INTERFACEID,
@@ -1955,4 +2057,47 @@ struct grub_efi_ip6_config_protocol
};
typedef struct grub_efi_ip6_config_protocol grub_efi_ip6_config_protocol_t;
+enum grub_efi_ip6_config_policy {
+ GRUB_EFI_IP6_CONFIG_POLICY_MANUAL,
+ GRUB_EFI_IP6_CONFIG_POLICY_AUTOMATIC
+};
+typedef enum grub_efi_ip6_config_policy grub_efi_ip6_config_policy_t;
+
+struct grub_efi_ip6_address_info {
+ grub_efi_ipv6_address_t address;
+ grub_efi_uint8_t prefix_length;
+};
+typedef struct grub_efi_ip6_address_info grub_efi_ip6_address_info_t;
+
+struct grub_efi_ip6_route_table {
+ grub_efi_pxe_ipv6_address_t gateway;
+ grub_efi_pxe_ipv6_address_t destination;
+ grub_efi_uint8_t prefix_length;
+};
+typedef struct grub_efi_ip6_route_table grub_efi_ip6_route_table_t;
+
+struct grub_efi_ip6_config_interface_info {
+ grub_efi_char16_t name[32];
+ grub_efi_uint8_t if_type;
+ grub_efi_uint32_t hw_address_size;
+ grub_efi_mac_address_t hw_address;
+ grub_efi_uint32_t address_info_count;
+ grub_efi_ip6_address_info_t *address_info;
+ grub_efi_uint32_t route_count;
+ grub_efi_ip6_route_table_t *route_table;
+};
+typedef struct grub_efi_ip6_config_interface_info grub_efi_ip6_config_interface_info_t;
+
+struct grub_efi_ip6_config_dup_addr_detect_transmits {
+ grub_efi_uint32_t dup_addr_detect_transmits;
+};
+typedef struct grub_efi_ip6_config_dup_addr_detect_transmits grub_efi_ip6_config_dup_addr_detect_transmits_t;
+
+struct grub_efi_ip6_config_manual_address {
+ grub_efi_ipv6_address_t address;
+ grub_efi_boolean_t is_anycast;
+ grub_efi_uint8_t prefix_length;
+};
+typedef struct grub_efi_ip6_config_manual_address grub_efi_ip6_config_manual_address_t;
+
#endif /* ! GRUB_EFI_API_HEADER */
diff --git a/include/grub/efi/dhcp.h b/include/grub/efi/dhcp.h
new file mode 100644
index 000000000..e4518128a
--- /dev/null
+++ b/include/grub/efi/dhcp.h
@@ -0,0 +1,343 @@
+#ifndef GRUB_EFI_DHCP_HEADER
+#define GRUB_EFI_DHCP_HEADER 1
+
+#define GRUB_EFI_DHCP4_SERVICE_BINDING_PROTOCOL_GUID \
+ { 0x9d9a39d8, 0xbd42, 0x4a73, \
+ { 0xa4, 0xd5, 0x8e, 0xe9, 0x4b, 0xe1, 0x13, 0x80 } \
+ }
+
+#define GRUB_EFI_DHCP4_PROTOCOL_GUID \
+ { 0x8a219718, 0x4ef5, 0x4761, \
+ { 0x91, 0xc8, 0xc0, 0xf0, 0x4b, 0xda, 0x9e, 0x56 } \
+ }
+
+#define GRUB_EFI_DHCP6_SERVICE_BINDING_PROTOCOL_GUID \
+ { 0x9fb9a8a1, 0x2f4a, 0x43a6, \
+ { 0x88, 0x9c, 0xd0, 0xf7, 0xb6, 0xc4 ,0x7a, 0xd5 } \
+ }
+
+#define GRUB_EFI_DHCP6_PROTOCOL_GUID \
+ { 0x87c8bad7, 0x595, 0x4053, \
+ { 0x82, 0x97, 0xde, 0xde, 0x39, 0x5f, 0x5d, 0x5b } \
+ }
+
+typedef struct grub_efi_dhcp4_protocol grub_efi_dhcp4_protocol_t;
+
+enum grub_efi_dhcp4_state {
+ GRUB_EFI_DHCP4_STOPPED,
+ GRUB_EFI_DHCP4_INIT,
+ GRUB_EFI_DHCP4_SELECTING,
+ GRUB_EFI_DHCP4_REQUESTING,
+ GRUB_EFI_DHCP4_BOUND,
+ GRUB_EFI_DHCP4_RENEWING,
+ GRUB_EFI_DHCP4_REBINDING,
+ GRUB_EFI_DHCP4_INIT_REBOOT,
+ GRUB_EFI_DHCP4_REBOOTING
+};
+
+typedef enum grub_efi_dhcp4_state grub_efi_dhcp4_state_t;
+
+struct grub_efi_dhcp4_header {
+ grub_efi_uint8_t op_code;
+ grub_efi_uint8_t hw_type;
+ grub_efi_uint8_t hw_addr_len;
+ grub_efi_uint8_t hops;
+ grub_efi_uint32_t xid;
+ grub_efi_uint16_t seconds;
+ grub_efi_uint16_t reserved;
+ grub_efi_ipv4_address_t client_addr;
+ grub_efi_ipv4_address_t your_addr;
+ grub_efi_ipv4_address_t server_addr;
+ grub_efi_ipv4_address_t gateway_addr;
+ grub_efi_uint8_t client_hw_addr[16];
+ grub_efi_char8_t server_name[64];
+ grub_efi_char8_t boot_file_name[128];
+} GRUB_PACKED;
+
+typedef struct grub_efi_dhcp4_header grub_efi_dhcp4_header_t;
+
+struct grub_efi_dhcp4_packet {
+ grub_efi_uint32_t size;
+ grub_efi_uint32_t length;
+ struct {
+ grub_efi_dhcp4_header_t header;
+ grub_efi_uint32_t magik;
+ grub_efi_uint8_t option[1];
+ } dhcp4;
+} GRUB_PACKED;
+
+typedef struct grub_efi_dhcp4_packet grub_efi_dhcp4_packet_t;
+
+struct grub_efi_dhcp4_listen_point {
+ grub_efi_ipv4_address_t listen_address;
+ grub_efi_ipv4_address_t subnet_mask;
+ grub_efi_uint16_t listen_port;
+};
+
+typedef struct grub_efi_dhcp4_listen_point grub_efi_dhcp4_listen_point_t;
+
+struct grub_efi_dhcp4_transmit_receive_token {
+ grub_efi_status_t status;
+ grub_efi_event_t completion_event;
+ grub_efi_ipv4_address_t remote_address;
+ grub_efi_uint16_t remote_port;
+ grub_efi_ipv4_address_t gateway_address;
+ grub_efi_uint32_t listen_point_count;
+ grub_efi_dhcp4_listen_point_t *listen_points;
+ grub_efi_uint32_t timeout_value;
+ grub_efi_dhcp4_packet_t *packet;
+ grub_efi_uint32_t response_count;
+ grub_efi_dhcp4_packet_t *response_list;
+};
+
+typedef struct grub_efi_dhcp4_transmit_receive_token grub_efi_dhcp4_transmit_receive_token_t;
+
+enum grub_efi_dhcp4_event {
+ GRUB_EFI_DHCP4_SEND_DISCOVER = 0X01,
+ GRUB_EFI_DHCP4_RCVD_OFFER,
+ GRUB_EFI_DHCP4_SELECT_OFFER,
+ GRUB_EFI_DHCP4_SEND_REQUEST,
+ GRUB_EFI_DHCP4_RCVD_ACK,
+ GRUB_EFI_DHCP4_RCVD_NAK,
+ GRUB_EFI_DHCP4_SEND_DECLINE,
+ GRUB_EFI_DHCP4_BOUND_COMPLETED,
+ GRUB_EFI_DHCP4_ENTER_RENEWING,
+ GRUB_EFI_DHCP4_ENTER_REBINDING,
+ GRUB_EFI_DHCP4_ADDRESS_LOST,
+ GRUB_EFI_DHCP4_FAIL
+};
+
+typedef enum grub_efi_dhcp4_event grub_efi_dhcp4_event_t;
+
+struct grub_efi_dhcp4_packet_option {
+ grub_efi_uint8_t op_code;
+ grub_efi_uint8_t length;
+ grub_efi_uint8_t data[1];
+} GRUB_PACKED;
+
+typedef struct grub_efi_dhcp4_packet_option grub_efi_dhcp4_packet_option_t;
+
+struct grub_efi_dhcp4_config_data {
+ grub_efi_uint32_t discover_try_count;
+ grub_efi_uint32_t *discover_timeout;
+ grub_efi_uint32_t request_try_count;
+ grub_efi_uint32_t *request_timeout;
+ grub_efi_ipv4_address_t client_address;
+ grub_efi_status_t (*dhcp4_callback) (
+ grub_efi_dhcp4_protocol_t *this,
+ void *context,
+ grub_efi_dhcp4_state_t current_state,
+ grub_efi_dhcp4_event_t dhcp4_event,
+ grub_efi_dhcp4_packet_t *packet,
+ grub_efi_dhcp4_packet_t **new_packet
+ );
+ void *callback_context;
+ grub_efi_uint32_t option_count;
+ grub_efi_dhcp4_packet_option_t **option_list;
+};
+
+typedef struct grub_efi_dhcp4_config_data grub_efi_dhcp4_config_data_t;
+
+struct grub_efi_dhcp4_mode_data {
+ grub_efi_dhcp4_state_t state;
+ grub_efi_dhcp4_config_data_t config_data;
+ grub_efi_ipv4_address_t client_address;
+ grub_efi_mac_address_t client_mac_address;
+ grub_efi_ipv4_address_t server_address;
+ grub_efi_ipv4_address_t router_address;
+ grub_efi_ipv4_address_t subnet_mask;
+ grub_efi_uint32_t lease_time;
+ grub_efi_dhcp4_packet_t *reply_packet;
+};
+
+typedef struct grub_efi_dhcp4_mode_data grub_efi_dhcp4_mode_data_t;
+
+struct grub_efi_dhcp4_protocol {
+ grub_efi_status_t (__grub_efi_api *get_mode_data) (grub_efi_dhcp4_protocol_t *this,
+ grub_efi_dhcp4_mode_data_t *dhcp4_mode_data);
+ grub_efi_status_t (__grub_efi_api *configure) (grub_efi_dhcp4_protocol_t *this,
+ grub_efi_dhcp4_config_data_t *dhcp4_cfg_data);
+ grub_efi_status_t (__grub_efi_api *start) (grub_efi_dhcp4_protocol_t *this,
+ grub_efi_event_t completion_event);
+ grub_efi_status_t (__grub_efi_api *renew_rebind) (grub_efi_dhcp4_protocol_t *this,
+ grub_efi_boolean_t rebind_request,
+ grub_efi_event_t completion_event);
+ grub_efi_status_t (__grub_efi_api *release) (grub_efi_dhcp4_protocol_t *this);
+ grub_efi_status_t (__grub_efi_api *stop) (grub_efi_dhcp4_protocol_t *this);
+ grub_efi_status_t (__grub_efi_api *build) (grub_efi_dhcp4_protocol_t *this,
+ grub_efi_dhcp4_packet_t *seed_packet,
+ grub_efi_uint32_t delete_count,
+ grub_efi_uint8_t *delete_list,
+ grub_efi_uint32_t append_count,
+ grub_efi_dhcp4_packet_option_t *append_list[],
+ grub_efi_dhcp4_packet_t **new_packet);
+ grub_efi_status_t (__grub_efi_api *transmit_receive) (grub_efi_dhcp4_protocol_t *this,
+ grub_efi_dhcp4_transmit_receive_token_t *token);
+ grub_efi_status_t (__grub_efi_api *parse) (grub_efi_dhcp4_protocol_t *this,
+ grub_efi_dhcp4_packet_t *packet,
+ grub_efi_uint32_t *option_count,
+ grub_efi_dhcp4_packet_option_t *packet_option_list[]);
+};
+
+typedef struct grub_efi_dhcp6_protocol grub_efi_dhcp6_protocol_t;
+
+struct grub_efi_dhcp6_retransmission {
+ grub_efi_uint32_t irt;
+ grub_efi_uint32_t mrc;
+ grub_efi_uint32_t mrt;
+ grub_efi_uint32_t mrd;
+};
+
+typedef struct grub_efi_dhcp6_retransmission grub_efi_dhcp6_retransmission_t;
+
+enum grub_efi_dhcp6_event {
+ GRUB_EFI_DHCP6_SEND_SOLICIT,
+ GRUB_EFI_DHCP6_RCVD_ADVERTISE,
+ GRUB_EFI_DHCP6_SELECT_ADVERTISE,
+ GRUB_EFI_DHCP6_SEND_REQUEST,
+ GRUB_EFI_DHCP6_RCVD_REPLY,
+ GRUB_EFI_DHCP6_RCVD_RECONFIGURE,
+ GRUB_EFI_DHCP6_SEND_DECLINE,
+ GRUB_EFI_DHCP6_SEND_CONFIRM,
+ GRUB_EFI_DHCP6_SEND_RELEASE,
+ GRUB_EFI_DHCP6_SEND_RENEW,
+ GRUB_EFI_DHCP6_SEND_REBIND
+};
+
+typedef enum grub_efi_dhcp6_event grub_efi_dhcp6_event_t;
+
+struct grub_efi_dhcp6_packet_option {
+ grub_efi_uint16_t op_code;
+ grub_efi_uint16_t op_len;
+ grub_efi_uint8_t data[1];
+} GRUB_PACKED;
+
+typedef struct grub_efi_dhcp6_packet_option grub_efi_dhcp6_packet_option_t;
+
+struct grub_efi_dhcp6_header {
+ grub_efi_uint32_t transaction_id:24;
+ grub_efi_uint32_t message_type:8;
+} GRUB_PACKED;
+
+typedef struct grub_efi_dhcp6_header grub_efi_dhcp6_header_t;
+
+struct grub_efi_dhcp6_packet {
+ grub_efi_uint32_t size;
+ grub_efi_uint32_t length;
+ struct {
+ grub_efi_dhcp6_header_t header;
+ grub_efi_uint8_t option[1];
+ } dhcp6;
+} GRUB_PACKED;
+
+typedef struct grub_efi_dhcp6_packet grub_efi_dhcp6_packet_t;
+
+struct grub_efi_dhcp6_ia_address {
+ grub_efi_ipv6_address_t ip_address;
+ grub_efi_uint32_t preferred_lifetime;
+ grub_efi_uint32_t valid_lifetime;
+};
+
+typedef struct grub_efi_dhcp6_ia_address grub_efi_dhcp6_ia_address_t;
+
+enum grub_efi_dhcp6_state {
+ GRUB_EFI_DHCP6_INIT,
+ GRUB_EFI_DHCP6_SELECTING,
+ GRUB_EFI_DHCP6_REQUESTING,
+ GRUB_EFI_DHCP6_DECLINING,
+ GRUB_EFI_DHCP6_CONFIRMING,
+ GRUB_EFI_DHCP6_RELEASING,
+ GRUB_EFI_DHCP6_BOUND,
+ GRUB_EFI_DHCP6_RENEWING,
+ GRUB_EFI_DHCP6_REBINDING
+};
+
+typedef enum grub_efi_dhcp6_state grub_efi_dhcp6_state_t;
+
+#define GRUB_EFI_DHCP6_IA_TYPE_NA 3
+#define GRUB_EFI_DHCP6_IA_TYPE_TA 4
+
+struct grub_efi_dhcp6_ia_descriptor {
+ grub_efi_uint16_t type;
+ grub_efi_uint32_t ia_id;
+};
+
+typedef struct grub_efi_dhcp6_ia_descriptor grub_efi_dhcp6_ia_descriptor_t;
+
+struct grub_efi_dhcp6_ia {
+ grub_efi_dhcp6_ia_descriptor_t descriptor;
+ grub_efi_dhcp6_state_t state;
+ grub_efi_dhcp6_packet_t *reply_packet;
+ grub_efi_uint32_t ia_address_count;
+ grub_efi_dhcp6_ia_address_t ia_address[1];
+};
+
+typedef struct grub_efi_dhcp6_ia grub_efi_dhcp6_ia_t;
+
+struct grub_efi_dhcp6_duid {
+ grub_efi_uint16_t length;
+ grub_efi_uint8_t duid[1];
+};
+
+typedef struct grub_efi_dhcp6_duid grub_efi_dhcp6_duid_t;
+
+struct grub_efi_dhcp6_mode_data {
+ grub_efi_dhcp6_duid_t *client_id;
+ grub_efi_dhcp6_ia_t *ia;
+};
+
+typedef struct grub_efi_dhcp6_mode_data grub_efi_dhcp6_mode_data_t;
+
+struct grub_efi_dhcp6_config_data {
+ grub_efi_status_t (*dhcp6_callback) (grub_efi_dhcp6_protocol_t this,
+ void *context,
+ grub_efi_dhcp6_state_t current_state,
+ grub_efi_dhcp6_event_t dhcp6_event,
+ grub_efi_dhcp6_packet_t *packet,
+ grub_efi_dhcp6_packet_t **new_packet);
+ void *callback_context;
+ grub_efi_uint32_t option_count;
+ grub_efi_dhcp6_packet_option_t **option_list;
+ grub_efi_dhcp6_ia_descriptor_t ia_descriptor;
+ grub_efi_event_t ia_info_event;
+ grub_efi_boolean_t reconfigure_accept;
+ grub_efi_boolean_t rapid_commit;
+ grub_efi_dhcp6_retransmission_t *solicit_retransmission;
+};
+
+typedef struct grub_efi_dhcp6_config_data grub_efi_dhcp6_config_data_t;
+
+struct grub_efi_dhcp6_protocol {
+ grub_efi_status_t (__grub_efi_api *get_mode_data) (grub_efi_dhcp6_protocol_t *this,
+ grub_efi_dhcp6_mode_data_t *dhcp6_mode_data,
+ grub_efi_dhcp6_config_data_t *dhcp6_config_data);
+ grub_efi_status_t (__grub_efi_api *configure) (grub_efi_dhcp6_protocol_t *this,
+ grub_efi_dhcp6_config_data_t *dhcp6_cfg_data);
+ grub_efi_status_t (__grub_efi_api *start) (grub_efi_dhcp6_protocol_t *this);
+ grub_efi_status_t (__grub_efi_api *info_request) (grub_efi_dhcp6_protocol_t *this,
+ grub_efi_boolean_t send_client_id,
+ grub_efi_dhcp6_packet_option_t *option_request,
+ grub_efi_uint32_t option_count,
+ grub_efi_dhcp6_packet_option_t *option_list[],
+ grub_efi_dhcp6_retransmission_t *retransmission,
+ grub_efi_event_t timeout_event,
+ grub_efi_status_t (__grub_efi_api *reply_callback) (grub_efi_dhcp6_protocol_t *this,
+ void *context,
+ grub_efi_dhcp6_packet_t *packet),
+ void *callback_context);
+ grub_efi_status_t (__grub_efi_api *renew_rebind) (grub_efi_dhcp6_protocol_t *this,
+ grub_efi_boolean_t rebind_request);
+ grub_efi_status_t (__grub_efi_api *decline) (grub_efi_dhcp6_protocol_t *this,
+ grub_efi_uint32_t address_count,
+ grub_efi_ipv6_address_t *addresses);
+ grub_efi_status_t (__grub_efi_api *release) (grub_efi_dhcp6_protocol_t *this,
+ grub_efi_uint32_t address_count,
+ grub_efi_ipv6_address_t *addresses);
+ grub_efi_status_t (__grub_efi_api *stop) (grub_efi_dhcp6_protocol_t *this);
+ grub_efi_status_t (__grub_efi_api *parse) (grub_efi_dhcp6_protocol_t *this,
+ grub_efi_dhcp6_packet_t *packet,
+ grub_efi_uint32_t *option_count,
+ grub_efi_dhcp6_packet_option_t *packet_option_list[]);
+};
+
+#endif /* ! GRUB_EFI_DHCP_HEADER */
diff --git a/include/grub/efi/http.h b/include/grub/efi/http.h
new file mode 100644
index 000000000..79592cdb7
--- /dev/null
+++ b/include/grub/efi/http.h
@@ -0,0 +1,215 @@
+/*
+ * GRUB -- GRand Unified Bootloader
+ * Copyright (C) 2006,2007,2008 Free Software Foundation, Inc.
+ *
+ * GRUB is free software: you can redistribute it and/or modify
+ * it under the terms of the GNU General Public License as published by
+ * the Free Software Foundation, either version 3 of the License, or
+ * (at your option) any later version.
+ *
+ * GRUB is distributed in the hope that it will be useful,
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
+ * GNU General Public License for more details.
+ *
+ * You should have received a copy of the GNU General Public License
+ * along with GRUB. If not, see <http://www.gnu.org/licenses/>.
+ */
+
+#ifndef GRUB_EFI_HTTP_HEADER
+#define GRUB_EFI_HTTP_HEADER 1
+
+#include <grub/symbol.h>
+#include <grub/net.h>
+#include <grub/efi/api.h>
+
+#define GRUB_EFI_HTTP_SERVICE_BINDING_PROTOCOL_GUID \
+ { 0xbdc8e6af, 0xd9bc, 0x4379, \
+ { 0xa7, 0x2a, 0xe0, 0xc4, 0xe7, 0x5d, 0xae, 0x1c } \
+ }
+
+#define GRUB_EFI_HTTP_PROTOCOL_GUID \
+ { 0x7A59B29B, 0x910B, 0x4171, \
+ { 0x82, 0x42, 0xA8, 0x5A, 0x0D, 0xF2, 0x5B, 0x5B } \
+ }
+
+#define EFIHTTP_WAIT_TIME 10000 // 10000ms = 10s
+#define EFIHTTP_RX_BUF_LEN 10240
+
+//******************************************
+// Protocol Interface Structure
+//******************************************
+struct grub_efi_http;
+
+//******************************************
+// EFI_HTTP_VERSION
+//******************************************
+typedef enum {
+ GRUB_EFI_HTTPVERSION10,
+ GRUB_EFI_HTTPVERSION11,
+ GRUB_EFI_HTTPVERSIONUNSUPPORTED
+} grub_efi_http_version_t;
+
+//******************************************
+// EFI_HTTPv4_ACCESS_POINT
+//******************************************
+typedef struct {
+ grub_efi_boolean_t use_default_address;
+ grub_efi_ipv4_address_t local_address;
+ grub_efi_ipv4_address_t local_subnet;
+ grub_efi_uint16_t local_port;
+} grub_efi_httpv4_access_point_t;
+
+//******************************************
+// EFI_HTTPv6_ACCESS_POINT
+//******************************************
+typedef struct {
+ grub_efi_ipv6_address_t local_address;
+ grub_efi_uint16_t local_port;
+} grub_efi_httpv6_access_point_t;
+
+//******************************************
+// EFI_HTTP_CONFIG_DATA
+//******************************************
+typedef struct {
+ grub_efi_http_version_t http_version;
+ grub_efi_uint32_t timeout_millisec;
+ grub_efi_boolean_t local_address_is_ipv6;
+ union {
+ grub_efi_httpv4_access_point_t *ipv4_node;
+ grub_efi_httpv6_access_point_t *ipv6_node;
+ } access_point;
+} grub_efi_http_config_data_t;
+
+//******************************************
+// EFI_HTTP_METHOD
+//******************************************
+typedef enum {
+ GRUB_EFI_HTTPMETHODGET,
+ GRUB_EFI_HTTPMETHODPOST,
+ GRUB_EFI_HTTPMETHODPATCH,
+ GRUB_EFI_HTTPMETHODOPTIONS,
+ GRUB_EFI_HTTPMETHODCONNECT,
+ GRUB_EFI_HTTPMETHODHEAD,
+ GRUB_EFI_HTTPMETHODPUT,
+ GRUB_EFI_HTTPMETHODDELETE,
+ GRUB_EFI_HTTPMETHODTRACE,
+} grub_efi_http_method_t;
+
+//******************************************
+// EFI_HTTP_REQUEST_DATA
+//******************************************
+typedef struct {
+ grub_efi_http_method_t method;
+ grub_efi_char16_t *url;
+} grub_efi_http_request_data_t;
+
+typedef enum {
+ GRUB_EFI_HTTP_STATUS_UNSUPPORTED_STATUS = 0,
+ GRUB_EFI_HTTP_STATUS_100_CONTINUE,
+ GRUB_EFI_HTTP_STATUS_101_SWITCHING_PROTOCOLS,
+ GRUB_EFI_HTTP_STATUS_200_OK,
+ GRUB_EFI_HTTP_STATUS_201_CREATED,
+ GRUB_EFI_HTTP_STATUS_202_ACCEPTED,
+ GRUB_EFI_HTTP_STATUS_203_NON_AUTHORITATIVE_INFORMATION,
+ GRUB_EFI_HTTP_STATUS_204_NO_CONTENT,
+ GRUB_EFI_HTTP_STATUS_205_RESET_CONTENT,
+ GRUB_EFI_HTTP_STATUS_206_PARTIAL_CONTENT,
+ GRUB_EFI_HTTP_STATUS_300_MULTIPLE_CHIOCES,
+ GRUB_EFI_HTTP_STATUS_301_MOVED_PERMANENTLY,
+ GRUB_EFI_HTTP_STATUS_302_FOUND,
+ GRUB_EFI_HTTP_STATUS_303_SEE_OTHER,
+ GRUB_EFI_HTTP_STATUS_304_NOT_MODIFIED,
+ GRUB_EFI_HTTP_STATUS_305_USE_PROXY,
+ GRUB_EFI_HTTP_STATUS_307_TEMPORARY_REDIRECT,
+ GRUB_EFI_HTTP_STATUS_400_BAD_REQUEST,
+ GRUB_EFI_HTTP_STATUS_401_UNAUTHORIZED,
+ GRUB_EFI_HTTP_STATUS_402_PAYMENT_REQUIRED,
+ GRUB_EFI_HTTP_STATUS_403_FORBIDDEN,
+ GRUB_EFI_HTTP_STATUS_404_NOT_FOUND,
+ GRUB_EFI_HTTP_STATUS_405_METHOD_NOT_ALLOWED,
+ GRUB_EFI_HTTP_STATUS_406_NOT_ACCEPTABLE,
+ GRUB_EFI_HTTP_STATUS_407_PROXY_AUTHENTICATION_REQUIRED,
+ GRUB_EFI_HTTP_STATUS_408_REQUEST_TIME_OUT,
+ GRUB_EFI_HTTP_STATUS_409_CONFLICT,
+ GRUB_EFI_HTTP_STATUS_410_GONE,
+ GRUB_EFI_HTTP_STATUS_411_LENGTH_REQUIRED,
+ GRUB_EFI_HTTP_STATUS_412_PRECONDITION_FAILED,
+ GRUB_EFI_HTTP_STATUS_413_REQUEST_ENTITY_TOO_LARGE,
+ GRUB_EFI_HTTP_STATUS_414_REQUEST_URI_TOO_LARGE,
+ GRUB_EFI_HTTP_STATUS_415_UNSUPPORTED_MEDIA_TYPE,
+ GRUB_EFI_HTTP_STATUS_416_REQUESTED_RANGE_NOT_SATISFIED,
+ GRUB_EFI_HTTP_STATUS_417_EXPECTATION_FAILED,
+ GRUB_EFI_HTTP_STATUS_500_INTERNAL_SERVER_ERROR,
+ GRUB_EFI_HTTP_STATUS_501_NOT_IMPLEMENTED,
+ GRUB_EFI_HTTP_STATUS_502_BAD_GATEWAY,
+ GRUB_EFI_HTTP_STATUS_503_SERVICE_UNAVAILABLE,
+ GRUB_EFI_HTTP_STATUS_504_GATEWAY_TIME_OUT,
+ GRUB_EFI_HTTP_STATUS_505_HTTP_VERSION_NOT_SUPPORTED
+} grub_efi_http_status_code_t;
+
+//******************************************
+// EFI_HTTP_RESPONSE_DATA
+//******************************************
+typedef struct {
+ grub_efi_http_status_code_t status_code;
+} grub_efi_http_response_data_t;
+
+//******************************************
+// EFI_HTTP_HEADER
+//******************************************
+typedef struct {
+ grub_efi_char8_t *field_name;
+ grub_efi_char8_t *field_value;
+} grub_efi_http_header_t;
+
+//******************************************
+// EFI_HTTP_MESSAGE
+//******************************************
+typedef struct {
+ union {
+ grub_efi_http_request_data_t *request;
+ grub_efi_http_response_data_t *response;
+ } data;
+ grub_efi_uint32_t header_count;
+ grub_efi_http_header_t *headers;
+ grub_efi_uint32_t body_length;
+ void *body;
+} grub_efi_http_message_t;
+
+//******************************************
+// EFI_HTTP_TOKEN
+//******************************************
+typedef struct {
+ grub_efi_event_t event;
+ grub_efi_status_t status;
+ grub_efi_http_message_t *message;
+} grub_efi_http_token_t;
+
+struct grub_efi_http {
+ grub_efi_status_t
+ (__grub_efi_api *get_mode_data) (struct grub_efi_http *this,
+ grub_efi_http_config_data_t *http_config_data);
+
+ grub_efi_status_t
+ (__grub_efi_api *configure) (struct grub_efi_http *this,
+ grub_efi_http_config_data_t *http_config_data);
+
+ grub_efi_status_t
+ (__grub_efi_api *request) (struct grub_efi_http *this,
+ grub_efi_http_token_t *token);
+
+ grub_efi_status_t
+ (__grub_efi_api *cancel) (struct grub_efi_http *this,
+ grub_efi_http_token_t *token);
+
+ grub_efi_status_t
+ (__grub_efi_api *response) (struct grub_efi_http *this,
+ grub_efi_http_token_t *token);
+
+ grub_efi_status_t
+ (__grub_efi_api *poll) (struct grub_efi_http *this);
+};
+typedef struct grub_efi_http grub_efi_http_t;
+
+#endif /* !GRUB_EFI_HTTP_HEADER */
diff --git a/include/grub/net/efi.h b/include/grub/net/efi.h
new file mode 100644
index 000000000..de90d223e
--- /dev/null
+++ b/include/grub/net/efi.h
@@ -0,0 +1,144 @@
+#ifndef GRUB_NET_EFI_HEADER
+#define GRUB_NET_EFI_HEADER 1
+
+#include <grub/efi/api.h>
+#include <grub/efi/http.h>
+#include <grub/efi/dhcp.h>
+#include <grub/command.h>
+
+typedef struct grub_efi_net_interface grub_efi_net_interface_t;
+typedef struct grub_efi_net_ip_config grub_efi_net_ip_config_t;
+typedef union grub_efi_net_ip_address grub_efi_net_ip_address_t;
+typedef struct grub_efi_net_ip_manual_address grub_efi_net_ip_manual_address_t;
+
+struct grub_efi_net_interface
+{
+ char *name;
+ int prefer_ip6;
+ struct grub_efi_net_device *dev;
+ struct grub_efi_net_io *io;
+ grub_efi_net_ip_config_t *ip_config;
+ int io_type;
+ struct grub_efi_net_interface *next;
+};
+
+#define efi_net_interface_get_hw_address(inf) inf->ip_config->get_hw_address (inf->dev)
+#define efi_net_interface_get_address(inf) inf->ip_config->get_address (inf->dev)
+#define efi_net_interface_get_route_table(inf) inf->ip_config->get_route_table (inf->dev)
+#define efi_net_interface_set_address(inf, addr, with_subnet) inf->ip_config->set_address (inf->dev, addr, with_subnet)
+#define efi_net_interface_set_gateway(inf, addr) inf->ip_config->set_gateway (inf->dev, addr)
+#define efi_net_interface_set_dns(inf, addr) inf->ip_config->set_dns (inf->dev, addr)
+
+struct grub_efi_net_ip_config
+{
+ char * (*get_hw_address) (struct grub_efi_net_device *dev);
+ char * (*get_address) (struct grub_efi_net_device *dev);
+ char ** (*get_route_table) (struct grub_efi_net_device *dev);
+ grub_efi_net_interface_t * (*best_interface) (struct grub_efi_net_device *dev, grub_efi_net_ip_address_t *address);
+ int (*set_address) (struct grub_efi_net_device *dev, grub_efi_net_ip_manual_address_t *net_ip, int with_subnet);
+ int (*set_gateway) (struct grub_efi_net_device *dev, grub_efi_net_ip_address_t *address);
+ int (*set_dns) (struct grub_efi_net_device *dev, grub_efi_net_ip_address_t *dns);
+};
+
+union grub_efi_net_ip_address
+{
+ grub_efi_ipv4_address_t ip4;
+ grub_efi_ipv6_address_t ip6;
+};
+
+struct grub_efi_net_ip_manual_address
+{
+ int is_ip6;
+ union
+ {
+ grub_efi_ip4_config2_manual_address_t ip4;
+ grub_efi_ip6_config_manual_address_t ip6;
+ };
+};
+
+struct grub_efi_net_device
+{
+ grub_efi_handle_t handle;
+ grub_efi_ip4_config2_protocol_t *ip4_config;
+ grub_efi_ip6_config_protocol_t *ip6_config;
+ grub_efi_handle_t http_handle;
+ grub_efi_http_t *http;
+ grub_efi_handle_t ip4_pxe_handle;
+ grub_efi_pxe_t *ip4_pxe;
+ grub_efi_handle_t ip6_pxe_handle;
+ grub_efi_pxe_t *ip6_pxe;
+ grub_efi_handle_t dhcp4_handle;
+ grub_efi_dhcp4_protocol_t *dhcp4;
+ grub_efi_handle_t dhcp6_handle;
+ grub_efi_dhcp6_protocol_t *dhcp6;
+ char *card_name;
+ grub_efi_net_interface_t *net_interfaces;
+ struct grub_efi_net_device *next;
+};
+
+struct grub_efi_net_io
+{
+ void (*configure) (struct grub_efi_net_device *dev, int prefer_ip6);
+ grub_err_t (*open) (struct grub_efi_net_device *dev,
+ int prefer_ip6,
+ grub_file_t file,
+ const char *filename,
+ int type);
+ grub_ssize_t (*read) (struct grub_efi_net_device *dev,
+ int prefer_ip6,
+ grub_file_t file,
+ char *buf,
+ grub_size_t len);
+ grub_err_t (*close) (struct grub_efi_net_device *dev,
+ int prefer_ip6,
+ grub_file_t file);
+};
+
+extern struct grub_efi_net_device *net_devices;
+
+extern struct grub_efi_net_io io_http;
+extern struct grub_efi_net_io io_pxe;
+
+extern grub_efi_net_ip_config_t *efi_net_ip4_config;
+extern grub_efi_net_ip_config_t *efi_net_ip6_config;
+
+char *
+grub_efi_ip4_address_to_string (grub_efi_ipv4_address_t *address);
+
+char *
+grub_efi_ip6_address_to_string (grub_efi_pxe_ipv6_address_t *address);
+
+char *
+grub_efi_hw_address_to_string (grub_efi_uint32_t hw_address_size, grub_efi_mac_address_t hw_address);
+
+int
+grub_efi_string_to_ip4_address (const char *val, grub_efi_ipv4_address_t *address, const char **rest);
+
+int
+grub_efi_string_to_ip6_address (const char *val, grub_efi_ipv6_address_t *address, const char **rest);
+
+char *
+grub_efi_ip6_interface_name (struct grub_efi_net_device *dev);
+
+char *
+grub_efi_ip4_interface_name (struct grub_efi_net_device *dev);
+
+grub_efi_net_interface_t *
+grub_efi_net_create_interface (struct grub_efi_net_device *dev,
+ const char *interface_name,
+ grub_efi_net_ip_manual_address_t *net_ip,
+ int has_subnet);
+
+int grub_efi_net_fs_init (void);
+void grub_efi_net_fs_fini (void);
+int grub_efi_net_boot_from_https (void);
+int grub_efi_net_boot_from_opa (void);
+
+extern grub_command_func_t grub_efi_net_list_routes;
+extern grub_command_func_t grub_efi_net_list_cards;
+extern grub_command_func_t grub_efi_net_list_addrs;
+extern grub_command_func_t grub_efi_net_add_addr;
+extern grub_command_func_t grub_efi_net_bootp;
+extern grub_command_func_t grub_efi_net_bootp6;
+
+#endif /* ! GRUB_NET_EFI_HEADER */
--
2.41.0
Loading...
马建仓 AI 助手
尝试更多
代码解读
代码找茬
代码优化
1
https://gitee.com/jackzhao166/grub2.git
git@gitee.com:jackzhao166/grub2.git
jackzhao166
grub2
grub2
master

搜索帮助