bsd.c 53 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646164716481649165016511652165316541655165616571658165916601661166216631664166516661667166816691670167116721673167416751676167716781679168016811682168316841685168616871688168916901691169216931694169516961697169816991700170117021703170417051706170717081709171017111712171317141715171617171718171917201721172217231724172517261727172817291730173117321733173417351736173717381739174017411742174317441745174617471748174917501751175217531754175517561757175817591760176117621763176417651766176717681769177017711772177317741775177617771778177917801781178217831784178517861787178817891790179117921793179417951796179717981799180018011802180318041805180618071808180918101811181218131814181518161817181818191820182118221823182418251826182718281829183018311832183318341835183618371838183918401841184218431844184518461847184818491850185118521853185418551856185718581859186018611862186318641865186618671868186918701871187218731874187518761877187818791880188118821883188418851886188718881889189018911892189318941895189618971898189919001901190219031904190519061907190819091910191119121913191419151916191719181919192019211922192319241925192619271928192919301931193219331934193519361937193819391940194119421943194419451946194719481949195019511952195319541955195619571958195919601961196219631964196519661967196819691970197119721973197419751976197719781979198019811982198319841985198619871988198919901991199219931994199519961997199819992000200120022003200420052006200720082009201020112012201320142015201620172018201920202021202220232024202520262027202820292030203120322033203420352036203720382039204020412042204320442045204620472048204920502051205220532054205520562057205820592060206120622063206420652066206720682069207020712072207320742075207620772078207920802081208220832084208520862087208820892090209120922093209420952096209720982099210021012102210321042105210621072108210921102111211221132114211521162117211821192120212121222123212421252126212721282129213021312132213321342135213621372138213921402141214221432144214521462147214821492150215121522153215421552156215721582159216021612162216321642165216621672168216921702171217221732174217521762177217821792180218121822183218421852186218721882189219021912192
  1. /*
  2. * GRUB -- GRand Unified Bootloader
  3. * Copyright (C) 2008,2009,2010 Free Software Foundation, Inc.
  4. *
  5. * GRUB is free software: you can redistribute it and/or modify
  6. * it under the terms of the GNU General Public License as published by
  7. * the Free Software Foundation, either version 3 of the License, or
  8. * (at your option) any later version.
  9. *
  10. * GRUB is distributed in the hope that it will be useful,
  11. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  12. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  13. * GNU General Public License for more details.
  14. *
  15. * You should have received a copy of the GNU General Public License
  16. * along with GRUB. If not, see <http://www.gnu.org/licenses/>.
  17. */
  18. #include <grub/loader.h>
  19. #include <grub/i386/bsd.h>
  20. #include <grub/i386/cpuid.h>
  21. #include <grub/memory.h>
  22. #include <grub/i386/memory.h>
  23. #include <grub/file.h>
  24. #include <grub/err.h>
  25. #include <grub/dl.h>
  26. #include <grub/mm.h>
  27. #include <grub/elfload.h>
  28. #include <grub/env.h>
  29. #include <grub/misc.h>
  30. #include <grub/aout.h>
  31. #include <grub/command.h>
  32. #include <grub/extcmd.h>
  33. #include <grub/i18n.h>
  34. #include <grub/ns8250.h>
  35. #include <grub/bsdlabel.h>
  36. #include <grub/crypto.h>
  37. #include <grub/safemath.h>
  38. #include <grub/verify.h>
  39. #ifdef GRUB_MACHINE_PCBIOS
  40. #include <grub/machine/int.h>
  41. #endif
  42. GRUB_MOD_LICENSE ("GPLv3+");
  43. #include <grub/video.h>
  44. #ifdef GRUB_MACHINE_PCBIOS
  45. #include <grub/machine/biosnum.h>
  46. #endif
  47. #ifdef GRUB_MACHINE_EFI
  48. #include <grub/efi/efi.h>
  49. #define NETBSD_DEFAULT_VIDEO_MODE "800x600"
  50. #else
  51. #define NETBSD_DEFAULT_VIDEO_MODE "text"
  52. #include <grub/i386/pc/vbe.h>
  53. #endif
  54. #include <grub/video.h>
  55. #include <grub/disk.h>
  56. #include <grub/device.h>
  57. #include <grub/partition.h>
  58. #include <grub/relocator.h>
  59. #include <grub/i386/relocator.h>
  60. #define ALIGN_DWORD(a) ALIGN_UP (a, 4)
  61. #define ALIGN_QWORD(a) ALIGN_UP (a, 8)
  62. #define ALIGN_VAR(a) ((is_64bit) ? (ALIGN_QWORD(a)) : (ALIGN_DWORD(a)))
  63. #define ALIGN_PAGE(a) ALIGN_UP (a, 4096)
  64. static int kernel_type = KERNEL_TYPE_NONE;
  65. static grub_dl_t my_mod;
  66. static grub_addr_t entry, entry_hi, kern_start, kern_end;
  67. static void *kern_chunk_src;
  68. static grub_uint32_t bootflags;
  69. static int is_elf_kernel, is_64bit;
  70. static grub_uint32_t openbsd_root;
  71. static struct grub_relocator *relocator = NULL;
  72. static struct grub_openbsd_ramdisk_descriptor openbsd_ramdisk;
  73. struct bsd_tag
  74. {
  75. struct bsd_tag *next;
  76. grub_size_t len;
  77. grub_uint32_t type;
  78. union {
  79. grub_uint8_t a;
  80. grub_uint16_t b;
  81. grub_uint32_t c;
  82. } data[0];
  83. };
  84. static struct bsd_tag *tags, *tags_last;
  85. struct netbsd_module
  86. {
  87. struct netbsd_module *next;
  88. struct grub_netbsd_btinfo_module mod;
  89. };
  90. static struct netbsd_module *netbsd_mods, *netbsd_mods_last;
  91. static const struct grub_arg_option freebsd_opts[] =
  92. {
  93. {"dual", 'D', 0, N_("Display output on all consoles."), 0, 0},
  94. {"serial", 'h', 0, N_("Use serial console."), 0, 0},
  95. {"askname", 'a', 0, N_("Ask for file name to reboot from."), 0, 0},
  96. {"cdrom", 'C', 0, N_("Use CD-ROM as root."), 0, 0},
  97. {"config", 'c', 0, N_("Invoke user configuration routing."), 0, 0},
  98. {"kdb", 'd', 0, N_("Enter in KDB on boot."), 0, 0},
  99. {"gdb", 'g', 0, N_("Use GDB remote debugger instead of DDB."), 0, 0},
  100. {"mute", 'm', 0, N_("Disable all boot output."), 0, 0},
  101. {"nointr", 'n', 0, "", 0, 0},
  102. {"pause", 'p', 0, N_("Wait for keypress after every line of output."), 0, 0},
  103. {"quiet", 'q', 0, "", 0, 0},
  104. {"dfltroot", 'r', 0, N_("Use compiled-in root device."), 0, 0},
  105. {"single", 's', 0, N_("Boot into single mode."), 0, 0},
  106. {"verbose", 'v', 0, N_("Boot with verbose messages."), 0, 0},
  107. {0, 0, 0, 0, 0, 0}
  108. };
  109. static const grub_uint32_t freebsd_flags[] =
  110. {
  111. FREEBSD_RB_DUAL, FREEBSD_RB_SERIAL, FREEBSD_RB_ASKNAME,
  112. FREEBSD_RB_CDROM, FREEBSD_RB_CONFIG, FREEBSD_RB_KDB,
  113. FREEBSD_RB_GDB, FREEBSD_RB_MUTE, FREEBSD_RB_NOINTR,
  114. FREEBSD_RB_PAUSE, FREEBSD_RB_QUIET, FREEBSD_RB_DFLTROOT,
  115. FREEBSD_RB_SINGLE, FREEBSD_RB_VERBOSE, 0
  116. };
  117. static const struct grub_arg_option openbsd_opts[] =
  118. {
  119. {"askname", 'a', 0, N_("Ask for file name to reboot from."), 0, 0},
  120. {"halt", 'b', 0, N_("Don't reboot, just halt."), 0, 0},
  121. {"config", 'c', 0, N_("Change configured devices."), 0, 0},
  122. {"single", 's', 0, N_("Boot into single mode."), 0, 0},
  123. {"kdb", 'd', 0, N_("Enter in KDB on boot."), 0, 0},
  124. {"root", 'r', 0, N_("Set root device."), "wdXY", ARG_TYPE_STRING},
  125. {"serial", 'h', GRUB_ARG_OPTION_OPTIONAL,
  126. N_("Use serial console."),
  127. /* TRANSLATORS: "com" is static and not to be translated. It refers to
  128. serial ports e.g. com1.
  129. */
  130. N_("comUNIT[,SPEED]"), ARG_TYPE_STRING},
  131. {0, 0, 0, 0, 0, 0}
  132. };
  133. static const grub_uint32_t openbsd_flags[] =
  134. {
  135. OPENBSD_RB_ASKNAME, OPENBSD_RB_HALT, OPENBSD_RB_CONFIG,
  136. OPENBSD_RB_SINGLE, OPENBSD_RB_KDB, 0
  137. };
  138. #define OPENBSD_ROOT_ARG (ARRAY_SIZE (openbsd_flags) - 1)
  139. #define OPENBSD_SERIAL_ARG (ARRAY_SIZE (openbsd_flags))
  140. static const struct grub_arg_option netbsd_opts[] =
  141. {
  142. {"no-smp", '1', 0, N_("Disable SMP."), 0, 0},
  143. {"no-acpi", '2', 0, N_("Disable ACPI."), 0, 0},
  144. {"askname", 'a', 0, N_("Ask for file name to reboot from."), 0, 0},
  145. {"halt", 'b', 0, N_("Don't reboot, just halt."), 0, 0},
  146. {"config", 'c', 0, N_("Change configured devices."), 0, 0},
  147. {"kdb", 'd', 0, N_("Enter in KDB on boot."), 0, 0},
  148. {"miniroot", 'm', 0, "", 0, 0},
  149. {"quiet", 'q', 0, N_("Don't display boot diagnostic messages."), 0, 0},
  150. {"single", 's', 0, N_("Boot into single mode."), 0, 0},
  151. {"verbose", 'v', 0, N_("Boot with verbose messages."), 0, 0},
  152. {"debug", 'x', 0, N_("Boot with debug messages."), 0, 0},
  153. {"silent", 'z', 0, N_("Suppress normal output (warnings remain)."), 0, 0},
  154. {"root", 'r', 0, N_("Set root device."), N_("DEVICE"), ARG_TYPE_STRING},
  155. {"serial", 'h', GRUB_ARG_OPTION_OPTIONAL,
  156. N_("Use serial console."),
  157. /* TRANSLATORS: "com" is static and not to be translated. It refers to
  158. serial ports e.g. com1.
  159. */
  160. N_("[ADDR|comUNIT][,SPEED]"), ARG_TYPE_STRING},
  161. {0, 0, 0, 0, 0, 0}
  162. };
  163. static const grub_uint32_t netbsd_flags[] =
  164. {
  165. NETBSD_AB_NOSMP, NETBSD_AB_NOACPI, NETBSD_RB_ASKNAME,
  166. NETBSD_RB_HALT, NETBSD_RB_USERCONFIG, NETBSD_RB_KDB,
  167. NETBSD_RB_MINIROOT, NETBSD_AB_QUIET, NETBSD_RB_SINGLE,
  168. NETBSD_AB_VERBOSE, NETBSD_AB_DEBUG, NETBSD_AB_SILENT, 0
  169. };
  170. #define NETBSD_ROOT_ARG (ARRAY_SIZE (netbsd_flags) - 1)
  171. #define NETBSD_SERIAL_ARG (ARRAY_SIZE (netbsd_flags))
  172. static void
  173. grub_bsd_get_device (grub_uint32_t * biosdev,
  174. grub_uint32_t * unit,
  175. grub_uint32_t * slice, grub_uint32_t * part)
  176. {
  177. grub_device_t dev;
  178. #ifdef GRUB_MACHINE_PCBIOS
  179. *biosdev = grub_get_root_biosnumber () & 0xff;
  180. #else
  181. *biosdev = 0xff;
  182. #endif
  183. *unit = (*biosdev & 0x7f);
  184. *slice = 0xff;
  185. *part = 0xff;
  186. dev = grub_device_open (0);
  187. if (dev && dev->disk && dev->disk->partition)
  188. {
  189. if (dev->disk->partition->parent)
  190. {
  191. *part = dev->disk->partition->number;
  192. *slice = dev->disk->partition->parent->number + 1;
  193. }
  194. else
  195. *slice = dev->disk->partition->number + 1;
  196. }
  197. if (dev)
  198. grub_device_close (dev);
  199. }
  200. static grub_err_t
  201. grub_bsd_add_meta_ptr (grub_uint32_t type, void **ptr, grub_uint32_t len)
  202. {
  203. struct bsd_tag *newtag;
  204. newtag = grub_malloc (len + sizeof (struct bsd_tag));
  205. if (!newtag)
  206. return grub_errno;
  207. newtag->len = len;
  208. newtag->type = type;
  209. newtag->next = NULL;
  210. *ptr = newtag->data;
  211. if (kernel_type == KERNEL_TYPE_FREEBSD
  212. && type == (FREEBSD_MODINFO_METADATA | FREEBSD_MODINFOMD_SMAP))
  213. {
  214. struct bsd_tag *p;
  215. for (p = tags;
  216. p && p->type != (FREEBSD_MODINFO_METADATA
  217. | FREEBSD_MODINFOMD_KERNEND);
  218. p = p->next);
  219. if (p)
  220. {
  221. newtag->next = p->next;
  222. p->next = newtag;
  223. if (newtag->next == NULL)
  224. tags_last = newtag;
  225. return GRUB_ERR_NONE;
  226. }
  227. }
  228. if (tags_last)
  229. tags_last->next = newtag;
  230. else
  231. tags = newtag;
  232. tags_last = newtag;
  233. return GRUB_ERR_NONE;
  234. }
  235. grub_err_t
  236. grub_bsd_add_meta (grub_uint32_t type, const void *data, grub_uint32_t len)
  237. {
  238. grub_err_t err;
  239. void *ptr = NULL;
  240. err = grub_bsd_add_meta_ptr (type, &ptr, len);
  241. if (err)
  242. return err;
  243. if (len)
  244. grub_memcpy (ptr, data, len);
  245. return GRUB_ERR_NONE;
  246. }
  247. struct grub_e820_mmap
  248. {
  249. grub_uint64_t addr;
  250. grub_uint64_t size;
  251. grub_uint32_t type;
  252. } GRUB_PACKED;
  253. #define GRUB_E820_RAM 1
  254. #define GRUB_E820_RESERVED 2
  255. #define GRUB_E820_ACPI 3
  256. #define GRUB_E820_NVS 4
  257. #define GRUB_E820_BADRAM 5
  258. #define GRUB_E820_COREBOOT_TABLES 0x10
  259. /* Context for generate_e820_mmap. */
  260. struct generate_e820_mmap_ctx
  261. {
  262. int count;
  263. struct grub_e820_mmap *mmap;
  264. struct grub_e820_mmap prev, cur;
  265. };
  266. /* Helper for generate_e820_mmap. */
  267. static int
  268. generate_e820_mmap_iter (grub_uint64_t addr, grub_uint64_t size,
  269. grub_memory_type_t type, void *data)
  270. {
  271. struct generate_e820_mmap_ctx *ctx = data;
  272. ctx->cur.addr = addr;
  273. ctx->cur.size = size;
  274. if (type == GRUB_MEMORY_COREBOOT_TABLES
  275. && addr == 0)
  276. /* Nowadays the tables at 0 don't contain anything important but
  277. *BSD needs the memory at 0 for own needs.
  278. */
  279. type = GRUB_E820_RAM;
  280. ctx->cur.type = type;
  281. /* Merge regions if possible. */
  282. if (ctx->count && ctx->cur.type == ctx->prev.type
  283. && ctx->cur.addr == ctx->prev.addr + ctx->prev.size)
  284. {
  285. ctx->prev.size += ctx->cur.size;
  286. if (ctx->mmap)
  287. ctx->mmap[-1] = ctx->prev;
  288. }
  289. else
  290. {
  291. if (ctx->mmap)
  292. *ctx->mmap++ = ctx->cur;
  293. ctx->prev = ctx->cur;
  294. ctx->count++;
  295. }
  296. if (kernel_type == KERNEL_TYPE_OPENBSD && ctx->prev.addr < 0x100000
  297. && ctx->prev.addr + ctx->prev.size > 0x100000)
  298. {
  299. ctx->cur.addr = 0x100000;
  300. ctx->cur.size = ctx->prev.addr + ctx->prev.size - 0x100000;
  301. ctx->cur.type = ctx->prev.type;
  302. ctx->prev.size = 0x100000 - ctx->prev.addr;
  303. if (ctx->mmap)
  304. {
  305. ctx->mmap[-1] = ctx->prev;
  306. ctx->mmap[0] = ctx->cur;
  307. ctx->mmap++;
  308. }
  309. ctx->prev = ctx->cur;
  310. ctx->count++;
  311. }
  312. return 0;
  313. }
  314. static void
  315. generate_e820_mmap (grub_size_t *len, grub_size_t *cnt, void *buf)
  316. {
  317. struct generate_e820_mmap_ctx ctx = {
  318. .count = 0,
  319. .mmap = buf
  320. };
  321. grub_mmap_iterate (generate_e820_mmap_iter, &ctx);
  322. if (len)
  323. *len = ctx.count * sizeof (struct grub_e820_mmap);
  324. *cnt = ctx.count;
  325. return;
  326. }
  327. static grub_err_t
  328. grub_bsd_add_mmap (void)
  329. {
  330. grub_size_t len, cnt;
  331. void *buf = NULL, *buf0;
  332. generate_e820_mmap (&len, &cnt, buf);
  333. if (kernel_type == KERNEL_TYPE_NETBSD)
  334. len += sizeof (grub_uint32_t);
  335. if (kernel_type == KERNEL_TYPE_OPENBSD)
  336. len += sizeof (struct grub_e820_mmap);
  337. buf = grub_malloc (len);
  338. if (!buf)
  339. return grub_errno;
  340. buf0 = buf;
  341. if (kernel_type == KERNEL_TYPE_NETBSD)
  342. {
  343. *(grub_uint32_t *) buf = cnt;
  344. buf = ((grub_uint32_t *) buf + 1);
  345. }
  346. generate_e820_mmap (NULL, &cnt, buf);
  347. if (kernel_type == KERNEL_TYPE_OPENBSD)
  348. grub_memset ((grub_uint8_t *) buf + len - sizeof (struct grub_e820_mmap), 0,
  349. sizeof (struct grub_e820_mmap));
  350. grub_dprintf ("bsd", "%u entries in smap\n", (unsigned) cnt);
  351. if (kernel_type == KERNEL_TYPE_NETBSD)
  352. grub_bsd_add_meta (NETBSD_BTINFO_MEMMAP, buf0, len);
  353. else if (kernel_type == KERNEL_TYPE_OPENBSD)
  354. grub_bsd_add_meta (OPENBSD_BOOTARG_MMAP, buf0, len);
  355. else
  356. grub_bsd_add_meta (FREEBSD_MODINFO_METADATA |
  357. FREEBSD_MODINFOMD_SMAP, buf0, len);
  358. grub_free (buf0);
  359. return grub_errno;
  360. }
  361. grub_err_t
  362. grub_freebsd_add_meta_module (const char *filename, const char *type,
  363. int argc, char **argv,
  364. grub_addr_t addr, grub_uint32_t size)
  365. {
  366. const char *name;
  367. grub_err_t err;
  368. name = grub_strrchr (filename, '/');
  369. if (name)
  370. name++;
  371. else
  372. name = filename;
  373. if (grub_strcmp (type, "/boot/zfs/zpool.cache") == 0)
  374. name = "/boot/zfs/zpool.cache";
  375. if (grub_bsd_add_meta (FREEBSD_MODINFO_NAME, name, grub_strlen (name) + 1))
  376. return grub_errno;
  377. if (is_64bit)
  378. {
  379. grub_uint64_t addr64 = addr, size64 = size;
  380. if (grub_bsd_add_meta (FREEBSD_MODINFO_TYPE, type, grub_strlen (type) + 1)
  381. || grub_bsd_add_meta (FREEBSD_MODINFO_ADDR, &addr64, sizeof (addr64))
  382. || grub_bsd_add_meta (FREEBSD_MODINFO_SIZE, &size64, sizeof (size64)))
  383. return grub_errno;
  384. }
  385. else
  386. {
  387. if (grub_bsd_add_meta (FREEBSD_MODINFO_TYPE, type, grub_strlen (type) + 1)
  388. || grub_bsd_add_meta (FREEBSD_MODINFO_ADDR, &addr, sizeof (addr))
  389. || grub_bsd_add_meta (FREEBSD_MODINFO_SIZE, &size, sizeof (size)))
  390. return grub_errno;
  391. }
  392. if (argc)
  393. {
  394. int i, n;
  395. n = 0;
  396. for (i = 0; i < argc; i++)
  397. {
  398. n += grub_strlen (argv[i]) + 1;
  399. }
  400. if (n)
  401. {
  402. void *cmdline;
  403. char *p;
  404. if (grub_bsd_add_meta_ptr (FREEBSD_MODINFO_ARGS, &cmdline, n))
  405. return grub_errno;
  406. p = cmdline;
  407. for (i = 0; i < argc; i++)
  408. {
  409. grub_strcpy (p, argv[i]);
  410. p += grub_strlen (argv[i]);
  411. *(p++) = ' ';
  412. }
  413. *p = 0;
  414. err = grub_verify_string (cmdline, GRUB_VERIFY_MODULE_CMDLINE);
  415. if (err)
  416. return err;
  417. }
  418. }
  419. return GRUB_ERR_NONE;
  420. }
  421. static void
  422. grub_freebsd_list_modules (void)
  423. {
  424. struct bsd_tag *tag;
  425. grub_printf (" %-18s %-18s%14s%14s\n", _("name"), _("type"), _("addr"),
  426. _("size"));
  427. for (tag = tags; tag; tag = tag->next)
  428. {
  429. switch (tag->type)
  430. {
  431. case FREEBSD_MODINFO_NAME:
  432. case FREEBSD_MODINFO_TYPE:
  433. grub_printf (" %-18s", (char *) tag->data);
  434. break;
  435. case FREEBSD_MODINFO_ADDR:
  436. {
  437. grub_uint32_t addr;
  438. addr = *((grub_uint32_t *) tag->data);
  439. grub_printf (" 0x%08x", addr);
  440. break;
  441. }
  442. case FREEBSD_MODINFO_SIZE:
  443. {
  444. grub_uint32_t len;
  445. len = *((grub_uint32_t *) tag->data);
  446. grub_printf (" 0x%08x\n", len);
  447. }
  448. }
  449. }
  450. }
  451. static grub_err_t
  452. grub_netbsd_add_meta_module (char *filename, grub_uint32_t type,
  453. grub_addr_t addr, grub_uint32_t size)
  454. {
  455. char *name;
  456. struct netbsd_module *mod;
  457. name = grub_strrchr (filename, '/');
  458. if (name)
  459. name++;
  460. else
  461. name = filename;
  462. mod = grub_zalloc (sizeof (*mod));
  463. if (!mod)
  464. return grub_errno;
  465. grub_strncpy (mod->mod.name, name, sizeof (mod->mod.name) - 1);
  466. mod->mod.addr = addr;
  467. mod->mod.type = type;
  468. mod->mod.size = size;
  469. if (netbsd_mods_last)
  470. netbsd_mods_last->next = mod;
  471. else
  472. netbsd_mods = mod;
  473. netbsd_mods_last = mod;
  474. return GRUB_ERR_NONE;
  475. }
  476. static void
  477. grub_netbsd_list_modules (void)
  478. {
  479. struct netbsd_module *mod;
  480. grub_printf (" %-18s%14s%14s%14s\n", _("name"), _("type"), _("addr"),
  481. _("size"));
  482. for (mod = netbsd_mods; mod; mod = mod->next)
  483. grub_printf (" %-18s 0x%08x 0x%08x 0x%08x", mod->mod.name,
  484. mod->mod.type, mod->mod.addr, mod->mod.size);
  485. }
  486. /* This function would be here but it's under different license. */
  487. #include "bsd_pagetable.c"
  488. static grub_uint32_t freebsd_bootdev, freebsd_biosdev;
  489. static grub_uint64_t freebsd_zfsguid;
  490. static void
  491. freebsd_get_zfs (void)
  492. {
  493. grub_device_t dev;
  494. grub_fs_t fs;
  495. char *uuid;
  496. grub_err_t err;
  497. dev = grub_device_open (0);
  498. if (!dev)
  499. return;
  500. fs = grub_fs_probe (dev);
  501. if (!fs)
  502. return;
  503. if (!fs->fs_uuid || grub_strcmp (fs->name, "zfs") != 0)
  504. return;
  505. err = fs->fs_uuid (dev, &uuid);
  506. if (err)
  507. return;
  508. if (!uuid)
  509. return;
  510. freebsd_zfsguid = grub_strtoull (uuid, 0, 16);
  511. grub_free (uuid);
  512. }
  513. static grub_err_t
  514. grub_freebsd_boot (void)
  515. {
  516. struct grub_freebsd_bootinfo bi;
  517. grub_uint8_t *p, *p0;
  518. grub_addr_t p_target;
  519. grub_size_t p_size = 0;
  520. grub_err_t err;
  521. grub_size_t tag_buf_len = 0;
  522. struct grub_env_var *var;
  523. grub_memset (&bi, 0, sizeof (bi));
  524. bi.version = FREEBSD_BOOTINFO_VERSION;
  525. bi.length = sizeof (bi);
  526. bi.boot_device = freebsd_biosdev;
  527. p_size = 0;
  528. FOR_SORTED_ENV (var)
  529. if ((grub_memcmp (var->name, "kFreeBSD.", sizeof("kFreeBSD.") - 1) == 0) && (var->name[sizeof("kFreeBSD.") - 1]))
  530. {
  531. p_size += grub_strlen (&var->name[sizeof("kFreeBSD.") - 1]);
  532. p_size++;
  533. p_size += grub_strlen (var->value) + 1;
  534. }
  535. if (p_size)
  536. p_size = ALIGN_PAGE (kern_end + p_size + 1) - kern_end;
  537. if (is_elf_kernel)
  538. {
  539. struct bsd_tag *tag;
  540. err = grub_bsd_add_mmap ();
  541. if (err)
  542. return err;
  543. err = grub_bsd_add_meta (FREEBSD_MODINFO_END, 0, 0);
  544. if (err)
  545. return err;
  546. tag_buf_len = 0;
  547. for (tag = tags; tag; tag = tag->next)
  548. tag_buf_len = ALIGN_VAR (tag_buf_len
  549. + sizeof (struct freebsd_tag_header)
  550. + tag->len);
  551. p_size = ALIGN_PAGE (kern_end + p_size + tag_buf_len) - kern_end;
  552. }
  553. if (is_64bit)
  554. p_size += 4096 * 3;
  555. {
  556. grub_relocator_chunk_t ch;
  557. err = grub_relocator_alloc_chunk_addr (relocator, &ch,
  558. kern_end, p_size);
  559. if (err)
  560. return err;
  561. p = get_virtual_current_address (ch);
  562. }
  563. p_target = kern_end;
  564. p0 = p;
  565. kern_end += p_size;
  566. FOR_SORTED_ENV (var)
  567. if ((grub_memcmp (var->name, "kFreeBSD.", sizeof("kFreeBSD.") - 1) == 0) && (var->name[sizeof("kFreeBSD.") - 1]))
  568. {
  569. grub_strcpy ((char *) p, &var->name[sizeof("kFreeBSD.") - 1]);
  570. p += grub_strlen ((char *) p);
  571. *(p++) = '=';
  572. grub_strcpy ((char *) p, var->value);
  573. p += grub_strlen ((char *) p) + 1;
  574. }
  575. if (p != p0)
  576. {
  577. *(p++) = 0;
  578. bi.environment = p_target;
  579. }
  580. if (is_elf_kernel)
  581. {
  582. grub_uint8_t *p_tag = p;
  583. struct bsd_tag *tag;
  584. for (tag = tags; tag; tag = tag->next)
  585. {
  586. struct freebsd_tag_header *head
  587. = (struct freebsd_tag_header *) p_tag;
  588. head->type = tag->type;
  589. head->len = tag->len;
  590. p_tag += sizeof (struct freebsd_tag_header);
  591. switch (tag->type)
  592. {
  593. case FREEBSD_MODINFO_METADATA | FREEBSD_MODINFOMD_HOWTO:
  594. if (is_64bit)
  595. *(grub_uint64_t *) p_tag = bootflags;
  596. else
  597. *(grub_uint32_t *) p_tag = bootflags;
  598. break;
  599. case FREEBSD_MODINFO_METADATA | FREEBSD_MODINFOMD_ENVP:
  600. if (is_64bit)
  601. *(grub_uint64_t *) p_tag = bi.environment;
  602. else
  603. *(grub_uint32_t *) p_tag = bi.environment;
  604. break;
  605. case FREEBSD_MODINFO_METADATA | FREEBSD_MODINFOMD_KERNEND:
  606. if (is_64bit)
  607. *(grub_uint64_t *) p_tag = kern_end;
  608. else
  609. *(grub_uint32_t *) p_tag = kern_end;
  610. break;
  611. default:
  612. grub_memcpy (p_tag, tag->data, tag->len);
  613. break;
  614. }
  615. p_tag += tag->len;
  616. p_tag = ALIGN_VAR (p_tag - p) + p;
  617. }
  618. bi.tags = (p - p0) + p_target;
  619. p = (ALIGN_PAGE ((p_tag - p0) + p_target) - p_target) + p0;
  620. }
  621. bi.kern_end = kern_end;
  622. grub_video_set_mode ("text", 0, 0);
  623. if (is_64bit)
  624. {
  625. struct grub_relocator64_state state = {0};
  626. grub_uint8_t *pagetable;
  627. grub_uint32_t *stack;
  628. grub_addr_t stack_target;
  629. {
  630. grub_relocator_chunk_t ch;
  631. err = grub_relocator_alloc_chunk_align (relocator, &ch,
  632. 0x10000, 0x90000,
  633. 3 * sizeof (grub_uint32_t)
  634. + sizeof (bi), 4,
  635. GRUB_RELOCATOR_PREFERENCE_NONE,
  636. 0);
  637. if (err)
  638. return err;
  639. stack = get_virtual_current_address (ch);
  640. stack_target = get_physical_target_address (ch);
  641. }
  642. #ifdef GRUB_MACHINE_EFI
  643. err = grub_efi_finish_boot_services (NULL, NULL, NULL, NULL, NULL);
  644. if (err)
  645. return err;
  646. #endif
  647. pagetable = p;
  648. fill_bsd64_pagetable (pagetable, (pagetable - p0) + p_target);
  649. state.cr3 = (pagetable - p0) + p_target;
  650. state.rsp = stack_target;
  651. state.rip = (((grub_uint64_t) entry_hi) << 32) | entry;
  652. stack[0] = entry;
  653. stack[1] = bi.tags;
  654. stack[2] = kern_end;
  655. return grub_relocator64_boot (relocator, state, 0, 0x40000000);
  656. }
  657. else
  658. {
  659. struct grub_relocator32_state state = {0};
  660. grub_uint32_t *stack;
  661. grub_addr_t stack_target;
  662. {
  663. grub_relocator_chunk_t ch;
  664. err = grub_relocator_alloc_chunk_align (relocator, &ch,
  665. 0x10000, 0x90000,
  666. 9 * sizeof (grub_uint32_t)
  667. + sizeof (bi), 4,
  668. GRUB_RELOCATOR_PREFERENCE_NONE,
  669. 0);
  670. if (err)
  671. return err;
  672. stack = get_virtual_current_address (ch);
  673. stack_target = get_physical_target_address (ch);
  674. }
  675. #ifdef GRUB_MACHINE_EFI
  676. err = grub_efi_finish_boot_services (NULL, NULL, NULL, NULL, NULL);
  677. if (err)
  678. return err;
  679. #endif
  680. grub_memcpy (&stack[9], &bi, sizeof (bi));
  681. state.eip = entry;
  682. state.esp = stack_target;
  683. state.ebp = stack_target;
  684. stack[0] = entry; /* "Return" address. */
  685. stack[1] = bootflags | FREEBSD_RB_BOOTINFO;
  686. stack[2] = freebsd_bootdev;
  687. stack[3] = freebsd_zfsguid ? 4 : 0;
  688. stack[4] = freebsd_zfsguid;
  689. stack[5] = freebsd_zfsguid >> 32;
  690. stack[6] = stack_target + 9 * sizeof (grub_uint32_t);
  691. stack[7] = bi.tags;
  692. stack[8] = kern_end;
  693. return grub_relocator32_boot (relocator, state, 0);
  694. }
  695. /* Not reached. */
  696. return GRUB_ERR_NONE;
  697. }
  698. static grub_err_t
  699. grub_openbsd_boot (void)
  700. {
  701. grub_uint32_t *stack;
  702. struct grub_relocator32_state state;
  703. void *curarg, *buf0, *arg0;
  704. grub_addr_t buf_target;
  705. grub_err_t err;
  706. grub_size_t tag_buf_len;
  707. err = grub_bsd_add_mmap ();
  708. if (err)
  709. return err;
  710. #ifdef GRUB_MACHINE_PCBIOS
  711. {
  712. struct grub_bios_int_registers regs;
  713. regs.flags = GRUB_CPU_INT_FLAGS_DEFAULT;
  714. regs.ebx = 0;
  715. regs.ecx = 0;
  716. regs.eax = 0xb101;
  717. regs.es = 0;
  718. regs.edi = 0;
  719. regs.edx = 0;
  720. grub_bios_interrupt (0x1a, &regs);
  721. if (regs.edx == 0x20494350)
  722. {
  723. struct grub_openbsd_bootarg_pcibios pcibios;
  724. pcibios.characteristics = regs.eax & 0xff;
  725. pcibios.revision = regs.ebx & 0xffff;
  726. pcibios.pm_entry = regs.edi;
  727. pcibios.last_bus = regs.ecx & 0xff;
  728. grub_bsd_add_meta (OPENBSD_BOOTARG_PCIBIOS, &pcibios,
  729. sizeof (pcibios));
  730. }
  731. }
  732. #endif
  733. {
  734. struct bsd_tag *tag;
  735. tag_buf_len = 0;
  736. for (tag = tags; tag; tag = tag->next)
  737. tag_buf_len = ALIGN_VAR (tag_buf_len
  738. + sizeof (struct grub_openbsd_bootargs)
  739. + tag->len);
  740. }
  741. buf_target = GRUB_BSD_TEMP_BUFFER - 9 * sizeof (grub_uint32_t);
  742. {
  743. grub_relocator_chunk_t ch;
  744. err = grub_relocator_alloc_chunk_addr (relocator, &ch, buf_target,
  745. tag_buf_len
  746. + sizeof (struct grub_openbsd_bootargs)
  747. + 9 * sizeof (grub_uint32_t));
  748. if (err)
  749. return err;
  750. buf0 = get_virtual_current_address (ch);
  751. }
  752. stack = (grub_uint32_t *) buf0;
  753. arg0 = curarg = stack + 9;
  754. {
  755. struct bsd_tag *tag;
  756. struct grub_openbsd_bootargs *head;
  757. for (tag = tags; tag; tag = tag->next)
  758. {
  759. head = curarg;
  760. head->ba_type = tag->type;
  761. head->ba_size = tag->len + sizeof (*head);
  762. curarg = head + 1;
  763. grub_memcpy (curarg, tag->data, tag->len);
  764. curarg = (grub_uint8_t *) curarg + tag->len;
  765. head->ba_next = (grub_uint8_t *) curarg - (grub_uint8_t *) buf0
  766. + buf_target;
  767. }
  768. head = curarg;
  769. head->ba_type = OPENBSD_BOOTARG_END;
  770. head->ba_size = 0;
  771. head->ba_next = 0;
  772. }
  773. grub_video_set_mode ("text", 0, 0);
  774. #ifdef GRUB_MACHINE_EFI
  775. err = grub_efi_finish_boot_services (NULL, NULL, NULL, NULL, NULL);
  776. if (err)
  777. return err;
  778. #endif
  779. state.eip = entry;
  780. state.ebp = state.esp
  781. = ((grub_uint8_t *) stack - (grub_uint8_t *) buf0) + buf_target;
  782. stack[0] = entry;
  783. stack[1] = bootflags;
  784. stack[2] = openbsd_root;
  785. stack[3] = OPENBSD_BOOTARG_APIVER;
  786. stack[4] = 0;
  787. stack[5] = grub_mmap_get_upper () >> 10;
  788. stack[6] = grub_mmap_get_lower () >> 10;
  789. stack[7] = (grub_uint8_t *) curarg - (grub_uint8_t *) arg0;
  790. stack[8] = ((grub_uint8_t *) arg0 - (grub_uint8_t *) buf0) + buf_target;
  791. return grub_relocator32_boot (relocator, state, 0);
  792. }
  793. static grub_err_t
  794. grub_netbsd_setup_video (void)
  795. {
  796. struct grub_video_mode_info mode_info;
  797. void *framebuffer;
  798. const char *modevar;
  799. struct grub_netbsd_btinfo_framebuf params = {0};
  800. grub_err_t err;
  801. grub_video_driver_id_t driv_id;
  802. modevar = grub_env_get ("gfxpayload");
  803. /* Now all graphical modes are acceptable.
  804. May change in future if we have modes without framebuffer. */
  805. if (modevar && *modevar != 0)
  806. {
  807. char *tmp;
  808. tmp = grub_xasprintf ("%s;" NETBSD_DEFAULT_VIDEO_MODE, modevar);
  809. if (! tmp)
  810. return grub_errno;
  811. err = grub_video_set_mode (tmp, 0, 0);
  812. grub_free (tmp);
  813. }
  814. else
  815. err = grub_video_set_mode (NETBSD_DEFAULT_VIDEO_MODE, 0, 0);
  816. if (err)
  817. return err;
  818. driv_id = grub_video_get_driver_id ();
  819. if (driv_id == GRUB_VIDEO_DRIVER_NONE)
  820. return GRUB_ERR_NONE;
  821. err = grub_video_get_info_and_fini (&mode_info, &framebuffer);
  822. if (err)
  823. return err;
  824. params.width = mode_info.width;
  825. params.height = mode_info.height;
  826. params.bpp = mode_info.bpp;
  827. params.pitch = mode_info.pitch;
  828. params.flags = 0;
  829. params.fbaddr = (grub_addr_t) framebuffer;
  830. params.red_mask_size = mode_info.red_mask_size;
  831. params.red_field_pos = mode_info.red_field_pos;
  832. params.green_mask_size = mode_info.green_mask_size;
  833. params.green_field_pos = mode_info.green_field_pos;
  834. params.blue_mask_size = mode_info.blue_mask_size;
  835. params.blue_field_pos = mode_info.blue_field_pos;
  836. #ifdef GRUB_MACHINE_PCBIOS
  837. /* VESA packed modes may come with zeroed mask sizes, which need
  838. to be set here according to DAC Palette width. If we don't,
  839. this results in Linux displaying a black screen. */
  840. if (mode_info.bpp <= 8 && driv_id == GRUB_VIDEO_DRIVER_VBE)
  841. {
  842. struct grub_vbe_info_block controller_info;
  843. int status;
  844. int width = 8;
  845. status = grub_vbe_bios_get_controller_info (&controller_info);
  846. if (status == GRUB_VBE_STATUS_OK &&
  847. (controller_info.capabilities & GRUB_VBE_CAPABILITY_DACWIDTH))
  848. status = grub_vbe_bios_set_dac_palette_width (&width);
  849. if (status != GRUB_VBE_STATUS_OK)
  850. /* 6 is default after mode reset. */
  851. width = 6;
  852. params.red_mask_size = params.green_mask_size
  853. = params.blue_mask_size = width;
  854. }
  855. #endif
  856. err = grub_bsd_add_meta (NETBSD_BTINFO_FRAMEBUF, &params, sizeof (params));
  857. return err;
  858. }
  859. static grub_err_t
  860. grub_netbsd_add_modules (void)
  861. {
  862. struct netbsd_module *mod;
  863. unsigned modcnt = 0;
  864. struct grub_netbsd_btinfo_modules *mods;
  865. unsigned i;
  866. grub_err_t err;
  867. grub_size_t sz;
  868. for (mod = netbsd_mods; mod; mod = mod->next)
  869. modcnt++;
  870. if (grub_mul (modcnt, sizeof (mods->mods[0]), &sz) ||
  871. grub_add (sz, sizeof (*mods), &sz))
  872. return GRUB_ERR_OUT_OF_RANGE;
  873. mods = grub_malloc (sz);
  874. if (!mods)
  875. return grub_errno;
  876. mods->num = modcnt;
  877. mods->last_addr = kern_end;
  878. for (mod = netbsd_mods, i = 0; mod; i++, mod = mod->next)
  879. mods->mods[i] = mod->mod;
  880. err = grub_bsd_add_meta (NETBSD_BTINFO_MODULES, mods,
  881. sizeof (*mods) + sizeof (mods->mods[0]) * modcnt);
  882. grub_free (mods);
  883. return err;
  884. }
  885. /*
  886. * Adds NetBSD bootinfo bootdisk and bootwedge. The partition identified
  887. * in these bootinfo fields is the root device.
  888. */
  889. static void
  890. grub_netbsd_add_boot_disk_and_wedge (void)
  891. {
  892. grub_device_t dev;
  893. grub_disk_t disk;
  894. grub_partition_t part;
  895. grub_uint32_t biosdev;
  896. grub_uint32_t partmapsector;
  897. union {
  898. grub_uint64_t raw[GRUB_DISK_SECTOR_SIZE / 8];
  899. struct grub_partition_bsd_disk_label label;
  900. } buf;
  901. if (GRUB_MD_MD5->mdlen > GRUB_CRYPTO_MAX_MDLEN)
  902. {
  903. grub_error (GRUB_ERR_BUG, "mdlen too long");
  904. return;
  905. }
  906. dev = grub_device_open (0);
  907. if (! (dev && dev->disk && dev->disk->partition))
  908. goto fail;
  909. disk = dev->disk;
  910. part = disk->partition;
  911. if (disk->dev && disk->dev->id == GRUB_DISK_DEVICE_BIOSDISK_ID)
  912. biosdev = (grub_uint32_t) disk->id & 0xff;
  913. else
  914. biosdev = 0xff;
  915. /* Absolute sector of the partition map describing this partition. */
  916. partmapsector = grub_partition_get_start (part->parent) + part->offset;
  917. disk->partition = part->parent;
  918. if (grub_disk_read (disk, part->offset, 0, GRUB_DISK_SECTOR_SIZE, buf.raw)
  919. != GRUB_ERR_NONE)
  920. goto fail;
  921. disk->partition = part;
  922. /* Fill bootwedge. */
  923. {
  924. struct grub_netbsd_btinfo_bootwedge biw;
  925. grub_uint8_t hash[GRUB_CRYPTO_MAX_MDLEN];
  926. grub_memset (&biw, 0, sizeof (biw));
  927. biw.biosdev = biosdev;
  928. biw.startblk = grub_partition_get_start (part);
  929. biw.nblks = part->len;
  930. biw.matchblk = partmapsector;
  931. biw.matchnblks = 1;
  932. grub_crypto_hash (GRUB_MD_MD5, hash,
  933. buf.raw, GRUB_DISK_SECTOR_SIZE);
  934. grub_memcpy (biw.matchhash, hash, 16);
  935. grub_bsd_add_meta (NETBSD_BTINFO_BOOTWEDGE, &biw, sizeof (biw));
  936. }
  937. /* Fill bootdisk. */
  938. {
  939. struct grub_netbsd_btinfo_bootdisk bid;
  940. grub_memset (&bid, 0, sizeof (bid));
  941. /* Check for a NetBSD disk label. */
  942. if (part->partmap != NULL &&
  943. (grub_strcmp (part->partmap->name, "netbsd") == 0 ||
  944. (part->parent == NULL && grub_strcmp (part->partmap->name, "bsd") == 0)))
  945. {
  946. bid.labelsector = partmapsector;
  947. bid.label.type = buf.label.type;
  948. bid.label.checksum = buf.label.checksum;
  949. grub_memcpy (bid.label.packname, buf.label.packname, 16);
  950. }
  951. else
  952. {
  953. bid.labelsector = -1;
  954. }
  955. bid.biosdev = biosdev;
  956. bid.partition = part->number;
  957. grub_bsd_add_meta (NETBSD_BTINFO_BOOTDISK, &bid, sizeof (bid));
  958. }
  959. fail:
  960. if (dev)
  961. grub_device_close (dev);
  962. }
  963. static grub_err_t
  964. grub_netbsd_boot (void)
  965. {
  966. struct grub_netbsd_bootinfo *bootinfo;
  967. void *curarg, *arg0;
  968. grub_addr_t arg_target, stack_target;
  969. grub_uint32_t *stack;
  970. grub_err_t err;
  971. struct grub_relocator32_state state;
  972. grub_size_t tag_buf_len = 0;
  973. int tag_count = 0;
  974. err = grub_bsd_add_mmap ();
  975. if (err)
  976. return err;
  977. err = grub_netbsd_setup_video ();
  978. if (err)
  979. {
  980. grub_print_error ();
  981. grub_puts_ (N_("Booting in blind mode"));
  982. grub_errno = GRUB_ERR_NONE;
  983. }
  984. err = grub_netbsd_add_modules ();
  985. if (err)
  986. return err;
  987. #ifdef GRUB_MACHINE_EFI
  988. err = grub_bsd_add_meta (NETBSD_BTINFO_EFI,
  989. &grub_efi_system_table,
  990. sizeof (grub_efi_system_table));
  991. if (err)
  992. return err;
  993. #endif
  994. {
  995. struct bsd_tag *tag;
  996. tag_buf_len = 0;
  997. for (tag = tags; tag; tag = tag->next)
  998. {
  999. tag_buf_len = ALIGN_VAR (tag_buf_len
  1000. + sizeof (struct grub_netbsd_btinfo_common)
  1001. + tag->len);
  1002. tag_count++;
  1003. }
  1004. }
  1005. arg_target = kern_end;
  1006. {
  1007. grub_relocator_chunk_t ch;
  1008. err = grub_relocator_alloc_chunk_addr (relocator, &ch,
  1009. arg_target, tag_buf_len
  1010. + sizeof (struct grub_netbsd_bootinfo)
  1011. + tag_count * sizeof (grub_uint32_t));
  1012. if (err)
  1013. return err;
  1014. curarg = get_virtual_current_address (ch);
  1015. }
  1016. arg0 = curarg;
  1017. bootinfo = (void *) ((grub_uint8_t *) arg0 + tag_buf_len);
  1018. {
  1019. struct bsd_tag *tag;
  1020. unsigned i;
  1021. bootinfo->bi_count = tag_count;
  1022. for (tag = tags, i = 0; tag; i++, tag = tag->next)
  1023. {
  1024. struct grub_netbsd_btinfo_common *head = curarg;
  1025. bootinfo->bi_data[i] = ((grub_uint8_t *) curarg - (grub_uint8_t *) arg0)
  1026. + arg_target;
  1027. head->type = tag->type;
  1028. head->len = tag->len + sizeof (*head);
  1029. curarg = head + 1;
  1030. grub_memcpy (curarg, tag->data, tag->len);
  1031. curarg = (grub_uint8_t *) curarg + tag->len;
  1032. }
  1033. }
  1034. {
  1035. grub_relocator_chunk_t ch;
  1036. err = grub_relocator_alloc_chunk_align (relocator, &ch, 0x10000, 0x90000,
  1037. 7 * sizeof (grub_uint32_t), 4,
  1038. GRUB_RELOCATOR_PREFERENCE_NONE,
  1039. 0);
  1040. if (err)
  1041. return err;
  1042. stack = get_virtual_current_address (ch);
  1043. stack_target = get_physical_target_address (ch);
  1044. }
  1045. #ifdef GRUB_MACHINE_EFI
  1046. err = grub_efi_finish_boot_services (NULL, NULL, NULL, NULL, NULL);
  1047. if (err)
  1048. return err;
  1049. #endif
  1050. state.eip = entry;
  1051. state.esp = stack_target;
  1052. state.ebp = stack_target;
  1053. stack[0] = entry;
  1054. stack[1] = bootflags;
  1055. stack[2] = 0;
  1056. stack[3] = ((grub_uint8_t *) bootinfo - (grub_uint8_t *) arg0) + arg_target;
  1057. stack[4] = 0;
  1058. stack[5] = grub_mmap_get_upper () >> 10;
  1059. stack[6] = grub_mmap_get_lower () >> 10;
  1060. return grub_relocator32_boot (relocator, state, 0);
  1061. }
  1062. static grub_err_t
  1063. grub_bsd_unload (void)
  1064. {
  1065. struct bsd_tag *tag, *next;
  1066. for (tag = tags; tag; tag = next)
  1067. {
  1068. next = tag->next;
  1069. grub_free (tag);
  1070. }
  1071. tags = NULL;
  1072. tags_last = NULL;
  1073. kernel_type = KERNEL_TYPE_NONE;
  1074. grub_dl_unref (my_mod);
  1075. grub_relocator_unload (relocator);
  1076. relocator = NULL;
  1077. return GRUB_ERR_NONE;
  1078. }
  1079. static grub_err_t
  1080. grub_bsd_load_aout (grub_file_t file, const char *filename)
  1081. {
  1082. grub_addr_t load_addr, load_end;
  1083. int ofs, align_page;
  1084. union grub_aout_header ah;
  1085. grub_err_t err;
  1086. grub_size_t bss_size;
  1087. if ((grub_file_seek (file, 0)) == (grub_off_t) - 1)
  1088. return grub_errno;
  1089. if (grub_file_read (file, &ah, sizeof (ah)) != sizeof (ah))
  1090. {
  1091. if (!grub_errno)
  1092. grub_error (GRUB_ERR_READ_ERROR, N_("premature end of file %s"),
  1093. filename);
  1094. return grub_errno;
  1095. }
  1096. if (grub_aout_get_type (&ah) != AOUT_TYPE_AOUT32)
  1097. return grub_error (GRUB_ERR_BAD_OS, "invalid a.out header");
  1098. entry = ah.aout32.a_entry & 0xFFFFFF;
  1099. if (AOUT_GETMAGIC (ah.aout32) == AOUT32_ZMAGIC)
  1100. {
  1101. load_addr = entry;
  1102. ofs = 0x1000;
  1103. align_page = 0;
  1104. }
  1105. else
  1106. {
  1107. load_addr = entry & 0xF00000;
  1108. ofs = sizeof (struct grub_aout32_header);
  1109. align_page = 1;
  1110. }
  1111. if (load_addr < 0x100000)
  1112. return grub_error (GRUB_ERR_BAD_OS, "load address below 1M");
  1113. kern_start = load_addr;
  1114. load_end = kern_end = load_addr + ah.aout32.a_text + ah.aout32.a_data;
  1115. if (align_page)
  1116. kern_end = ALIGN_PAGE (kern_end);
  1117. if (ah.aout32.a_bss)
  1118. {
  1119. kern_end += ah.aout32.a_bss;
  1120. if (align_page)
  1121. kern_end = ALIGN_PAGE (kern_end);
  1122. bss_size = kern_end - load_end;
  1123. }
  1124. else
  1125. bss_size = 0;
  1126. {
  1127. grub_relocator_chunk_t ch;
  1128. err = grub_relocator_alloc_chunk_addr (relocator, &ch,
  1129. kern_start, kern_end - kern_start);
  1130. if (err)
  1131. return err;
  1132. kern_chunk_src = get_virtual_current_address (ch);
  1133. }
  1134. return grub_aout_load (file, ofs, kern_chunk_src,
  1135. ah.aout32.a_text + ah.aout32.a_data,
  1136. bss_size);
  1137. }
  1138. static grub_err_t
  1139. grub_bsd_load_elf (grub_elf_t elf, const char *filename)
  1140. {
  1141. grub_err_t err;
  1142. kern_end = 0;
  1143. kern_start = ~0;
  1144. if (grub_elf_is_elf32 (elf))
  1145. {
  1146. grub_relocator_chunk_t ch;
  1147. Elf32_Phdr *phdr;
  1148. entry = elf->ehdr.ehdr32.e_entry & 0xFFFFFFF;
  1149. FOR_ELF32_PHDRS (elf, phdr)
  1150. {
  1151. Elf32_Addr paddr;
  1152. if (phdr->p_type != PT_LOAD
  1153. && phdr->p_type != PT_DYNAMIC)
  1154. continue;
  1155. paddr = phdr->p_paddr & 0xFFFFFFF;
  1156. if (paddr < kern_start)
  1157. kern_start = paddr;
  1158. if (paddr + phdr->p_memsz > kern_end)
  1159. kern_end = paddr + phdr->p_memsz;
  1160. }
  1161. if (grub_errno)
  1162. return grub_errno;
  1163. err = grub_relocator_alloc_chunk_addr (relocator, &ch,
  1164. kern_start, kern_end - kern_start);
  1165. if (err)
  1166. return err;
  1167. kern_chunk_src = get_virtual_current_address (ch);
  1168. err = grub_elf32_load (elf, filename, (grub_uint8_t *) kern_chunk_src - kern_start, GRUB_ELF_LOAD_FLAGS_LOAD_PT_DYNAMIC | GRUB_ELF_LOAD_FLAGS_28BITS, 0, 0);
  1169. if (err)
  1170. return err;
  1171. if (kernel_type != KERNEL_TYPE_OPENBSD)
  1172. return GRUB_ERR_NONE;
  1173. return grub_openbsd_find_ramdisk32 (elf->file, filename, kern_start,
  1174. kern_chunk_src, &openbsd_ramdisk);
  1175. }
  1176. else if (grub_elf_is_elf64 (elf))
  1177. {
  1178. Elf64_Phdr *phdr;
  1179. is_64bit = 1;
  1180. if (! grub_cpuid_has_longmode)
  1181. return grub_error (GRUB_ERR_BAD_OS, "your CPU does not implement AMD64 architecture");
  1182. /* FreeBSD has 64-bit entry point. */
  1183. if (kernel_type == KERNEL_TYPE_FREEBSD)
  1184. {
  1185. entry = elf->ehdr.ehdr64.e_entry & 0xffffffff;
  1186. entry_hi = (elf->ehdr.ehdr64.e_entry >> 32) & 0xffffffff;
  1187. }
  1188. else
  1189. {
  1190. entry = elf->ehdr.ehdr64.e_entry & 0x0fffffff;
  1191. entry_hi = 0;
  1192. }
  1193. FOR_ELF64_PHDRS (elf, phdr)
  1194. {
  1195. Elf64_Addr paddr;
  1196. if (phdr->p_type != PT_LOAD
  1197. && phdr->p_type != PT_DYNAMIC)
  1198. continue;
  1199. paddr = phdr->p_paddr & 0xFFFFFFF;
  1200. if (paddr < kern_start)
  1201. kern_start = paddr;
  1202. if (paddr + phdr->p_memsz > kern_end)
  1203. kern_end = paddr + phdr->p_memsz;
  1204. }
  1205. if (grub_errno)
  1206. return grub_errno;
  1207. grub_dprintf ("bsd", "kern_start = %lx, kern_end = %lx\n",
  1208. (unsigned long) kern_start, (unsigned long) kern_end);
  1209. {
  1210. grub_relocator_chunk_t ch;
  1211. err = grub_relocator_alloc_chunk_addr (relocator, &ch, kern_start,
  1212. kern_end - kern_start);
  1213. if (err)
  1214. return err;
  1215. kern_chunk_src = get_virtual_current_address (ch);
  1216. }
  1217. err = grub_elf64_load (elf, filename,
  1218. (grub_uint8_t *) kern_chunk_src - kern_start, GRUB_ELF_LOAD_FLAGS_LOAD_PT_DYNAMIC | GRUB_ELF_LOAD_FLAGS_28BITS, 0, 0);
  1219. if (err)
  1220. return err;
  1221. if (kernel_type != KERNEL_TYPE_OPENBSD)
  1222. return GRUB_ERR_NONE;
  1223. return grub_openbsd_find_ramdisk64 (elf->file, filename, kern_start,
  1224. kern_chunk_src, &openbsd_ramdisk);
  1225. }
  1226. else
  1227. return grub_error (GRUB_ERR_BAD_OS, N_("invalid arch-dependent ELF magic"));
  1228. }
  1229. static grub_err_t
  1230. grub_bsd_load (int argc, char *argv[])
  1231. {
  1232. grub_file_t file;
  1233. grub_elf_t elf;
  1234. grub_dl_ref (my_mod);
  1235. grub_loader_unset ();
  1236. grub_memset (&openbsd_ramdisk, 0, sizeof (openbsd_ramdisk));
  1237. if (argc == 0)
  1238. {
  1239. grub_error (GRUB_ERR_BAD_ARGUMENT, N_("filename expected"));
  1240. goto fail;
  1241. }
  1242. file = grub_file_open (argv[0], GRUB_FILE_TYPE_BSD_KERNEL);
  1243. if (!file)
  1244. goto fail;
  1245. relocator = grub_relocator_new ();
  1246. if (!relocator)
  1247. {
  1248. grub_file_close (file);
  1249. goto fail;
  1250. }
  1251. elf = grub_elf_file (file, argv[0]);
  1252. if (elf)
  1253. {
  1254. is_elf_kernel = 1;
  1255. grub_bsd_load_elf (elf, argv[0]);
  1256. grub_elf_close (elf);
  1257. }
  1258. else
  1259. {
  1260. is_elf_kernel = 0;
  1261. grub_errno = 0;
  1262. grub_bsd_load_aout (file, argv[0]);
  1263. grub_file_close (file);
  1264. }
  1265. kern_end = ALIGN_PAGE (kern_end);
  1266. fail:
  1267. if (grub_errno != GRUB_ERR_NONE)
  1268. grub_dl_unref (my_mod);
  1269. return grub_errno;
  1270. }
  1271. static grub_uint32_t
  1272. grub_bsd_parse_flags (const struct grub_arg_list *state,
  1273. const grub_uint32_t * flags)
  1274. {
  1275. grub_uint32_t result = 0;
  1276. unsigned i;
  1277. for (i = 0; flags[i]; i++)
  1278. if (state[i].set)
  1279. result |= flags[i];
  1280. return result;
  1281. }
  1282. static grub_err_t
  1283. grub_cmd_freebsd (grub_extcmd_context_t ctxt, int argc, char *argv[])
  1284. {
  1285. kernel_type = KERNEL_TYPE_FREEBSD;
  1286. bootflags = grub_bsd_parse_flags (ctxt->state, freebsd_flags);
  1287. if (grub_bsd_load (argc, argv) == GRUB_ERR_NONE)
  1288. {
  1289. grub_uint32_t unit, slice, part;
  1290. kern_end = ALIGN_PAGE (kern_end);
  1291. if (is_elf_kernel)
  1292. {
  1293. grub_err_t err;
  1294. grub_uint64_t data = 0;
  1295. grub_file_t file;
  1296. int len = is_64bit ? 8 : 4;
  1297. err = grub_freebsd_add_meta_module (argv[0], is_64bit
  1298. ? FREEBSD_MODTYPE_KERNEL64
  1299. : FREEBSD_MODTYPE_KERNEL,
  1300. argc - 1, argv + 1,
  1301. kern_start,
  1302. kern_end - kern_start);
  1303. if (err)
  1304. return err;
  1305. file = grub_file_open (argv[0], GRUB_FILE_TYPE_BSD_KERNEL);
  1306. if (! file)
  1307. return grub_errno;
  1308. if (is_64bit)
  1309. err = grub_freebsd_load_elf_meta64 (relocator, file, argv[0],
  1310. &kern_end);
  1311. else
  1312. err = grub_freebsd_load_elf_meta32 (relocator, file, argv[0],
  1313. &kern_end);
  1314. if (err)
  1315. return err;
  1316. err = grub_bsd_add_meta (FREEBSD_MODINFO_METADATA |
  1317. FREEBSD_MODINFOMD_HOWTO, &data, 4);
  1318. if (err)
  1319. return err;
  1320. err = grub_bsd_add_meta (FREEBSD_MODINFO_METADATA |
  1321. FREEBSD_MODINFOMD_ENVP, &data, len);
  1322. if (err)
  1323. return err;
  1324. err = grub_bsd_add_meta (FREEBSD_MODINFO_METADATA |
  1325. FREEBSD_MODINFOMD_KERNEND, &data, len);
  1326. if (err)
  1327. return err;
  1328. }
  1329. grub_bsd_get_device (&freebsd_biosdev, &unit, &slice, &part);
  1330. freebsd_zfsguid = 0;
  1331. if (!is_64bit)
  1332. freebsd_get_zfs ();
  1333. grub_print_error ();
  1334. freebsd_bootdev = (FREEBSD_B_DEVMAGIC + ((slice + 1) << FREEBSD_B_SLICESHIFT) +
  1335. (unit << FREEBSD_B_UNITSHIFT) + (part << FREEBSD_B_PARTSHIFT));
  1336. grub_loader_set (grub_freebsd_boot, grub_bsd_unload, 0);
  1337. }
  1338. return grub_errno;
  1339. }
  1340. static const char *types[] = {
  1341. [0] = "wd",
  1342. [2] = "fd",
  1343. [4] = "sd",
  1344. [6] = "cd",
  1345. [14] = "vnd",
  1346. [17] = "rd"
  1347. };
  1348. static grub_err_t
  1349. grub_cmd_openbsd (grub_extcmd_context_t ctxt, int argc, char *argv[])
  1350. {
  1351. grub_uint32_t bootdev;
  1352. kernel_type = KERNEL_TYPE_OPENBSD;
  1353. bootflags = grub_bsd_parse_flags (ctxt->state, openbsd_flags);
  1354. if (ctxt->state[OPENBSD_ROOT_ARG].set && ctxt->state[OPENBSD_ROOT_ARG].arg != NULL)
  1355. {
  1356. const char *arg = ctxt->state[OPENBSD_ROOT_ARG].arg;
  1357. unsigned type, unit, part;
  1358. for (type = 0; type < ARRAY_SIZE (types); type++)
  1359. if (types[type]
  1360. && grub_strncmp (arg, types[type],
  1361. grub_strlen (types[type])) == 0)
  1362. {
  1363. arg += grub_strlen (types[type]);
  1364. break;
  1365. }
  1366. if (type == ARRAY_SIZE (types))
  1367. return grub_error (GRUB_ERR_BAD_ARGUMENT,
  1368. "unknown disk type name");
  1369. unit = grub_strtoul (arg, &arg, 10);
  1370. if (! (*arg >= 'a' && *arg <= 'z'))
  1371. return grub_error (GRUB_ERR_BAD_ARGUMENT,
  1372. "only device specifications of form "
  1373. "<type><number><lowercase letter> are supported");
  1374. part = *arg - 'a';
  1375. bootdev = (OPENBSD_B_DEVMAGIC | (type << OPENBSD_B_TYPESHIFT)
  1376. | (unit << OPENBSD_B_UNITSHIFT)
  1377. | (part << OPENBSD_B_PARTSHIFT));
  1378. }
  1379. else
  1380. bootdev = 0;
  1381. if (ctxt->state[OPENBSD_SERIAL_ARG].set)
  1382. {
  1383. struct grub_openbsd_bootarg_console serial;
  1384. const char *ptr;
  1385. unsigned port = 0;
  1386. unsigned speed = 9600;
  1387. grub_memset (&serial, 0, sizeof (serial));
  1388. if (ctxt->state[OPENBSD_SERIAL_ARG].arg)
  1389. {
  1390. ptr = ctxt->state[OPENBSD_SERIAL_ARG].arg;
  1391. if (grub_memcmp (ptr, "com", sizeof ("com") - 1) != 0)
  1392. return grub_error (GRUB_ERR_BAD_ARGUMENT,
  1393. "only com0-com3 are supported");
  1394. ptr += sizeof ("com") - 1;
  1395. port = grub_strtoul (ptr, &ptr, 0);
  1396. if (port >= 4)
  1397. return grub_error (GRUB_ERR_BAD_ARGUMENT,
  1398. "only com0-com3 are supported");
  1399. if (*ptr == ',')
  1400. {
  1401. ptr++;
  1402. speed = grub_strtoul (ptr, &ptr, 0);
  1403. if (grub_errno)
  1404. return grub_errno;
  1405. }
  1406. }
  1407. serial.device = (GRUB_OPENBSD_COM_MAJOR << 8) | port;
  1408. serial.speed = speed;
  1409. serial.addr = grub_ns8250_hw_get_port (port);
  1410. grub_bsd_add_meta (OPENBSD_BOOTARG_CONSOLE, &serial, sizeof (serial));
  1411. bootflags |= OPENBSD_RB_SERCONS;
  1412. }
  1413. else
  1414. {
  1415. struct grub_openbsd_bootarg_console serial;
  1416. grub_memset (&serial, 0, sizeof (serial));
  1417. serial.device = (GRUB_OPENBSD_VGA_MAJOR << 8);
  1418. serial.addr = 0xffffffff;
  1419. grub_bsd_add_meta (OPENBSD_BOOTARG_CONSOLE, &serial, sizeof (serial));
  1420. bootflags &= ~OPENBSD_RB_SERCONS;
  1421. }
  1422. if (grub_bsd_load (argc, argv) == GRUB_ERR_NONE)
  1423. {
  1424. grub_loader_set (grub_openbsd_boot, grub_bsd_unload, 0);
  1425. openbsd_root = bootdev;
  1426. }
  1427. return grub_errno;
  1428. }
  1429. static grub_err_t
  1430. grub_cmd_netbsd (grub_extcmd_context_t ctxt, int argc, char *argv[])
  1431. {
  1432. grub_err_t err;
  1433. kernel_type = KERNEL_TYPE_NETBSD;
  1434. bootflags = grub_bsd_parse_flags (ctxt->state, netbsd_flags);
  1435. if (grub_bsd_load (argc, argv) == GRUB_ERR_NONE)
  1436. {
  1437. if (is_elf_kernel)
  1438. {
  1439. grub_file_t file;
  1440. file = grub_file_open (argv[0], GRUB_FILE_TYPE_BSD_KERNEL);
  1441. if (! file)
  1442. return grub_errno;
  1443. if (is_64bit)
  1444. err = grub_netbsd_load_elf_meta64 (relocator, file, argv[0], &kern_end);
  1445. else
  1446. err = grub_netbsd_load_elf_meta32 (relocator, file, argv[0], &kern_end);
  1447. if (err)
  1448. return err;
  1449. }
  1450. {
  1451. char bootpath[GRUB_NETBSD_MAX_BOOTPATH_LEN];
  1452. char *name;
  1453. name = grub_strrchr (argv[0], '/');
  1454. if (name)
  1455. name++;
  1456. else
  1457. name = argv[0];
  1458. grub_memset (bootpath, 0, sizeof (bootpath));
  1459. grub_strncpy (bootpath, name, sizeof (bootpath) - 1);
  1460. grub_bsd_add_meta (NETBSD_BTINFO_BOOTPATH, bootpath, sizeof (bootpath));
  1461. }
  1462. if (ctxt->state[NETBSD_ROOT_ARG].set)
  1463. {
  1464. char root[GRUB_NETBSD_MAX_ROOTDEVICE_LEN];
  1465. grub_memset (root, 0, sizeof (root));
  1466. grub_strncpy (root, ctxt->state[NETBSD_ROOT_ARG].arg,
  1467. sizeof (root) - 1);
  1468. grub_bsd_add_meta (NETBSD_BTINFO_ROOTDEVICE, root, sizeof (root));
  1469. }
  1470. if (ctxt->state[NETBSD_SERIAL_ARG].set)
  1471. {
  1472. struct grub_netbsd_btinfo_serial serial;
  1473. const char *ptr;
  1474. grub_memset (&serial, 0, sizeof (serial));
  1475. grub_strcpy (serial.devname, "com");
  1476. serial.addr = grub_ns8250_hw_get_port (0);
  1477. serial.speed = 9600;
  1478. if (ctxt->state[NETBSD_SERIAL_ARG].arg)
  1479. {
  1480. ptr = ctxt->state[NETBSD_SERIAL_ARG].arg;
  1481. if (grub_memcmp (ptr, "com", sizeof ("com") - 1) == 0)
  1482. {
  1483. ptr += sizeof ("com") - 1;
  1484. serial.addr
  1485. = grub_ns8250_hw_get_port (grub_strtoul (ptr, &ptr, 0));
  1486. }
  1487. else
  1488. serial.addr = grub_strtoul (ptr, &ptr, 0);
  1489. if (grub_errno)
  1490. return grub_errno;
  1491. if (*ptr == ',')
  1492. {
  1493. ptr++;
  1494. serial.speed = grub_strtoul (ptr, &ptr, 0);
  1495. if (grub_errno)
  1496. return grub_errno;
  1497. }
  1498. }
  1499. grub_bsd_add_meta (NETBSD_BTINFO_CONSOLE, &serial, sizeof (serial));
  1500. }
  1501. else
  1502. {
  1503. struct grub_netbsd_btinfo_serial cons;
  1504. grub_memset (&cons, 0, sizeof (cons));
  1505. grub_strcpy (cons.devname, "pc");
  1506. grub_bsd_add_meta (NETBSD_BTINFO_CONSOLE, &cons, sizeof (cons));
  1507. }
  1508. grub_netbsd_add_boot_disk_and_wedge ();
  1509. grub_loader_set (grub_netbsd_boot, grub_bsd_unload, 0);
  1510. }
  1511. return grub_errno;
  1512. }
  1513. static grub_err_t
  1514. grub_cmd_freebsd_loadenv (grub_command_t cmd __attribute__ ((unused)),
  1515. int argc, char *argv[])
  1516. {
  1517. grub_file_t file = 0;
  1518. char *buf = 0, *curr, *next;
  1519. int len;
  1520. if (! grub_loader_is_loaded ())
  1521. return grub_error (GRUB_ERR_BAD_ARGUMENT,
  1522. N_("you need to load the kernel first"));
  1523. if (kernel_type != KERNEL_TYPE_FREEBSD)
  1524. return grub_error (GRUB_ERR_BAD_ARGUMENT,
  1525. "only FreeBSD supports environment");
  1526. if (argc == 0)
  1527. {
  1528. grub_error (GRUB_ERR_BAD_ARGUMENT, N_("filename expected"));
  1529. goto fail;
  1530. }
  1531. file = grub_file_open (argv[0], GRUB_FILE_TYPE_FREEBSD_ENV);
  1532. if ((!file) || (!file->size))
  1533. goto fail;
  1534. len = file->size;
  1535. buf = grub_malloc (len + 1);
  1536. if (!buf)
  1537. goto fail;
  1538. if (grub_file_read (file, buf, len) != len)
  1539. goto fail;
  1540. buf[len] = 0;
  1541. next = buf;
  1542. while (next)
  1543. {
  1544. char *p;
  1545. curr = next;
  1546. next = grub_strchr (curr, '\n');
  1547. if (next)
  1548. {
  1549. p = next - 1;
  1550. while (p > curr)
  1551. {
  1552. if ((*p != '\r') && (*p != ' ') && (*p != '\t'))
  1553. break;
  1554. p--;
  1555. }
  1556. if ((p > curr) && (*p == '"'))
  1557. p--;
  1558. *(p + 1) = 0;
  1559. next++;
  1560. }
  1561. if (*curr == '#')
  1562. continue;
  1563. p = grub_strchr (curr, '=');
  1564. if (!p)
  1565. continue;
  1566. *(p++) = 0;
  1567. if (*curr)
  1568. {
  1569. char *name;
  1570. if (*p == '"')
  1571. p++;
  1572. name = grub_xasprintf ("kFreeBSD.%s", curr);
  1573. if (!name)
  1574. goto fail;
  1575. if (grub_env_set (name, p))
  1576. {
  1577. grub_free (name);
  1578. goto fail;
  1579. }
  1580. grub_free (name);
  1581. }
  1582. }
  1583. fail:
  1584. grub_free (buf);
  1585. if (file)
  1586. grub_file_close (file);
  1587. return grub_errno;
  1588. }
  1589. static grub_err_t
  1590. grub_cmd_freebsd_module (grub_command_t cmd __attribute__ ((unused)),
  1591. int argc, char *argv[])
  1592. {
  1593. grub_file_t file = 0;
  1594. int modargc;
  1595. char **modargv;
  1596. const char *type;
  1597. grub_err_t err;
  1598. void *src;
  1599. if (! grub_loader_is_loaded ())
  1600. return grub_error (GRUB_ERR_BAD_ARGUMENT,
  1601. N_("you need to load the kernel first"));
  1602. if (kernel_type != KERNEL_TYPE_FREEBSD)
  1603. return grub_error (GRUB_ERR_BAD_ARGUMENT, "no FreeBSD loaded");
  1604. if (!is_elf_kernel)
  1605. return grub_error (GRUB_ERR_BAD_ARGUMENT,
  1606. "only ELF kernel supports module");
  1607. /* List the current modules if no parameter. */
  1608. if (!argc)
  1609. {
  1610. grub_freebsd_list_modules ();
  1611. return 0;
  1612. }
  1613. file = grub_file_open (argv[0], GRUB_FILE_TYPE_FREEBSD_MODULE);
  1614. if ((!file) || (!file->size))
  1615. goto fail;
  1616. {
  1617. grub_relocator_chunk_t ch;
  1618. err = grub_relocator_alloc_chunk_addr (relocator, &ch, kern_end,
  1619. file->size);
  1620. if (err)
  1621. goto fail;
  1622. src = get_virtual_current_address (ch);
  1623. }
  1624. grub_file_read (file, src, file->size);
  1625. if (grub_errno)
  1626. goto fail;
  1627. modargc = argc - 1;
  1628. modargv = argv + 1;
  1629. if (modargc && (! grub_memcmp (modargv[0], "type=", 5)))
  1630. {
  1631. type = &modargv[0][5];
  1632. modargc--;
  1633. modargv++;
  1634. }
  1635. else
  1636. type = FREEBSD_MODTYPE_RAW;
  1637. err = grub_freebsd_add_meta_module (argv[0], type, modargc, modargv,
  1638. kern_end, file->size);
  1639. if (err)
  1640. goto fail;
  1641. kern_end = ALIGN_PAGE (kern_end + file->size);
  1642. fail:
  1643. if (file)
  1644. grub_file_close (file);
  1645. return grub_errno;
  1646. }
  1647. static grub_err_t
  1648. grub_netbsd_module_load (char *filename, grub_uint32_t type)
  1649. {
  1650. grub_file_t file = 0;
  1651. void *src;
  1652. grub_err_t err;
  1653. file = grub_file_open (filename, GRUB_FILE_TYPE_NETBSD_MODULE);
  1654. if ((!file) || (!file->size))
  1655. goto fail;
  1656. {
  1657. grub_relocator_chunk_t ch;
  1658. err = grub_relocator_alloc_chunk_addr (relocator, &ch, kern_end,
  1659. file->size);
  1660. if (err)
  1661. goto fail;
  1662. src = get_virtual_current_address (ch);
  1663. }
  1664. grub_file_read (file, src, file->size);
  1665. if (grub_errno)
  1666. goto fail;
  1667. err = grub_netbsd_add_meta_module (filename, type, kern_end, file->size);
  1668. if (err)
  1669. goto fail;
  1670. kern_end = ALIGN_PAGE (kern_end + file->size);
  1671. fail:
  1672. if (file)
  1673. grub_file_close (file);
  1674. return grub_errno;
  1675. }
  1676. static grub_err_t
  1677. grub_cmd_netbsd_module (grub_command_t cmd,
  1678. int argc, char *argv[])
  1679. {
  1680. grub_uint32_t type;
  1681. if (! grub_loader_is_loaded ())
  1682. return grub_error (GRUB_ERR_BAD_ARGUMENT,
  1683. N_("you need to load the kernel first"));
  1684. if (kernel_type != KERNEL_TYPE_NETBSD)
  1685. return grub_error (GRUB_ERR_BAD_ARGUMENT, "no NetBSD loaded");
  1686. if (!is_elf_kernel)
  1687. return grub_error (GRUB_ERR_BAD_ARGUMENT,
  1688. "only ELF kernel supports module");
  1689. /* List the current modules if no parameter. */
  1690. if (!argc)
  1691. {
  1692. grub_netbsd_list_modules ();
  1693. return 0;
  1694. }
  1695. if (grub_strcmp (cmd->name, "knetbsd_module_elf") == 0)
  1696. type = GRUB_NETBSD_MODULE_ELF;
  1697. else
  1698. type = GRUB_NETBSD_MODULE_RAW;
  1699. return grub_netbsd_module_load (argv[0], type);
  1700. }
  1701. static grub_err_t
  1702. grub_cmd_freebsd_module_elf (grub_command_t cmd __attribute__ ((unused)),
  1703. int argc, char *argv[])
  1704. {
  1705. grub_file_t file = 0;
  1706. grub_err_t err;
  1707. if (! grub_loader_is_loaded ())
  1708. return grub_error (GRUB_ERR_BAD_ARGUMENT,
  1709. N_("you need to load the kernel first"));
  1710. if (kernel_type != KERNEL_TYPE_FREEBSD)
  1711. return grub_error (GRUB_ERR_BAD_ARGUMENT,
  1712. "only FreeBSD supports module");
  1713. if (! is_elf_kernel)
  1714. return grub_error (GRUB_ERR_BAD_ARGUMENT,
  1715. "only ELF kernel supports module");
  1716. /* List the current modules if no parameter. */
  1717. if (! argc)
  1718. {
  1719. grub_freebsd_list_modules ();
  1720. return 0;
  1721. }
  1722. file = grub_file_open (argv[0], GRUB_FILE_TYPE_FREEBSD_MODULE_ELF);
  1723. if (!file)
  1724. return grub_errno;
  1725. if (!file->size)
  1726. {
  1727. grub_file_close (file);
  1728. return grub_errno;
  1729. }
  1730. if (is_64bit)
  1731. err = grub_freebsd_load_elfmodule_obj64 (relocator, file,
  1732. argc, argv, &kern_end);
  1733. else
  1734. err = grub_freebsd_load_elfmodule32 (relocator, file,
  1735. argc, argv, &kern_end);
  1736. grub_file_close (file);
  1737. return err;
  1738. }
  1739. static grub_err_t
  1740. grub_cmd_openbsd_ramdisk (grub_command_t cmd __attribute__ ((unused)),
  1741. int argc, char *args[])
  1742. {
  1743. grub_file_t file;
  1744. grub_size_t size;
  1745. if (argc != 1)
  1746. return grub_error (GRUB_ERR_BAD_ARGUMENT, N_("filename expected"));
  1747. if (! grub_loader_is_loaded ())
  1748. return grub_error (GRUB_ERR_BAD_ARGUMENT,
  1749. N_("you need to load the kernel first"));
  1750. if (kernel_type != KERNEL_TYPE_OPENBSD)
  1751. return grub_error (GRUB_ERR_BAD_OS, "no kOpenBSD loaded");
  1752. if (!openbsd_ramdisk.max_size)
  1753. return grub_error (GRUB_ERR_BAD_OS, "your kOpenBSD doesn't support ramdisk");
  1754. file = grub_file_open (args[0], GRUB_FILE_TYPE_OPENBSD_RAMDISK);
  1755. if (! file)
  1756. return grub_errno;
  1757. size = grub_file_size (file);
  1758. if (size > openbsd_ramdisk.max_size)
  1759. {
  1760. grub_file_close (file);
  1761. return grub_error (GRUB_ERR_BAD_OS, "your kOpenBSD supports ramdisk only"
  1762. " up to %" PRIuGRUB_SIZE " bytes, however you supplied"
  1763. " a %" PRIuGRUB_SIZE " bytes one",
  1764. openbsd_ramdisk.max_size, size);
  1765. }
  1766. if (grub_file_read (file, openbsd_ramdisk.target, size)
  1767. != (grub_ssize_t) (size))
  1768. {
  1769. grub_file_close (file);
  1770. if (!grub_errno)
  1771. grub_error (GRUB_ERR_BAD_OS, N_("premature end of file %s"), args[0]);
  1772. return grub_errno;
  1773. }
  1774. grub_memset (openbsd_ramdisk.target + size, 0,
  1775. openbsd_ramdisk.max_size - size);
  1776. *openbsd_ramdisk.size = ALIGN_UP (size, 512);
  1777. return GRUB_ERR_NONE;
  1778. }
  1779. static grub_extcmd_t cmd_freebsd, cmd_openbsd, cmd_netbsd;
  1780. static grub_command_t cmd_freebsd_loadenv, cmd_freebsd_module;
  1781. static grub_command_t cmd_netbsd_module, cmd_freebsd_module_elf;
  1782. static grub_command_t cmd_netbsd_module_elf, cmd_openbsd_ramdisk;
  1783. GRUB_MOD_INIT (bsd)
  1784. {
  1785. /* Net and OpenBSD kernels are often compressed. */
  1786. grub_dl_load ("gzio");
  1787. cmd_freebsd = grub_register_extcmd ("kfreebsd", grub_cmd_freebsd, 0,
  1788. N_("FILE"), N_("Load kernel of FreeBSD."),
  1789. freebsd_opts);
  1790. cmd_openbsd = grub_register_extcmd ("kopenbsd", grub_cmd_openbsd, 0,
  1791. N_("FILE"), N_("Load kernel of OpenBSD."),
  1792. openbsd_opts);
  1793. cmd_netbsd = grub_register_extcmd ("knetbsd", grub_cmd_netbsd, 0,
  1794. N_("FILE"), N_("Load kernel of NetBSD."),
  1795. netbsd_opts);
  1796. cmd_freebsd_loadenv =
  1797. grub_register_command ("kfreebsd_loadenv", grub_cmd_freebsd_loadenv,
  1798. 0, N_("Load FreeBSD env."));
  1799. cmd_freebsd_module =
  1800. grub_register_command ("kfreebsd_module", grub_cmd_freebsd_module,
  1801. 0, N_("Load FreeBSD kernel module."));
  1802. cmd_netbsd_module =
  1803. grub_register_command ("knetbsd_module", grub_cmd_netbsd_module,
  1804. 0, N_("Load NetBSD kernel module."));
  1805. cmd_netbsd_module_elf =
  1806. grub_register_command ("knetbsd_module_elf", grub_cmd_netbsd_module,
  1807. 0, N_("Load NetBSD kernel module (ELF)."));
  1808. cmd_freebsd_module_elf =
  1809. grub_register_command ("kfreebsd_module_elf", grub_cmd_freebsd_module_elf,
  1810. 0, N_("Load FreeBSD kernel module (ELF)."));
  1811. cmd_openbsd_ramdisk = grub_register_command ("kopenbsd_ramdisk",
  1812. grub_cmd_openbsd_ramdisk, 0,
  1813. /* TRANSLATORS: ramdisk isn't identifier,
  1814. it can be translated. */
  1815. N_("Load kOpenBSD ramdisk."));
  1816. my_mod = mod;
  1817. }
  1818. GRUB_MOD_FINI (bsd)
  1819. {
  1820. grub_unregister_extcmd (cmd_freebsd);
  1821. grub_unregister_extcmd (cmd_openbsd);
  1822. grub_unregister_extcmd (cmd_netbsd);
  1823. grub_unregister_command (cmd_freebsd_loadenv);
  1824. grub_unregister_command (cmd_freebsd_module);
  1825. grub_unregister_command (cmd_netbsd_module);
  1826. grub_unregister_command (cmd_freebsd_module_elf);
  1827. grub_unregister_command (cmd_netbsd_module_elf);
  1828. grub_unregister_command (cmd_openbsd_ramdisk);
  1829. grub_bsd_unload ();
  1830. }