grub-dev.texi 91 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646164716481649165016511652165316541655165616571658165916601661166216631664166516661667166816691670167116721673167416751676167716781679168016811682168316841685168616871688168916901691169216931694169516961697169816991700170117021703170417051706170717081709171017111712171317141715171617171718171917201721172217231724172517261727172817291730173117321733173417351736173717381739174017411742174317441745174617471748174917501751175217531754175517561757175817591760176117621763176417651766176717681769177017711772177317741775177617771778177917801781178217831784178517861787178817891790179117921793179417951796179717981799180018011802180318041805180618071808180918101811181218131814181518161817181818191820182118221823182418251826182718281829183018311832183318341835183618371838183918401841184218431844184518461847184818491850185118521853185418551856185718581859186018611862186318641865186618671868186918701871187218731874187518761877187818791880188118821883188418851886188718881889189018911892189318941895189618971898189919001901190219031904190519061907190819091910191119121913191419151916191719181919192019211922192319241925192619271928192919301931193219331934193519361937193819391940194119421943194419451946194719481949195019511952195319541955195619571958195919601961196219631964196519661967196819691970197119721973197419751976197719781979198019811982198319841985198619871988198919901991199219931994199519961997199819992000200120022003200420052006200720082009201020112012201320142015201620172018201920202021202220232024202520262027202820292030203120322033203420352036203720382039204020412042204320442045204620472048204920502051205220532054205520562057205820592060206120622063206420652066206720682069207020712072207320742075207620772078207920802081208220832084208520862087208820892090209120922093209420952096209720982099210021012102210321042105210621072108210921102111211221132114211521162117211821192120212121222123212421252126212721282129213021312132213321342135213621372138213921402141214221432144214521462147214821492150215121522153215421552156215721582159216021612162216321642165216621672168216921702171217221732174217521762177217821792180218121822183218421852186218721882189219021912192219321942195219621972198219922002201220222032204220522062207220822092210221122122213221422152216221722182219222022212222222322242225222622272228222922302231223222332234223522362237223822392240224122422243224422452246224722482249225022512252225322542255225622572258225922602261226222632264226522662267226822692270227122722273227422752276227722782279228022812282228322842285228622872288228922902291229222932294229522962297229822992300230123022303230423052306230723082309231023112312231323142315231623172318231923202321232223232324232523262327232823292330233123322333233423352336233723382339234023412342234323442345234623472348234923502351235223532354235523562357235823592360236123622363236423652366236723682369237023712372237323742375237623772378237923802381238223832384238523862387238823892390239123922393239423952396239723982399240024012402240324042405240624072408240924102411241224132414
  1. \input texinfo
  2. @c -*-texinfo-*-
  3. @c %**start of header
  4. @setfilename grub-dev.info
  5. @include version-dev.texi
  6. @settitle GNU GRUB Developers Manual @value{VERSION}
  7. @c Unify all our little indices for now.
  8. @syncodeindex fn cp
  9. @syncodeindex vr cp
  10. @syncodeindex ky cp
  11. @syncodeindex pg cp
  12. @syncodeindex tp cp
  13. @c %**end of header
  14. @footnotestyle separate
  15. @paragraphindent 3
  16. @finalout
  17. @copying
  18. This developer manual is for GNU GRUB (version @value{VERSION},
  19. @value{UPDATED}).
  20. Copyright @copyright{} 1999,2000,2001,2002,2004,2005,2006,2008,2009,2010,2011 Free Software Foundation, Inc.
  21. @quotation
  22. Permission is granted to copy, distribute and/or modify this document
  23. under the terms of the GNU Free Documentation License, Version 1.2 or
  24. any later version published by the Free Software Foundation; with no
  25. Invariant Sections.
  26. @end quotation
  27. @end copying
  28. @dircategory Kernel
  29. @direntry
  30. * grub-dev: (grub-dev). The GRand Unified Bootloader Dev
  31. @end direntry
  32. @setchapternewpage odd
  33. @titlepage
  34. @sp 10
  35. @title the GNU GRUB developer manual
  36. @subtitle The GRand Unified Bootloader, version @value{VERSION}, @value{UPDATED}.
  37. @author Yoshinori K. Okuji
  38. @author Colin D Bennett
  39. @author Vesa Jääskeläinen
  40. @author Colin Watson
  41. @author Robert Millan
  42. @author Carles Pina
  43. @c The following two commands start the copyright page.
  44. @page
  45. @vskip 0pt plus 1filll
  46. @insertcopying
  47. @end titlepage
  48. @c Output the table of contents at the beginning.
  49. @contents
  50. @finalout
  51. @headings double
  52. @ifnottex
  53. @node Top
  54. @top GNU GRUB developer manual
  55. This is the developer documentation of GNU GRUB, the GRand Unified Bootloader,
  56. a flexible and powerful boot loader program for a wide range of
  57. architectures.
  58. This edition documents version @value{VERSION}.
  59. @insertcopying
  60. @end ifnottex
  61. @menu
  62. * Getting the source code::
  63. * Coding style::
  64. * Finding your way around::
  65. * Contributing Changes::
  66. * Setting up and running test suite::
  67. * Updating External Code::
  68. * Debugging::
  69. * Porting::
  70. * Error Handling::
  71. * Stack and heap size::
  72. * BIOS port memory map::
  73. * Video Subsystem::
  74. * PFF2 Font File Format::
  75. * Graphical Menu Software Design::
  76. * Verifiers framework::
  77. * Lockdown framework::
  78. * Copying This Manual:: Copying This Manual
  79. * Index::
  80. @end menu
  81. @node Getting the source code
  82. @chapter Getting the source code
  83. GRUB is maintained using the @uref{https://git-scm.com/book/en/v2,
  84. GIT revision control system}. To fetch:
  85. @example
  86. git clone git://git.sv.gnu.org/grub.git
  87. @end example
  88. Web access is available under
  89. @example
  90. http://git.savannah.gnu.org/cgit/grub.git/
  91. @end example
  92. The branches available are:
  93. @table @samp
  94. @item master
  95. Main development branch.
  96. @item grub-legacy
  97. GRUB 0.97 codebase. Kept for reference and legal reasons
  98. @item multiboot
  99. Multiboot specfication
  100. @item multiboot2
  101. Multiboot2 specfication
  102. @item developer branches
  103. Prefixed with developer name. Every developer of a team manages his own branches.
  104. Developer branches do not need changelog entries.
  105. @end table
  106. Once you have used @kbd{git clone} to fetch an initial copy of a branch, you
  107. can use @kbd{git pull} to keep it up to date. If you have modified your
  108. local version, you may need to resolve conflicts when pulling.
  109. @node Coding style
  110. @chapter Coding style
  111. @c By YoshinoriOkuji, VesaJääskeläinen and ColinBennett
  112. Basically we follow the @uref{http://www.gnu.org/prep/standards_toc.html, GNU Coding Standards}. We define additional conventions for GRUB here.
  113. @menu
  114. * Naming Conventions::
  115. * Functions::
  116. * Variables::
  117. * Types::
  118. * Macros::
  119. * Comments::
  120. * Multi-Line Comments::
  121. @end menu
  122. @node Naming Conventions
  123. @section Naming Conventions
  124. All global symbols (i.e. functions, variables, types, and macros) must have the prefix grub_ or GRUB_. The all capital form is used only by macros.
  125. @node Functions
  126. @section Functions
  127. If a function is global, its name must be prefixed with grub_ and must consist of only small letters. If the function belongs to a specific function module, the name must also be prefixed with the module name. For example, if a function is for file systems, its name is prefixed with grub_fs_. If a function is for FAT file system but not for all file systems, its name is prefixed with grub_fs_fat_. The hierarchy is noted this way.
  128. After a prefix, a function name must start with a verb (such as get or is). It must not be a noun. Some kind of abbreviation is permitted, as long as it wouldn't make code less readable (e.g. init).
  129. If a function is local, its name may not start with any prefix. It must start with a verb.
  130. @node Variables
  131. @section Variables
  132. The rule is mostly the same as functions, as noted above. If a variable is global, its name must be prefixed with grub_ and must consist of only small letters. If the variable belongs to a specific function module, the name must also be prefixed with the module name. For example, if a function is for dynamic loading, its name is prefixed with grub_dl_. If a variable is for ELF but not for all dynamic loading systems, its name is prefixed with grub_dl_elf_.
  133. After a prefix, a variable name must start with a noun or an adjective (such as name or long) and it should end with a noun. Some kind of abbreviation is permitted, as long as it wouldn't make code less readable (e.g. i18n).
  134. If a variable is global in the scope of a single file (i.e. it is declared with static), its name may not start with any prefix. It must start with a noun or an adjective.
  135. If a variable is local, you may choose any shorter name, as long as it wouldn't make code less readable (e.g. i).
  136. @node Types
  137. @section Types
  138. The name of a type must be prefixed with grub_ and must consist of only small letters. If the type belongs to a specific function module, the name must also be prefixed with the module name. For example, if a type is for OS loaders, its name is prefixed with grub_loader_. If a type is for Multiboot but not for all OS loaders, its name is prefixed with grub_loader_linux_.
  139. The name must be suffixed with _t, to emphasize the fact that it is a type but not a variable or a function.
  140. @node Macros
  141. @section Macros
  142. If a macro is global, its name must be prefixed with GRUB_ and must consist of only large letters. Other rules are the same as functions or variables, depending on whether a macro is used like a function or a variable.
  143. @node Comments
  144. @section Comments
  145. All comments shall be C-style comments, of the form @samp{/* @dots{} */}.
  146. A comment can be placed immediately preceding the entity it describes or it
  147. can be placed together with code, variable declarations, or other non-comment
  148. entities. However, it is recommended to not mix various forms especially in
  149. types/structs descriptions.
  150. Acceptable:
  151. @example
  152. /* The page # that is the front buffer. */
  153. int displayed_page;
  154. @end example
  155. @example
  156. int render_page; /* The page # that is the back buffer. */
  157. @end example
  158. @node Multi-Line Comments
  159. @section Multi-Line Comments
  160. Comments spanning multiple lines shall be formatted with all lines after the
  161. first aligned with the first line. Asterisk characters should be repeated at
  162. the start of each subsequent line.
  163. Acceptable:
  164. @example
  165. /*
  166. * This is a comment
  167. * which spans multiple lines.
  168. * It is long.
  169. */
  170. @end example
  171. Unacceptable:
  172. @example
  173. /* This is a comment
  174. which spans multiple lines.
  175. It is long. */
  176. @end example
  177. @example
  178. /*
  179. * This is a comment
  180. * which spans multiple lines.
  181. * It is long. */
  182. @end example
  183. @example
  184. /* This is a comment
  185. * which spans multiple lines.
  186. * It is long.
  187. */
  188. @end example
  189. In particular first unacceptable form makes comment difficult to distinguish
  190. from the code itself. Especially if it contains the code snippets and/or is
  191. long. So, its usage is disallowed.
  192. @node Finding your way around
  193. @chapter Finding your way around
  194. Here is a brief map of the GRUB code base.
  195. GRUB uses Autoconf and Automake, with most of the Automake input generated
  196. by a Python script. The top-level build rules are in @file{configure.ac},
  197. @file{grub-core/Makefile.core.def}, and @file{Makefile.util.def}. Each
  198. block in a @file{*.def} file represents a build target, and specifies the
  199. source files used to build it on various platforms. The @file{*.def} files
  200. are processed into Automake input by @file{gentpl.py} (which you only need
  201. to look at if you are extending the build system). If you are adding a new
  202. module which follows an existing pattern, such as a new command or a new
  203. filesystem implementation, it is usually easiest to grep
  204. @file{grub-core/Makefile.core.def} and @file{Makefile.util.def} for an
  205. existing example of that pattern to find out where it should be added.
  206. In general, code that may be run at boot time is in a subdirectory of
  207. @file{grub-core}, while code that is only run from within a full operating
  208. system is in a subdirectory of the top level.
  209. Low-level boot code, such as the MBR implementation on PC BIOS systems, is
  210. in the @file{grub-core/boot/} directory.
  211. The GRUB kernel is in @file{grub-core/kern/}. This contains core facilities
  212. such as the device, disk, and file frameworks, environment variable
  213. handling, list processing, and so on. The kernel should contain enough to
  214. get up to a rescue prompt. Header files for kernel facilities, among
  215. others, are in @file{include/}.
  216. Terminal implementations are in @file{grub-core/term/}.
  217. Disk access code is spread across @file{grub-core/disk/} (for accessing the
  218. disk devices themselves), @file{grub-core/partmap/} (for interpreting
  219. partition table data), and @file{grub-core/fs/} (for accessing filesystems).
  220. Note that, with the odd specialised exception, GRUB only contains code to
  221. @emph{read} from filesystems and tries to avoid containing any code to
  222. @emph{write} to filesystems; this lets us confidently assure users that GRUB
  223. cannot be responsible for filesystem corruption.
  224. PCI and USB bus handling is in @file{grub-core/bus/}.
  225. Video handling code is in @file{grub-core/video/}. The graphical menu
  226. system uses this heavily, but is in a separate directory,
  227. @file{grub-core/gfxmenu/}.
  228. Most commands are implemented by files in @file{grub-core/commands/}, with
  229. the following exceptions:
  230. @itemize
  231. @item
  232. A few core commands live in @file{grub-core/kern/corecmd.c}.
  233. @item
  234. Commands related to normal mode live under @file{grub-core/normal/}.
  235. @item
  236. Commands that load and boot kernels live under @file{grub-core/loader/}.
  237. @item
  238. The @samp{loopback} command is really a disk device, and so lives in
  239. @file{grub-core/disk/loopback.c}.
  240. @item
  241. The @samp{gettext} command lives under @file{grub-core/gettext/}.
  242. @item
  243. The @samp{loadfont} and @samp{lsfonts} commands live under
  244. @file{grub-core/font/}.
  245. @item
  246. The @samp{serial}, @samp{terminfo}, and @samp{background_image} commands
  247. live under @file{grub-core/term/}.
  248. @item
  249. The @samp{efiemu_*} commands live under @file{grub-core/efiemu/}.
  250. @item
  251. OS-dependent code should be under @file{grub-core/osdep/}
  252. @item
  253. Utility programs meant to be run from a full operating system
  254. (except OS-dependent code mentioned previously) are in @file{util/}.
  255. @end itemize
  256. There are a few other special-purpose exceptions; grep for them if they
  257. matter to you.
  258. @node Contributing Changes
  259. @chapter Contributing changes
  260. @c By YoshinoriOkuji, VesaJääskeläinen, ColinWatson
  261. Contributing changes to GRUB 2 is welcomed activity. However we have a
  262. bit of control what kind of changes will be accepted to GRUB 2.
  263. Therefore it is important to discuss your changes on grub-devel mailing list
  264. (see MailingLists). On this page there are some basic details on the
  265. development process and activities.
  266. First of all you should come up with the idea yourself what you want to
  267. contribute. If you do not have that beforehand you are advised to study this
  268. manual and try GRUB 2 out to see what you think is missing from there.
  269. Here are additional pointers:
  270. @itemize
  271. @item @uref{https://savannah.gnu.org/task/?group=grub, GRUB's Task Tracker}
  272. @item @uref{https://savannah.gnu.org/bugs/?group=grub, GRUB's Bug Tracker}
  273. @end itemize
  274. If you intended to make changes to GRUB Legacy (<=0.97) those are not accepted
  275. anymore.
  276. @menu
  277. * Getting started::
  278. * Typical Developer Experience::
  279. * When you are approved for write access to project's files::
  280. @end menu
  281. @node Getting started
  282. @section Getting started
  283. @itemize
  284. @item Always use latest GRUB 2 source code. So get that first.
  285. For developers it is recommended always to use the newest development version of GRUB 2. If development takes a long period of time, please remember to keep in sync with newest developments regularly so it is much easier to integrate your change in the future. GRUB 2 is being developed in a GIT repository.
  286. Please check Savannah's GRUB project page for details how to get newest git:
  287. @uref{https://savannah.gnu.org/git/?group=grub, GRUB 2 git Repository}
  288. @item Compile it and try it out.
  289. It is always good idea to first see that things work somehow and after that
  290. to start to implement new features or develop fixes to bugs.
  291. @item Study the code.
  292. There are sometimes odd ways to do things in GRUB 2 code base.
  293. This is mainly related to limited environment where GRUB 2 is being executed.
  294. You usually do not need to understand it all so it is better to only try to
  295. look at places that relates to your work. Please do not hesitate to ask for
  296. help if there is something that you do not understand.
  297. @item Develop a new feature.
  298. Now that you know what to do and how it should work in GRUB 2 code base, please
  299. be free to develop it. If you have not so far announced your idea on grub-devel
  300. mailing list, please do it now. This is to make sure you are not wasting your
  301. time working on the solution that will not be integrated to GRUB 2 code base.
  302. You might want to study our coding style before starting development so you
  303. do not need to change much of the code when your patch is being reviewed.
  304. (see @ref{Coding style})
  305. For every accepted patch there has to exist a ChangeLog entry. Our ChangeLog
  306. consist of changes within source code and are not describing about what the
  307. change logically does. Please see examples from previous entries.
  308. Also remember that GRUB 2 is licensed under GPLv3 license and that usually
  309. means that you are not allowed to copy pieces of code from other projects.
  310. Even if the source project's license would be compatible with GPLv3, please
  311. discuss it beforehand on grub-devel mailing list.
  312. @item Test your change.
  313. Test that your change works properly. Try it out a couple of times, preferably on different systems, and try to find problems with it.
  314. @item Publish your change.
  315. When you are happy with your change, first make sure it is compilable with
  316. latest development version of GRUB 2. After that please send a patch to
  317. grub-devel for review. Please describe in your email why you made the change,
  318. what it changes and so on. Please be prepared to receive even discouraging
  319. comments about your patch. There is usually at least something that needs
  320. to be improved in every patch.
  321. Please use unified diff to make your patch (good match of arguments for diff is @samp{-pruN}).
  322. @item Respond to received feedback.
  323. If you are asked to modify your patch, please do that and resubmit it for
  324. review. If your change is large you are required to submit a copyright
  325. agreement to FSF. Please keep in mind that if you are asked to submit
  326. for copyright agreement, process can take some time and is mandatory
  327. in order to get your changes integrated.
  328. If you are not on grub-devel to respond to questions, most likely your patch
  329. will not be accepted. Also if problems arise from your changes later on,
  330. it would be preferable that you also fix the problem. So stay around
  331. for a while.
  332. @item Your patch is accepted.
  333. Good job! Your patch will now be integrated into GRUB 2 mainline, and if it didn't break anything it will be publicly available in the next release.
  334. Now you are welcome to do further improvements :)
  335. @end itemize
  336. @node Typical Developer Experience
  337. @section Typical Developer Experience
  338. The typical experience for a developer in this project is the following:
  339. @enumerate
  340. @item You find yourself wanting to do something (e.g. fixing a bug).
  341. @item You show some result in the mailing list or the IRC.
  342. @item You are getting to be known to other developers.
  343. @item You accumulate significant amount of contribution, so copyright assignment is processed.
  344. @item You are free to check in your changes on your own, legally speaking.
  345. @end enumerate
  346. At this point, it is rather annoying that you ought to ask somebody else every
  347. change to be checked in. For efficiency, it is far better, if you can commit
  348. it yourself. Therefore, our policy is to give you the write permission to our
  349. official repository, once you have shown your skill and will,
  350. and the FSF clerks have dealt with your copyright assignment.
  351. @node When you are approved for write access to project's files
  352. @section When you are approved for write access to project's files
  353. As you might know, GRUB is hosted on
  354. @uref{https://savannah.gnu.org/projects/grub, Savannah}, thus the membership
  355. is managed by Savannah. This means that, if you want to be a member of this
  356. project:
  357. @enumerate
  358. @item You need to create your own account on Savannah.
  359. @item You can submit ``Request for Inclusion'' from ``My Groups'' on Savannah.
  360. @end enumerate
  361. Then, one of the admins can approve your request, and you will be a member.
  362. If you don't want to use the Savannah interface to submit a request, you can
  363. simply notify the admins by email or something else, alternatively. But you
  364. still need to create an account beforehand.
  365. NOTE: we sometimes receive a ``Request for Inclusion'' from an unknown person.
  366. In this case, the request would be just discarded, since it is too dangerous
  367. to allow a stranger to be a member, which automatically gives him a commit
  368. right to the repository, both for a legal reason and for a technical reason.
  369. If your intention is to just get started, please do not submit a inclusion
  370. request. Instead, please subscribe to the mailing list, and communicate first
  371. (e.g. sending a patch, asking a question, commenting on another message...).
  372. @node Setting up and running test suite
  373. @chapter Setting up and running test suite
  374. GRUB is basically a tiny operating system with read support for many file
  375. systems and which has been ported to a variety of architectures. As such, its
  376. test suite has quite a few dependencies required to fully run the suite.
  377. These dependencies are currently documented in the
  378. @uref{https://git.savannah.gnu.org/cgit/grub.git/tree/INSTALL, INSTALL}
  379. file in the source repository. Once installed, the test suite can be started
  380. by running the @command{make check} command from the GRUB build directory.
  381. @node Updating External Code
  382. @chapter Updating external code
  383. GRUB includes some code from other projects, and it is sometimes necessary
  384. to update it.
  385. @menu
  386. * Gnulib::
  387. * jsmn::
  388. * minilzo::
  389. @end menu
  390. @node Gnulib
  391. @section Gnulib
  392. Gnulib is a source code library that provides basic functionality to
  393. programs and libraries. Many software packages make use of Gnulib
  394. to avoid reinventing the portability wheel.
  395. GRUB imports Gnulib using its @command{bootstrap} utility, identifying a
  396. particular Git commit in @file{bootstrap.conf}. To upgrade to a new Gnulib
  397. commit, set @code{GNULIB_REVISION} in @file{bootstrap.conf} to the new commit
  398. ID, then run @kbd{./bootstrap} and whatever else you need to make sure it
  399. works. Check for changes to Gnulib's @file{NEWS} file between the old and new
  400. commits; in some cases it will be necessary to adjust GRUB to match. You may
  401. also need to update the patches in @file{grub-core/lib/gnulib-patches/}.
  402. To add a new Gnulib module or remove one that is no longer needed, change
  403. @code{gnulib_modules} in @file{bootstrap.conf}. Again, run @kbd{./bootstrap}
  404. and whatever else you need to make sure it works.
  405. Bootstrapping from an older distribution containing gettext version < 0.18.3,
  406. will require a patch similar to this to be applied first before running the
  407. @command{./bootstrap} utility:
  408. @example
  409. diff --git a/bootstrap.conf b/bootstrap.conf
  410. index 988dda0..a3193a9 100644
  411. --- a/bootstrap.conf
  412. +++ b/bootstrap.conf
  413. @@ -67,7 +67,7 @@ SKIP_PO=t
  414. buildreq="\
  415. autoconf 2.63
  416. automake 1.11
  417. -gettext 0.18.3
  418. +gettext 0.17
  419. git 1.5.5
  420. tar -
  421. "
  422. diff --git a/configure.ac b/configure.ac
  423. index 08b518f..99f5b36 100644
  424. --- a/configure.ac
  425. +++ b/configure.ac
  426. @@ -362,7 +362,7 @@ AC_CHECK_PROG(HAVE_CXX, $CXX, yes, no)
  427. AC_GNU_SOURCE
  428. AM_GNU_GETTEXT([external])
  429. -AM_GNU_GETTEXT_VERSION([0.18.3])
  430. +AM_GNU_GETTEXT_VERSION([0.17])
  431. AC_SYS_LARGEFILE
  432. # Identify characteristics of the host architecture.
  433. @end example
  434. It will also be necessary to adjust the patches in
  435. @file{po/gettext-patches/} to apply to an older version of gettext.
  436. @node jsmn
  437. @section jsmn
  438. jsmn is a minimalistic JSON parser which is implemented in a single header file
  439. @file{jsmn.h}. To import a different version of the jsmn parser, you may simply
  440. download the @file{jsmn.h} header from the desired tag or commit to the target
  441. directory:
  442. @example
  443. curl -L https://raw.githubusercontent.com/zserge/jsmn/v1.1.0/jsmn.h \
  444. -o grub-core/lib/json/jsmn.h
  445. @end example
  446. @node minilzo
  447. @section minilzo
  448. miniLZO is a very lightweight subset of the LZO library intended for easy
  449. inclusion in other projects. It is generated automatically from the LZO
  450. source code and contains the most important LZO functions.
  451. To upgrade to a new version of the miniLZO library, download the release
  452. tarball and copy the files into the target directory:
  453. @example
  454. curl -L -O https://www.oberhumer.com/opensource/lzo/download/minilzo-2.10.tar.gz
  455. tar -zxf minilzo-2.10.tar.gz
  456. rm minilzo-2.10/testmini.c
  457. rm -r grub-core/lib/minilzo/*
  458. cp minilzo-2.10/*.[hc] grub-core/lib/minilzo
  459. rm -r minilzo-2.10*
  460. @end example
  461. @node Debugging
  462. @chapter Debugging
  463. GRUB2 can be difficult to debug because it runs on the bare-metal and thus
  464. does not have the debugging facilities normally provided by an operating
  465. system. This chapter aims to provide useful information on some ways to
  466. debug GRUB2 for some architectures. It by no means intends to be exhaustive.
  467. The focus will be one x86_64 and i386 architectures. Luckily for some issues
  468. virtual machines have made the ability to debug GRUB2 much easier, and this
  469. chapter will focus debugging via the QEMU virtual machine. We will not be
  470. going over debugging of the userland tools (eg. grub-install), there are
  471. many tutorials on debugging programs in userland.
  472. You will need GDB and the QEMU binaries for your system, on Debian these
  473. can be installed with the @samp{gdb} and @samp{qemu-system-x86} packages.
  474. Also it is assumed that you have already successfully compiled GRUB2 from
  475. source for the target specified in the section below and have some
  476. familiarity with GDB. When GRUB2 is built it will create many different
  477. binaries. The ones of concern will be in the @file{grub-core}
  478. directory of the GRUB2 build dir. To aide in debugging we will want the
  479. debugging symbols generated during the build because these symbols are not
  480. kept in the binaries which get installed to the boot location. The build
  481. process outputs two sets of binaries, one without symbols which gets executed
  482. at boot, and another set of ELF images with debugging symbols. The built
  483. images with debugging symbols will have a @file{.image} suffix, and the ones
  484. without a @file{.img} suffix. Similarly, loadable modules with debugging
  485. symbols will have a @file{.module} suffix, and ones without a @file{.mod}
  486. suffix. In the case of the kernel the binary with symbols is named
  487. @file{kernel.exec}.
  488. In the following sections, information will be provided on debugging on
  489. various targets using @command{gdb} and the @samp{gdb_grub} GDB script.
  490. @menu
  491. * i386-pc::
  492. * x86_64-efi::
  493. @end menu
  494. @node i386-pc
  495. @section i386-pc
  496. The i386-pc target is a good place to start when first debugging GRUB2
  497. because in some respects it's easier than EFI platforms. The reason being
  498. that the initial load address is always known in advance. To start
  499. debugging GRUB2 first QEMU must be started in GDB stub mode. The following
  500. command is a simple illustration:
  501. @example
  502. qemu-system-i386 -drive file=disk.img,format=raw \
  503. -device virtio-scsi-pci,id=scsi0 -S -s
  504. @end example
  505. This will start a QEMU instance booting from @file{disk.img}. It will pause
  506. at start waiting for a GDB instance to attach to it. You should change
  507. @file{disk.img} to something more appropriate. A block device can be used,
  508. but you may need to run QEMU as a privileged user.
  509. To connect to this QEMU instance with GDB, the @code{target remote} GDB
  510. command must be used. We also need to load a binary image, preferably with
  511. symbols. This can be done using the GDB command @code{file kernel.exec}, if
  512. GDB is started from the @file{grub-core} directory in the GRUB2 build
  513. directory. GRUB2 developers have made this more simple by including a GDB
  514. script which does much of the setup. This file is at @file{grub-core/gdb_grub}
  515. in the build directory and is also installed via @command{make install}.
  516. When using a pre-built GRUB, the distribution may have a package which installs
  517. this GDB script along with debug symbol binaries, such as Debian's
  518. @samp{grub-pc-dbg} package. The GDB script is intended to be used
  519. like so, assuming that @samp{/path/to/script} is the path to the directory
  520. containing the gdb_grub script and debug symbol files:
  521. @example
  522. cd $(dirname /path/to/script/gdb_grub)
  523. gdb -x gdb_grub
  524. @end example
  525. Once GDB has been started with the @file{gdb_grub} script it will
  526. automatically connect to the QEMU instance. You can then do things you
  527. normally would in GDB like set a break point on @var{grub_main}.
  528. Setting breakpoints in modules is trickier since they haven't been loaded
  529. yet and are loaded at addresses determined at runtime. The module could be
  530. loaded to different addresses in different QEMU instances. The debug symbols
  531. in the modules @file{.module} binary, thus are always wrong, and GDB needs
  532. to be told where to load the symbols to. But this must happen at runtime
  533. after GRUB2 has determined where the module will get loaded. Luckily the
  534. @file{gdb_grub} script takes care of this with the @command{runtime_load_module}
  535. command, which configures GDB to watch for GRUB2 module loading and when
  536. it does add the module symbols with the appropriate offset.
  537. @node x86_64-efi
  538. @section x86_64-efi
  539. Using GDB to debug GRUB2 for the x86_64-efi target has some similarities with
  540. the i386-pc target. Please read and familiarize yourself with the @ref{i386-pc}
  541. section when reading this one. Extra care must be used to run QEMU such that it
  542. boots a UEFI firmware. This usually involves either using the @samp{-bios}
  543. option with a UEFI firmware blob (eg. @file{OVMF.fd}) or loading the firmware
  544. via pflash. This document will not go further into how to do this as there are
  545. ample resource on the web.
  546. Like all EFI implementations, on x86_64-efi the (U)EFI firmware that loads
  547. the GRUB2 EFI application determines at runtime where the application will
  548. be loaded. This means that we do not know where to tell GDB to load the
  549. symbols for the GRUB2 core until the (U)EFI firmware determines it. There are
  550. two good ways of figuring this out when running in QEMU: use a @ref{OVMF debug log,
  551. debug build of OVMF} and check the debug log, or have GRUB2 say where it is
  552. loaded. Neither of these are ideal because they both generally give the
  553. information after GRUB2 is already running, which makes debugging early boot
  554. infeasible. Technically, the first method does give the load address before
  555. GRUB2 is run, but without debugging the EFI firmware with symbols, the author
  556. currently does not know how to cause the OVMF firmware to pause at that point
  557. to use the load address before GRUB2 is run.
  558. Even after getting the application load address, the loading of core symbols
  559. is complicated by the fact that the debugging symbols for the kernel are in
  560. an ELF binary named @file{kernel.exec} while what is in memory are sections
  561. for the PE32+ EFI binary. When @command{grub-mkimage} creates the PE32+
  562. binary it condenses several segments from the ELF kernel binary into one
  563. .data section in the PE32+ binary. This must be taken into account to
  564. properly load the other non-text sections. Otherwise, GDB will work as
  565. expected when breaking on functions, but, for instance, global variables
  566. will point to the wrong address in memory and thus give incorrect values
  567. (which can be difficult to debug).
  568. Calculating the correct offsets for sections is taken care of automatically
  569. when loading the kernel symbols via the user-defined GDB command
  570. @command{dynamic_load_kernel_exec_symbols}, which takes one argument, the
  571. address where the text section is loaded as determined by one of the methods
  572. above. Alternatively, the command @command{dynamic_load_symbols} with the text
  573. section address as an agrument can be called to load the kernel symbols and set
  574. up loading the module symbols as they are loaded at runtime.
  575. In the author's experience, when debugging with QEMU and OVMF, to have
  576. debugging symbols loaded at the start of GRUB2 execution the GRUB2 EFI
  577. application must be run via QEMU at least once prior in order to get the
  578. load address. Two methods for obtaining the load address are described in
  579. two subsections below. Generally speaking, the load address does not change
  580. between QEMU runs. There are exceptions to this, namely that different
  581. GRUB2 EFI applications can be run at different addresses. Also, it has been
  582. observed that after running the EFI application for the first time, the
  583. second run will sometimes have a different load address, but subsequent
  584. runs of the same EFI application will have the same load address as the
  585. second run. And it's a near certainty that if the GRUB EFI binary has changed,
  586. eg. been recompiled, the load address will also be different.
  587. This ability to predict what the load address will be allows one to assume
  588. the load address on subsequent runs and thus load the symbols before GRUB2
  589. starts. The following command illustrates this, assuming that QEMU is
  590. running and waiting for a debugger connection and the current working
  591. directory is where @file{gdb_grub} resides:
  592. @example
  593. gdb -x gdb_grub -ex 'dynamic_load_symbols @var{address of .text section}'
  594. @end example
  595. If you load the symbols in this manner and, after continuing execution, do
  596. not see output showing the module symbols loading, then it is very likely
  597. that the load address was incorrect.
  598. Another thing to be aware of is how the loading of the GRUB image by the
  599. firmware affects previously set software breakpoints. On x86 platforms,
  600. software breakpoints are implemented by GDB by writing a special processor
  601. instruction at the location of the desired breakpoint. This special instruction
  602. when executed will stop the program execution and hand control to the
  603. debugger, GDB. GDB will first save the instruction bytes that are
  604. overwritten at the breakpoint and will put them back when the breakpoint
  605. is hit. If GRUB is being run for the first time in QEMU, the firmware will
  606. be loading the GRUB image into memory where every byte is already set to 0.
  607. This means that if a breakpoint is set before GRUB is loaded, GDB will save
  608. the 0-byte(s) where the the special instruction will go. Then when the firmware
  609. loads the GRUB image and because it is unaware of the debugger, it will
  610. write the GRUB image to memory, overwriting anything that was there previously ---
  611. notably in this case the instruction that implements the software breakpoint.
  612. This will be confusing for the person using GDB because GDB will show the
  613. breakpoint as set, but the brekapoint will never be hit. Furthermore, GDB
  614. then becomes confused, such that even deleting an recreating the breakpoint
  615. will not create usable breakpoints. The @file{gdb_grub} script takes care of
  616. this by saving the breakpoints just before they are overwritten, and then
  617. restores them at the start of GRUB execution. So breakpoints for GRUB can be
  618. set before GRUB is loaded, but be mindful of this effect if you are confused
  619. as to why breakpoints are not getting hit.
  620. Also note, that hardware breakpoints do not suffer this problem. They are
  621. implemented by having the breakpoint address in special debug registers on
  622. the CPU. So they can always be set freely without regard to whether GRUB has
  623. been loaded or not. The reason that hardware breakpoints aren't always used
  624. is because there are a limited number of them, usually around 4 on various
  625. CPUs, and specifically exactly 4 for x86 CPUs. The @file{gdb_grub} script goes
  626. out of its way to avoid using hardware breakpoints internally and uses them as
  627. briefly as possible when needed, thus allowing the user to have a maximal
  628. number at their disposal.
  629. @menu
  630. * OVMF debug log::
  631. * Using the gdbinfo command::
  632. @end menu
  633. @node OVMF debug log
  634. @subsection OVMF debug log
  635. In order to get the GRUB2 load address from OVMF, first, a debug build
  636. of OVMF must be obtained (@uref{https://github.com/retrage/edk2-nightly/raw/master/bin/DEBUGX64_OVMF.fd,
  637. here is one} which is not officially recommended). OVMF will output debug
  638. messages to a special serial device, which we must add to QEMU. The following
  639. QEMU command will run the debug OVMF and write the debug messages to a
  640. file named @file{debug.log}. It is assumed that @file{disk.img} is a disk
  641. image or block device that is set up to boot GRUB2 EFI.
  642. @example
  643. qemu-system-x86_64 -bios /path/to/debug/OVMF.fd \
  644. -drive file=disk.img,format=raw \
  645. -device virtio-scsi-pci,id=scsi0 \
  646. -debugcon file:debug.log -global isa-debugcon.iobase=0x402
  647. @end example
  648. If GRUB2 was started by the (U)EFI firmware, then in the @file{debug.log}
  649. file one of the last lines should be a log message like:
  650. @samp{Loading driver at 0x00006AEE000 EntryPoint=0x00006AEE756}. This
  651. means that the GRUB2 EFI application was loaded at @samp{0x00006AEE000} and
  652. its .text section is at @samp{0x00006AEE756}.
  653. @node Using the gdbinfo command
  654. @subsection Using the gdbinfo command
  655. On EFI platforms the command @command{gdbinfo} will output a string that
  656. is to be run in a GDB session running with the @file{gdb_grub} GDB script.
  657. @node Porting
  658. @chapter Porting
  659. GRUB2 is designed to be easily portable accross platforms. But because of the
  660. nature of bootloader every new port must be done separately. Here is how I did
  661. MIPS (loongson and ARC) and Xen ports. Note than this is more of suggestions,
  662. not absolute truth.
  663. First of all grab any architecture specifications you can find in public
  664. (please avoid NDA).
  665. First stage is ``Hello world''. I've done it outside of GRUB for simplicity.
  666. Your task is to have a small program which is loadable as bootloader and
  667. clearly shows its presence to you. If you have easily accessible console
  668. you can just print a message. If you have a mapped framebuffer you know address
  669. of, you can draw a square. If you have a debug facility, just hanging without
  670. crashing might be enough. For the first stage you can choose to load the
  671. bootloader across the network since format for network image is often easier
  672. than for local boot and it skips the need of small intermediary stages and
  673. nvram handling. Additionally you can often have a good idea of the needed
  674. format by running ``file'' on any netbootable executable for given platform.
  675. This program should probably have 2 parts: an assembler and C one. Assembler one
  676. handles BSS cleaning and other needed setup (on some platforms you may need
  677. to switch modes or copy the executable to its definitive position). So your code
  678. may look like (x86 assembly for illustration purposes)
  679. @example
  680. .globl _start
  681. _start:
  682. movl $_bss_start, %edi
  683. movl $_end, %ecx
  684. subl %edi, %ecx
  685. xorl %eax, %eax
  686. cld
  687. rep
  688. stosb
  689. call main
  690. @end example
  691. @example
  692. static const char msg[] = "Hello, world";
  693. void
  694. putchar (int c)
  695. @{
  696. ...
  697. @}
  698. void
  699. main (void)
  700. @{
  701. const char *ptr = msg;
  702. while (*ptr)
  703. putchar (*ptr++);
  704. while (1);
  705. @}
  706. @end example
  707. Sometimes you need a third file: assembly stubs for ABI-compatibility.
  708. Once this file is functional it's time to move it into GRUB2. The startup
  709. assembly file goes to grub-core/kern/$cpu/$platform/startup.S. You should also
  710. include grub/symbol.h and replace call to entry point with call to
  711. EXT_C(grub_main). The C file goes to grub-core/kern/$cpu/$platform/init.c
  712. and its entry point is renamed to void grub_machine_init (void). Keep final
  713. infinite loop for now. Stubs file if any goes to
  714. grub-core/kern/$cpu/$platform/callwrap.S. Sometimes either $cpu or $platform
  715. is dropped if file is used on several cpus respectivelyplatforms.
  716. Check those locations if they already have what you're looking for.
  717. Then modify in configure.ac the following parts:
  718. CPU names:
  719. @example
  720. case "$target_cpu" in
  721. i[[3456]]86) target_cpu=i386 ;;
  722. amd64) target_cpu=x86_64 ;;
  723. sparc) target_cpu=sparc64 ;;
  724. s390x) target_cpu=s390 ;;
  725. ...
  726. esac
  727. @end example
  728. Sometimes CPU have additional architecture names which don't influence booting.
  729. You might want to have some canonical name to avoid having bunch of identical
  730. platforms with different names.
  731. NOTE: it doesn't influence compile optimisations which depend solely on
  732. chosen compiler and compile options.
  733. @example
  734. if test "x$with_platform" = x; then
  735. case "$target_cpu"-"$target_vendor" in
  736. i386-apple) platform=efi ;;
  737. i386-*) platform=pc ;;
  738. x86_64-apple) platform=efi ;;
  739. x86_64-*) platform=pc ;;
  740. powerpc-*) platform=ieee1275 ;;
  741. ...
  742. esac
  743. else
  744. ...
  745. fi
  746. @end example
  747. This part deals with guessing the platform from CPU and vendor. Sometimes you
  748. need to use 32-bit mode for booting even if OS runs in 64-bit one. If so add
  749. your platform to:
  750. @example
  751. case "$target_cpu"-"$platform" in
  752. x86_64-efi) ;;
  753. x86_64-emu) ;;
  754. x86_64-*) target_cpu=i386 ;;
  755. powerpc64-ieee1275) target_cpu=powerpc ;;
  756. esac
  757. @end example
  758. Add your platform to the list of supported ones:
  759. @example
  760. case "$target_cpu"-"$platform" in
  761. i386-efi) ;;
  762. x86_64-efi) ;;
  763. i386-pc) ;;
  764. i386-multiboot) ;;
  765. i386-coreboot) ;;
  766. ...
  767. esac
  768. @end example
  769. If explicit -m32 or -m64 is needed add it to:
  770. @example
  771. case "$target_cpu" in
  772. i386 | powerpc) target_m32=1 ;;
  773. x86_64 | sparc64) target_m64=1 ;;
  774. esac
  775. @end example
  776. Finally you need to add a conditional to the following block:
  777. @example
  778. AM_CONDITIONAL([COND_mips_arc], [test x$target_cpu = xmips -a x$platform = xarc])
  779. AM_CONDITIONAL([COND_sparc64_ieee1275], [test x$target_cpu = xsparc64 -a x$platform = xieee1275])
  780. AM_CONDITIONAL([COND_powerpc_ieee1275], [test x$target_cpu = xpowerpc -a x$platform = xieee1275])
  781. @end example
  782. Next stop is gentpl.py. You need to add your platform to the list of supported
  783. ones (sorry that this list is duplicated):
  784. @example
  785. GRUB_PLATFORMS = [ "emu", "i386_pc", "i386_efi", "i386_qemu", "i386_coreboot",
  786. "i386_multiboot", "i386_ieee1275", "x86_64_efi",
  787. "mips_loongson", "sparc64_ieee1275",
  788. "powerpc_ieee1275", "mips_arc", "ia64_efi",
  789. "mips_qemu_mips", "s390_mainframe" ]
  790. @end example
  791. You may also want already to add new platform to one or several of available
  792. groups. In particular we always have a group for each CPU even when only
  793. one platform for given CPU is available.
  794. Then comes grub-core/Makefile.core.def. In the block ``kernel'' you'll need
  795. to define ldflags for your platform ($cpu_$platform_ldflags). You also need to
  796. declare startup asm file ($cpu_$platform_startup) as well as any other files
  797. (e.g. init.c and callwrap.S) (e.g. $cpu_$platform = kern/$cpu/$platform/init.c).
  798. At this stage you will also need to add dummy dl.c and cache.S with functions
  799. grub_err_t grub_arch_dl_check_header (void *ehdr), grub_err_t
  800. grub_arch_dl_relocate_symbols (grub_dl_t mod, void *ehdr) (dl.c) and
  801. void grub_arch_sync_caches (void *address, grub_size_t len) (cache.S). They
  802. won't be used for now.
  803. You will need to create directory include/$cpu/$platform and a file
  804. include/$cpu/types.h. The latter following this template:
  805. @example
  806. #ifndef GRUB_TYPES_CPU_HEADER
  807. #define GRUB_TYPES_CPU_HEADER 1
  808. /* The size of void *. */
  809. #define GRUB_TARGET_SIZEOF_VOID_P 4
  810. /* The size of long. */
  811. #define GRUB_TARGET_SIZEOF_LONG 4
  812. /* mycpu is big-endian. */
  813. #define GRUB_TARGET_WORDS_BIGENDIAN 1
  814. /* Alternatively: mycpu is little-endian. */
  815. #undef GRUB_TARGET_WORDS_BIGENDIAN
  816. #endif /* ! GRUB_TYPES_CPU_HEADER */
  817. @end example
  818. You will also need to add a dummy file to datetime and setjmp modules to
  819. avoid any of it having no files. It can be just completely empty at this stage.
  820. You'll need to make grub-mkimage.c (util/grub_mkimage.c) aware of the needed
  821. format. For most commonly used formats like ELF, PE, aout or raw the support
  822. is already present and you'll need to make it follow the existant code paths
  823. for your platform adding adjustments if necessary. When done compile:
  824. @example
  825. ./bootstrap
  826. ./configure --target=$cpu --with-platform=$platform TARGET_CC=.. OBJCOPY=... STRIP=...
  827. make > /dev/null
  828. @end example
  829. And create image
  830. @example
  831. ./grub-mkimage -d grub-core -O $format_id -o test.img
  832. @end example
  833. And it's time to test your test.img.
  834. If it works next stage is to have heap, console and timer.
  835. To have the heap working you need to determine which regions are suitable for
  836. heap usage, allocate them from firmware and map (if applicable). Then call
  837. grub_mm_init_region (void *start, grub_size_t s) for every of this region.
  838. As a shortcut for early port you can allocate right after _end or have
  839. a big static array for heap. If you do you'll probably need to come back to
  840. this later. As for output console you should distinguish between an array of
  841. text, terminfo or graphics-based console. Many of real-world examples don't
  842. fit perfectly into any of these categories but one of the models is easier
  843. to be used as base. In second and third case you should add your platform to
  844. terminfokernel respectively videoinkernel group. A good example of array of
  845. text is i386-pc (kern/i386/pc/init.c and term/i386/pc/console.c).
  846. Of terminfo is ieee1275 (kern/ieee1275/init.c and term/ieee1275/console.c).
  847. Of video is loongson (kern/mips/loongson/init.c). Note that terminfo has
  848. to be inited in 2 stages: one before (to get at least rudimentary console
  849. as early as possible) and another after the heap (to get full-featured console).
  850. For the input there are string of keys, terminfo and direct hardware. For string
  851. of keys look at i386-pc (same files), for terminfo ieee1275 (same files) and for
  852. hardware loongson (kern/mips/loongson/init.c and term/at_keyboard.c).
  853. For the timer you'll need to call grub_install_get_time_ms (...) with as sole
  854. argument a function returning a grub_uint64_t of a number of milliseconds
  855. elapsed since arbitrary point in the past.
  856. Once these steps accomplished you can remove the inifinite loop and you should
  857. be able to get to the minimal console. Next step is to have module loading
  858. working. For this you'll need to fill kern/$cpu/dl.c and kern/$cpu/cache.S
  859. with real handling of relocations and respectively the real sync of I and D
  860. caches. Also you'll need to decide where in the image to store the modules.
  861. Usual way is to have it concatenated at the end. In this case you'll need to
  862. modify startup.S to copy modules out of bss to let's say ALIGN_UP (_end, 8)
  863. before cleaning out bss. You'll probably find useful to add total_module_size
  864. field to startup.S. In init.c you need to set grub_modbase to the address
  865. where modules can be found. You may need grub_modules_get_end () to avoid
  866. declaring the space occupied by modules as usable for heap. You can test modules
  867. with:
  868. @example
  869. ./grub-mkimage -d grub-core -O $format_id -o test.img hello
  870. @end example
  871. and then running ``hello'' in the shell.
  872. Once this works, you should think of implementing disk access. Look around
  873. disk/ for examples.
  874. Then, very importantly, you probably need to implement the actual loader
  875. (examples available in loader/)
  876. Last step to have minimally usable port is to add support to grub-install to
  877. put GRUB in a place where firmware or platform will pick it up.
  878. Next steps are: filling datetime.c, setjmp.S, network (net/drivers),
  879. video (video/), halt (lib/), reboot (lib/).
  880. Please add your platform to Platform limitations and Supported kernels chapter
  881. in user documentation and mention any steps you skipped which result in reduced
  882. features or performance. Here is the quick checklist of features. Some of them
  883. are less important than others and skipping them is completely ok, just needs
  884. to be mentioned in user documentation.
  885. Checklist:
  886. @itemize
  887. @item Is heap big enough?
  888. @item Which charset is supported by console?
  889. @item Does platform have disk driver?
  890. @item Do you have network card support?
  891. @item Are you able to retrieve datetime (with date)?
  892. @item Are you able to set datetime (with date)?
  893. @item Is serial supported?
  894. @item Do you have direct disk support?
  895. @item Do you have direct keyboard support?
  896. @item Do you have USB support?
  897. @item Do you support loading through network?
  898. @item Do you support loading from disk?
  899. @item Do you support chainloading?
  900. @item Do you support network chainloading?
  901. @item Does cpuid command supports checking all
  902. CPU features that the user might want conditionalise on
  903. (64-bit mode, hypervisor,...)
  904. @item Do you support hints? How reliable are they?
  905. @item Does platform have ACPI? If so do ``acpi'' and ``lsacpi'' modules work?
  906. @item Do any of platform-specific operations mentioned in the relevant section of
  907. user manual makes sense on your platform?
  908. @item Does your platform support PCI? If so is there an appropriate driver for
  909. GRUB?
  910. @item Do you support badram?
  911. @end itemize
  912. @node Error Handling
  913. @chapter Error Handling
  914. Error handling in GRUB 2 is based on exception handling model. As C language
  915. doesn't directly support exceptions, exception handling behavior is emulated
  916. in software.
  917. When exception is raised, function must return to calling function. If calling
  918. function does not provide handling of the exception it must return back to its
  919. calling function and so on, until exception is handled. If exception is not
  920. handled before prompt is displayed, error message will be shown to user.
  921. Exception information is stored on @code{grub_errno} global variable. If
  922. @code{grub_errno} variable contains value @code{GRUB_ERR_NONE}, there is no active
  923. exception and application can continue normal processing. When @code{grub_errno} has
  924. other value, it is required that application code either handles this error or
  925. returns instantly to caller. If function is with return type @code{grub_err_t} is
  926. about to return @code{GRUB_ERR_NONE}, it should not set @code{grub_errno} to that
  927. value. Only set @code{grub_errno} in cases where there is error situation.
  928. Simple exception forwarder.
  929. @example
  930. grub_err_t
  931. forwarding_example (void)
  932. @{
  933. /* Call function that might cause exception. */
  934. foobar ();
  935. /* No special exception handler, just forward possible exceptions. */
  936. if (grub_errno != GRUB_ERR_NONE)
  937. @{
  938. return grub_errno;
  939. @}
  940. /* All is OK, do more processing. */
  941. /* Return OK signal, to caller. */
  942. return GRUB_ERR_NONE;
  943. @}
  944. @end example
  945. Error reporting has two components, the actual error code (of type
  946. @code{grub_err_t}) and textual message that will be displayed to user. List of
  947. valid error codes is listed in header file @file{include/grub/err.h}. Textual
  948. error message can contain any textual data. At time of writing, error message
  949. can contain up to 256 characters (including terminating NUL). To ease error
  950. reporting there is a helper function @code{grub_error} that allows easier
  951. formatting of error messages and should be used instead of writing directly to
  952. global variables.
  953. Example of error reporting.
  954. @example
  955. grub_err_t
  956. failing_example ()
  957. @{
  958. return grub_error (GRUB_ERR_FILE_NOT_FOUND,
  959. "Failed to read %s, tried %d times.",
  960. "test.txt",
  961. 10);
  962. @}
  963. @end example
  964. If there is a special reason that error code does not need to be taken account,
  965. @code{grub_errno} can be zeroed back to @code{GRUB_ERR_NONE}. In cases like this all
  966. previous error codes should have been handled correctly. This makes sure that
  967. there are no unhandled exceptions.
  968. Example of zeroing @code{grub_errno}.
  969. @example
  970. grub_err_t
  971. probe_example ()
  972. @{
  973. /* Try to probe device type 1. */
  974. probe_for_device ();
  975. if (grub_errno == GRUB_ERR_NONE)
  976. @{
  977. /* Device type 1 was found on system. */
  978. register_device ();
  979. return GRUB_ERR_NONE;
  980. @}
  981. /* Zero out error code. */
  982. grub_errno = GRUB_ERR_NONE;
  983. /* No device type 1 found, try to probe device type 2. */
  984. probe_for_device2 ();
  985. if (grub_errno == GRUB_ERR_NONE)
  986. @{
  987. /* Device type 2 was found on system. */
  988. register_device2 ();
  989. return GRUB_ERR_NONE;
  990. @}
  991. /* Zero out error code. */
  992. grub_errno = GRUB_ERR_NONE;
  993. /* Return custom error message. */
  994. return grub_error (GRUB_ERR_UNKNOWN_DEVICE, "No device type 1 or 2 found.");
  995. @}
  996. @end example
  997. Some times there is a need to continue processing even if there is a error
  998. state in application. In situations like this, there is a needed to save old
  999. error state and then call other functions that might fail. To aid in this,
  1000. there is a error stack implemented. Error state can be pushed to error stack
  1001. by calling function @code{grub_error_push ()}. When processing has been completed,
  1002. @code{grub_error_pop ()} can be used to pop error state from stack. Error stack
  1003. contains predefined amount of error stack items. Error stack is protected for
  1004. overflow and marks these situations so overflow error does not get unseen.
  1005. If there is no space available to store error message, it is simply discarded
  1006. and overflow will be marked as happened. When overflow happens, it most likely
  1007. will corrupt error stack consistency as for pushed error there is no matching
  1008. pop, but overflow message will be shown to inform user about the situation.
  1009. Overflow message will be shown at time when prompt is about to be drawn.
  1010. Example usage of error stack.
  1011. @example
  1012. /* Save possible old error message. */
  1013. grub_error_push ();
  1014. /* Do your stuff here. */
  1015. call_possibly_failing_function ();
  1016. if (grub_errno != GRUB_ERR_NONE)
  1017. @{
  1018. /* Inform rest of the code that there is error (grub_errno
  1019. is set). There is no pop here as we want both error states
  1020. to be displayed. */
  1021. return;
  1022. @}
  1023. /* Restore old error state by popping previous item from stack. */
  1024. grub_error_pop ();
  1025. @end example
  1026. @node Stack and heap size
  1027. @chapter Stack and heap size
  1028. On emu stack and heap are just normal host OS stack and heap. Stack is typically
  1029. 8 MiB although it's OS-dependent.
  1030. On i386-pc, i386-coreboot, i386-qemu and i386-multiboot the stack is 60KiB.
  1031. All available space between 1MiB and 4GiB marks is part of heap.
  1032. On *-xen stack is 4MiB. If compiled for x86-64 with GCC 4.4 or later addressable
  1033. space is unlimited. When compiled for x86-64 with older GCC version addressable
  1034. space is limited to 2GiB. When compiling for i386 addressable space is limited
  1035. to 4GiB. All addressable pages except the ones for stack, GRUB binary, special
  1036. pages and page table are in the heap.
  1037. On *-efi GRUB uses same stack as EFI. If compiled for x86-64 with GCC 4.4 or
  1038. later addressable space is unlimited. When compiled for x86-64 with older GCC
  1039. version addressable space is limited to 2GiB. For all other platforms addressable
  1040. space is limited to 4GiB. GRUB allocates pages from EFI for its heap, at most
  1041. 1.6 GiB.
  1042. On i386-ieee1275 and powerpc-ieee1275 GRUB uses same stack as IEEE1275.
  1043. On i386-ieee1275 and powerpc-ieee1275, GRUB will allocate 32MiB for its heap on
  1044. startup. It may allocate more at runtime, as long as at least 128MiB remain free
  1045. in OpenFirmware.
  1046. On sparc64-ieee1275 stack is 256KiB and heap is 2MiB.
  1047. On mips(el)-qemu_mips and mipsel-loongson stack is 2MiB (everything below
  1048. GRUB image) and everything above GRUB image (from 2MiB + kernel size)
  1049. until 256MiB is part of heap.
  1050. On mips-arc stack is 2MiB (everything below GRUB image) and everything above
  1051. GRUB image(from 2MiB + kernel size) until 128MiB is part of heap.
  1052. On mipsel-arc stack is 2MiB (everything below GRUB image which is not part
  1053. of ARC) and everything above GRUB image (from 7MiB + kernel size)
  1054. until 256MiB is part of heap.
  1055. On arm-uboot stack is 256KiB and heap is 2MiB.
  1056. In short:
  1057. @multitable @columnfractions .15 .25 .5
  1058. @headitem Platform @tab Stack @tab Heap
  1059. @item emu @tab 8 MiB @tab ?
  1060. @item i386-pc @tab 60 KiB @tab < 4 GiB
  1061. @item i386-coreboot @tab 60 KiB @tab < 4 GiB
  1062. @item i386-multiboot @tab 60 KiB @tab < 4 GiB
  1063. @item i386-qemu @tab 60 KiB @tab < 4 GiB
  1064. @item *-efi @tab ? @tab < 1.6 GiB
  1065. @item i386-ieee1275 @tab ? @tab < 32 MiB
  1066. @item powerpc-ieee1275 @tab ? @tab available memory - 128MiB
  1067. @item sparc64-ieee1275 @tab 256KiB @tab 2 MiB
  1068. @item arm-uboot @tab 256KiB @tab 2 MiB
  1069. @item mips(el)-qemu_mips @tab 2MiB @tab 253 MiB
  1070. @item mipsel-loongson @tab 2MiB @tab 253 MiB
  1071. @item mips-arc @tab 2MiB @tab 125 MiB
  1072. @item mipsel-arc @tab 2MiB @tab 248 MiB
  1073. @item x86_64-xen (GCC >= 4.4) @tab 4MiB @tab unlimited
  1074. @item x86_64-xen (GCC < 4.4) @tab 4MiB @tab < 2GiB
  1075. @item i386-xen @tab 4MiB @tab < 4GiB
  1076. @end multitable
  1077. @node BIOS port memory map
  1078. @chapter BIOS port memory map
  1079. @c By Yoshinori K Okuji
  1080. @multitable @columnfractions .15 .25 .5
  1081. @headitem Start @tab End @tab Usage
  1082. @item 0 @tab 0x1000 - 1 @tab BIOS and real mode interrupts
  1083. @item 0x07BE @tab 0x07FF @tab Partition table passed to another boot loader
  1084. @item ? @tab 0x2000 - 1 @tab Real mode stack
  1085. @item 0x7C00 @tab 0x7D00 - 1 @tab Boot sector
  1086. @item 0x8000 @tab ? @tab GRUB kernel
  1087. @item 0x68000 @tab 0x71000 - 1 @tab Disk buffer
  1088. @item ? @tab 0x80000 - 1 @tab Protected mode stack
  1089. @item ? @tab 0xA0000 - 1 @tab Extended BIOS Data Area
  1090. @item 0xA0000 @tab 0xC0000 - 1 @tab Video RAM
  1091. @item 0xC0000 @tab 0x100000 - 1 @tab BIOS
  1092. @item 0x100000 @tab ? @tab Heap and module code
  1093. @end multitable
  1094. @node Video Subsystem
  1095. @chapter Video Subsystem
  1096. @c By VesaJääskeläinen
  1097. This document contains specification for Video Subsystem for GRUB2.
  1098. Currently only the usage interface is described in this document.
  1099. Internal structure of how video drivers are registering and how video
  1100. driver manager works are not included here.
  1101. @menu
  1102. * Video API::
  1103. * Example usage of Video API::
  1104. * Bitmap API::
  1105. @end menu
  1106. @node Video API
  1107. @section Video API
  1108. @subsection grub_video_setup
  1109. @itemize
  1110. @item Prototype:
  1111. @example
  1112. grub_err_t
  1113. grub_video_setup (unsigned int width, unsigned int height, unsigned int mode_type);
  1114. @end example
  1115. @item Description:
  1116. Driver will use information provided to it to select best possible video mode and switch to it. Supported values for @code{mode_type} are @code{GRUB_VIDEO_MODE_TYPE_INDEX_COLOR} for index color modes, @code{GRUB_VIDEO_MODE_TYPE_RGB} for direct RGB color modes and @code{GRUB_VIDEO_MODE_TYPE_DOUBLE_BUFFERED} for double buffering. When requesting RGB mode, highest bits per pixel mode will be selected. When requesting Index color mode, mode with highest number of colors will be selected. If all parameters are specified as zero, video adapter will try to figure out best possible mode and initialize it, platform specific differences are allowed here. If there is no mode matching request, error X will be returned. If there are no problems, function returns @code{GRUB_ERR_NONE}.
  1117. This function also performs following task upon succesful mode switch. Active rendering target is changed to screen and viewport is maximized to allow whole screen to be used when performing graphics operations. In RGB modes, emulated palette gets 16 entries containing default values for VGA palette, other colors are defined as black. When switching to Indexed Color mode, driver may set default VGA palette to screen if the video card allows the operation.
  1118. @end itemize
  1119. @subsection grub_video_restore
  1120. @itemize
  1121. @item Prototype:
  1122. @example
  1123. grub_err_t
  1124. grub_video_restore (void);
  1125. @end example
  1126. @item Description:
  1127. Video subsystem will deinitialize activated video driver to restore old state of video device. This can be used to switch back to text mode.
  1128. @end itemize
  1129. @subsection grub_video_get_info
  1130. @itemize
  1131. @item Prototype:
  1132. @example
  1133. grub_err_t
  1134. grub_video_get_info (struct grub_video_mode_info *mode_info);
  1135. @end example
  1136. @example
  1137. struct grub_video_mode_info
  1138. @{
  1139. /* Width of the screen. */
  1140. unsigned int width;
  1141. /* Height of the screen. */
  1142. unsigned int height;
  1143. /* Mode type bitmask. Contains information like is it Index color or
  1144. RGB mode. */
  1145. unsigned int mode_type;
  1146. /* Bits per pixel. */
  1147. unsigned int bpp;
  1148. /* Bytes per pixel. */
  1149. unsigned int bytes_per_pixel;
  1150. /* Pitch of one scanline. How many bytes there are for scanline. */
  1151. unsigned int pitch;
  1152. /* In index color mode, number of colors. In RGB mode this is 256. */
  1153. unsigned int number_of_colors;
  1154. /* Optimization hint how binary data is coded. */
  1155. enum grub_video_blit_format blit_format;
  1156. /* How many bits are reserved for red color. */
  1157. unsigned int red_mask_size;
  1158. /* What is location of red color bits. In Index Color mode, this is 0. */
  1159. unsigned int red_field_pos;
  1160. /* How many bits are reserved for green color. */
  1161. unsigned int green_mask_size;
  1162. /* What is location of green color bits. In Index Color mode, this is 0. */
  1163. unsigned int green_field_pos;
  1164. /* How many bits are reserved for blue color. */
  1165. unsigned int blue_mask_size;
  1166. /* What is location of blue color bits. In Index Color mode, this is 0. */
  1167. unsigned int blue_field_pos;
  1168. /* How many bits are reserved in color. */
  1169. unsigned int reserved_mask_size;
  1170. /* What is location of reserved color bits. In Index Color mode,
  1171. this is 0. */
  1172. unsigned int reserved_field_pos;
  1173. @};
  1174. @end example
  1175. @item Description:
  1176. Software developer can use this function to query properties of active rendering taget. Information provided here can be used by other parts of GRUB, like image loaders to convert loaded images to correct screen format to allow more optimized blitters to be used. If there there is no configured video driver with active screen, error @code{GRUB_ERR_BAD_DEVICE} is returned, otherwise @code{mode_info} is filled with valid information and @code{GRUB_ERR_NONE} is returned.
  1177. @end itemize
  1178. @subsection grub_video_get_blit_format
  1179. @itemize
  1180. @item Prototype:
  1181. @example
  1182. enum grub_video_blit_format
  1183. grub_video_get_blit_format (struct grub_video_mode_info *mode_info);
  1184. @end example
  1185. @example
  1186. enum grub_video_blit_format
  1187. @{
  1188. /* Follow exactly field & mask information. */
  1189. GRUB_VIDEO_BLIT_FORMAT_RGBA,
  1190. /* Make optimization assumption. */
  1191. GRUB_VIDEO_BLIT_FORMAT_R8G8B8A8,
  1192. /* Follow exactly field & mask information. */
  1193. GRUB_VIDEO_BLIT_FORMAT_RGB,
  1194. /* Make optimization assumption. */
  1195. GRUB_VIDEO_BLIT_FORMAT_R8G8B8,
  1196. /* When needed, decode color or just use value as is. */
  1197. GRUB_VIDEO_BLIT_FORMAT_INDEXCOLOR
  1198. @};
  1199. @end example
  1200. @item Description:
  1201. Used to query how data could be optimized to suit specified video mode. Returns exact video format type, or a generic one if there is no definition for the type. For generic formats, use @code{grub_video_get_info} to query video color coding settings.
  1202. @end itemize
  1203. @subsection grub_video_set_palette
  1204. @itemize
  1205. @item Prototype:
  1206. @example
  1207. grub_err_t
  1208. grub_video_set_palette (unsigned int start, unsigned int count, struct grub_video_palette_data *palette_data);
  1209. @end example
  1210. @example
  1211. struct grub_video_palette_data
  1212. @{
  1213. grub_uint8_t r; /* Red color value (0-255). */
  1214. grub_uint8_t g; /* Green color value (0-255). */
  1215. grub_uint8_t b; /* Blue color value (0-255). */
  1216. grub_uint8_t a; /* Reserved bits value (0-255). */
  1217. @};
  1218. @end example
  1219. @item Description:
  1220. Used to setup indexed color palettes. If mode is RGB mode, colors will be set to emulated palette data. In Indexed Color modes, palettes will be set to hardware. Color values will be converted to suit requirements of the video mode. @code{start} will tell what hardware color index (or emulated color index) will be set to according information in first indice of @code{palette_data}, after that both hardware color index and @code{palette_data} index will be incremented until @code{count} number of colors have been set.
  1221. @end itemize
  1222. @subsection grub_video_get_palette
  1223. @itemize
  1224. @item Prototype:
  1225. @example
  1226. grub_err_t
  1227. grub_video_get_palette (unsigned int start, unsigned int count, struct grub_video_palette_data *palette_data);
  1228. @end example
  1229. @example
  1230. struct grub_video_palette_data
  1231. @{
  1232. grub_uint8_t r; /* Red color value (0-255). */
  1233. grub_uint8_t g; /* Green color value (0-255). */
  1234. grub_uint8_t b; /* Blue color value (0-255). */
  1235. grub_uint8_t a; /* Reserved bits value (0-255). */
  1236. @};
  1237. @end example
  1238. @item Description:
  1239. Used to query indexed color palettes. If mode is RGB mode, colors will be copied from emulated palette data. In Indexed Color modes, palettes will be read from hardware. Color values will be converted to suit structure format. @code{start} will tell what hardware color index (or emulated color index) will be used as a source for first indice of @code{palette_data}, after that both hardware color index and @code{palette_data} index will be incremented until @code{count} number of colors have been read.
  1240. @end itemize
  1241. @subsection grub_video_set_area_status
  1242. @itemize
  1243. @item Prototype:
  1244. @example
  1245. grub_err_t
  1246. grub_video_set_area_status (grub_video_area_status_t area_status);
  1247. @end example
  1248. @example
  1249. enum grub_video_area_status_t
  1250. @{
  1251. GRUB_VIDEO_AREA_DISABLED,
  1252. GRUB_VIDEO_AREA_ENABLED
  1253. @};
  1254. @end example
  1255. @item Description:
  1256. Used to set area drawing mode for redrawing the specified region. Draw commands
  1257. are performed in the intersection of the viewport and the region called area.
  1258. Coordinates remain related to the viewport. If draw commands try to draw over
  1259. the area, they are clipped.
  1260. Set status to DISABLED if you need to draw everything.
  1261. Set status to ENABLED and region to the desired rectangle to redraw everything
  1262. inside the region leaving everything else intact.
  1263. Should be used for redrawing of active elements.
  1264. @end itemize
  1265. @subsection grub_video_get_area_status
  1266. @itemize
  1267. @item Prototype:
  1268. @example
  1269. grub_err_r
  1270. grub_video_get_area_status (grub_video_area_status_t *area_status);
  1271. @end example
  1272. @item Description:
  1273. Used to query the area status.
  1274. @end itemize
  1275. @subsection grub_video_set_viewport
  1276. @itemize
  1277. @item Prototype:
  1278. @example
  1279. grub_err_t
  1280. grub_video_set_viewport (unsigned int x, unsigned int y, unsigned int width, unsigned int height);
  1281. @end example
  1282. @item Description:
  1283. Used to specify viewport where draw commands are performed. When viewport is set, all draw commands coordinates relate to those specified by @code{x} and @code{y}. If draw commands try to draw over viewport, they are clipped. If developer requests larger than possible viewport, width and height will be clamped to fit screen. If @code{x} and @code{y} are out of bounds, all functions drawing to screen will not be displayed. In order to maximize viewport, use @code{grub_video_get_info} to query actual screen dimensions and provide that information to this function.
  1284. @end itemize
  1285. @subsection grub_video_get_viewport
  1286. @itemize
  1287. @item Prototype:
  1288. @example
  1289. grub_err_t
  1290. grub_video_get_viewport (unsigned int *x, unsigned int *y, unsigned int *width, unsigned int *height);
  1291. @end example
  1292. @item Description:
  1293. Used to query current viewport dimensions. Software developer can use this to choose best way to render contents of the viewport.
  1294. @end itemize
  1295. @subsection grub_video_set_region
  1296. @itemize
  1297. @item Prototype:
  1298. @example
  1299. grub_err_t
  1300. grub_video_set_region (unsigned int x, unsigned int y, unsigned int width, unsigned int height);
  1301. @end example
  1302. @item Description:
  1303. Used to specify the region of the screen which should be redrawn. Use absolute
  1304. values. When the region is set and area status is ENABLE all draw commands will
  1305. be performed inside the interseption of region and viewport named area.
  1306. If draw commands try to draw over viewport, they are clipped. If developer
  1307. requests larger than possible region, width and height will be clamped to fit
  1308. screen. Should be used for redrawing of active elements.
  1309. @end itemize
  1310. @subsection grub_video_get_region
  1311. @itemize
  1312. @item Prototype:
  1313. @example
  1314. grub_err_t
  1315. grub_video_get_region (unsigned int *x, unsigned int *y, unsigned int *width, unsigned int *height);
  1316. @end example
  1317. @item Description:
  1318. Used to query current region dimensions.
  1319. @end itemize
  1320. @subsection grub_video_map_color
  1321. @itemize
  1322. @item Prototype:
  1323. @example
  1324. grub_video_color_t
  1325. grub_video_map_color (grub_uint32_t color_name);
  1326. @end example
  1327. @item Description:
  1328. Map color can be used to support color themes in GRUB. There will be collection of color names that can be used to query actual screen mapped color data. Examples could be @code{GRUB_COLOR_CONSOLE_BACKGROUND}, @code{GRUB_COLOR_CONSOLE_TEXT}. The actual color defines are not specified at this point.
  1329. @end itemize
  1330. @subsection grub_video_map_rgb
  1331. @itemize
  1332. @item Prototype:
  1333. @example
  1334. grub_video_color_t
  1335. grub_video_map_rgb (grub_uint8_t red, grub_uint8_t green, grub_uint8_t blue);
  1336. @end example
  1337. @item Description:
  1338. Map RGB values to compatible screen color data. Values are expected to be in range 0-255 and in RGB modes they will be converted to screen color data. In index color modes, index color palette will be searched for specified color and then index is returned.
  1339. @end itemize
  1340. @subsection grub_video_map_rgba
  1341. @itemize
  1342. @item Prototype:
  1343. @example
  1344. grub_video_color_t
  1345. grub_video_map_rgba (grub_uint8_t red, grub_uint8_t green, grub_uint8_t blue, grub_uint8_t alpha);
  1346. @end example
  1347. @item Description:
  1348. Map RGBA values to compatible screen color data. Values are expected to be in range 0-255. In RGBA modes they will be converted to screen color data. In index color modes, index color palette will be searched for best matching color and its index is returned.
  1349. @end itemize
  1350. @subsection grub_video_unmap_color
  1351. @itemize
  1352. @item Prototype:
  1353. @example
  1354. grub_err_t
  1355. grub_video_unmap_color (grub_video_color_t color, grub_uint8_t *red, grub_uint8_t *green, grub_uint8_t *blue, grub_uint8_t *alpha);
  1356. @end example
  1357. @item Description:
  1358. Unmap color value from @code{color} to color channels in @code{red}, @code{green}, @code{blue} and @code{alpha}. Values will be in range 0-255. Active rendering target will be used for color domain. In case alpha information is not available in rendering target, it is assumed to be opaque (having value 255).
  1359. @end itemize
  1360. @subsection grub_video_fill_rect
  1361. @itemize
  1362. @item Prototype:
  1363. @example
  1364. grub_err_t
  1365. grub_video_fill_rect (grub_video_color_t color, int x, int y, unsigned int width, unsigned int height);
  1366. @end example
  1367. @item Description:
  1368. Fill specified area limited by given coordinates within specified viewport. Negative coordinates are accepted in order to allow easy moving of rectangle within viewport. If coordinates are negative, area of the rectangle will be shrinken to follow size limits of the viewport.
  1369. Software developer should use either @code{grub_video_map_color}, @code{grub_video_map_rgb} or @code{grub_video_map_rgba} to map requested color to @code{color} parameter.
  1370. @end itemize
  1371. @subsection grub_video_blit_glyph
  1372. @itemize
  1373. @item Prototype:
  1374. @example
  1375. grub_err_t
  1376. grub_video_blit_glyph (struct grub_font_glyph *glyph, grub_video_color_t color, int x, int y);
  1377. @end example
  1378. @example
  1379. struct grub_font_glyph @{
  1380. /* TBD. */
  1381. @};
  1382. @end example
  1383. @item Description:
  1384. Used to blit glyph to viewport in specified coodinates. If glyph is at edge of viewport, pixels outside of viewport will be clipped out. Software developer should use either @code{grub_video_map_rgb} or @code{grub_video_map_rgba} to map requested color to @code{color} parameter.
  1385. @end itemize
  1386. @subsection grub_video_blit_bitmap
  1387. @itemize
  1388. @item Prototype:
  1389. @example
  1390. grub_err_t
  1391. grub_video_blit_bitmap (struct grub_video_bitmap *bitmap, enum grub_video_blit_operators oper, int x, int y, int offset_x, int offset_y, unsigned int width, unsigned int height);
  1392. @end example
  1393. @example
  1394. struct grub_video_bitmap
  1395. @{
  1396. /* TBD. */
  1397. @};
  1398. enum grub_video_blit_operators
  1399. @{
  1400. GRUB_VIDEO_BLIT_REPLACE,
  1401. GRUB_VIDEO_BLIT_BLEND
  1402. @};
  1403. @end example
  1404. @item Description:
  1405. Used to blit bitmap to viewport in specified coordinates. If part of bitmap is outside of viewport region, it will be clipped out. Offsets affect bitmap position where data will be copied from. Negative values for both viewport coordinates and bitmap offset coordinates are allowed. If data is looked out of bounds of bitmap, color value will be assumed to be transparent. If viewport coordinates are negative, area of the blitted rectangle will be shrinken to follow size limits of the viewport and bitmap. Blitting operator @code{oper} specifies should source pixel replace data in screen or blend with pixel alpha value.
  1406. Software developer should use @code{grub_video_bitmap_create} or @code{grub_video_bitmap_load} to create or load bitmap data.
  1407. @end itemize
  1408. @subsection grub_video_blit_render_target
  1409. @itemize
  1410. @item Prototype:
  1411. @example
  1412. grub_err_t
  1413. grub_video_blit_render_target (struct grub_video_render_target *source, enum grub_video_blit_operators oper, int x, int y, int offset_x, int offset_y, unsigned int width, unsigned int height);
  1414. @end example
  1415. @example
  1416. struct grub_video_render_target @{
  1417. /* This is private data for video driver. Should not be accessed from elsewhere directly. */
  1418. @};
  1419. enum grub_video_blit_operators
  1420. @{
  1421. GRUB_VIDEO_BLIT_REPLACE,
  1422. GRUB_VIDEO_BLIT_BLEND
  1423. @};
  1424. @end example
  1425. @item Description:
  1426. Used to blit source render target to viewport in specified coordinates. If part of source render target is outside of viewport region, it will be clipped out. If blitting operator is specified and source contains alpha values, resulting pixel color components will be calculated using formula ((src_color * src_alpha) + (dst_color * (255 - src_alpha)) / 255, if target buffer has alpha, it will be set to src_alpha. Offsets affect render target position where data will be copied from. If data is looked out of bounds of render target, color value will be assumed to be transparent. Blitting operator @code{oper} specifies should source pixel replace data in screen or blend with pixel alpha value.
  1427. @end itemize
  1428. @subsection grub_video_scroll
  1429. @itemize
  1430. @item Prototype:
  1431. @example
  1432. grub_err_t
  1433. grub_video_scroll (grub_video_color_t color, int dx, int dy);
  1434. @end example
  1435. @item Description:
  1436. Used to scroll viewport to specified direction. New areas are filled with specified color. This function is used when screen is scroller up in video terminal.
  1437. @end itemize
  1438. @subsection grub_video_swap_buffers
  1439. @itemize
  1440. @item Prototype:
  1441. @example
  1442. grub_err_t
  1443. grub_video_swap_buffers (void);
  1444. @end example
  1445. @item Description:
  1446. If double buffering is enabled, this swaps frontbuffer and backbuffer, in order to show values drawn to back buffer. Video driver is free to choose how this operation is techincally done.
  1447. @end itemize
  1448. @subsection grub_video_create_render_target
  1449. @itemize
  1450. @item Prototype:
  1451. @example
  1452. grub_err_t
  1453. grub_video_create_render_target (struct grub_video_render_target **result, unsigned int width, unsigned int height, unsigned int mode_type);
  1454. @end example
  1455. @example
  1456. struct grub_video_render_target @{
  1457. /* This is private data for video driver. Should not be accessed from elsewhere directly. */
  1458. @};
  1459. @end example
  1460. @item Description:
  1461. Driver will use information provided to it to create best fitting render target. @code{mode_type} will be used to guide on selecting what features are wanted for render target. Supported values for @code{mode_type} are @code{GRUB_VIDEO_MODE_TYPE_INDEX_COLOR} for index color modes, @code{GRUB_VIDEO_MODE_TYPE_RGB} for direct RGB color modes and @code{GRUB_VIDEO_MODE_TYPE_ALPHA} for alpha component.
  1462. @end itemize
  1463. @subsection grub_video_delete_render_target
  1464. @itemize
  1465. @item Prototype:
  1466. @example
  1467. grub_err_t
  1468. grub_video_delete_render_target (struct grub_video_render_target *target);
  1469. @end example
  1470. @item Description:
  1471. Used to delete previously created render target. If @code{target} contains @code{NULL} pointer, nothing will be done. If render target is correctly destroyed, GRUB_ERR_NONE is returned.
  1472. @end itemize
  1473. @subsection grub_video_set_active_render_target
  1474. @itemize
  1475. @item Prototype:
  1476. @example
  1477. grub_err_t
  1478. grub_video_set_active_render_target (struct grub_video_render_target *target);
  1479. @end example
  1480. @item Description:
  1481. Sets active render target. If this comand is successful all drawing commands will be done to specified @code{target}. There is also special values for target, @code{GRUB_VIDEO_RENDER_TARGET_DISPLAY} used to reference screen's front buffer, @code{GRUB_VIDEO_RENDER_TARGET_FRONT_BUFFER} used to reference screen's front buffer (alias for @code{GRUB_VIDEO_RENDER_TARGET_DISPLAY}) and @code{GRUB_VIDEO_RENDER_TARGET_BACK_BUFFER} used to reference back buffer (if double buffering is enabled). If render target is correclty switched GRUB_ERR_NONE is returned. In no any event shall there be non drawable active render target.
  1482. @end itemize
  1483. @subsection grub_video_get_active_render_target
  1484. @itemize
  1485. @item Prototype:
  1486. @example
  1487. grub_err_t
  1488. grub_video_get_active_render_target (struct grub_video_render_target **target);
  1489. @end example
  1490. @item Description:
  1491. Returns currently active render target. It returns value in @code{target} that can be subsequently issued back to @code{grub_video_set_active_render_target}.
  1492. @end itemize
  1493. @node Example usage of Video API
  1494. @section Example usage of Video API
  1495. @subsection Example of screen setup
  1496. @example
  1497. grub_err_t rc;
  1498. /* Try to initialize video mode 1024 x 768 with direct RGB. */
  1499. rc = grub_video_setup (1024, 768, GRUB_VIDEO_MODE_TYPE_RGB);
  1500. if (rc != GRUB_ERR_NONE)
  1501. @{
  1502. /* Fall back to standard VGA Index Color mode. */
  1503. rc = grub_video_setup (640, 480, GRUB_VIDEO_MODE_TYPE_INDEX);
  1504. if (rc != GRUB_ERR_NONE)
  1505. @{
  1506. /* Handle error. */
  1507. @}
  1508. @}
  1509. @end example
  1510. @subsection Example of setting up console viewport
  1511. @example
  1512. grub_uint32_t x, y, width, height;
  1513. grub_video_color_t color;
  1514. struct grub_font_glyph glyph;
  1515. grub_err_t rc;
  1516. /* Query existing viewport. */
  1517. grub_video_get_viewport (&x, &y, &width, &height);
  1518. /* Fill background. */
  1519. color = grub_video_map_color (GRUB_COLOR_BACKGROUND);
  1520. grub_video_fill_rect (color, 0, 0, width, height);
  1521. /* Setup console viewport. */
  1522. grub_video_set_viewport (x + 10, y + 10, width - 20, height - 20);
  1523. grub_video_get_viewport (&x, &y, &width, &height);
  1524. color = grub_video_map_color (GRUB_COLOR_CONSOLE_BACKGROUND);
  1525. grub_video_fill_rect (color, 0, 0, width, height);
  1526. /* Draw text to viewport. */
  1527. color = grub_video_map_color (GRUB_COLOR_CONSOLE_TEXT);
  1528. grub_font_get_glyph ('X', &glyph);
  1529. grub_video_blit_glyph (&glyph, color, 0, 0);
  1530. @end example
  1531. @node Bitmap API
  1532. @section Bitmap API
  1533. @subsection grub_video_bitmap_create
  1534. @itemize
  1535. @item Prototype:
  1536. @example
  1537. grub_err_t grub_video_bitmap_create (struct grub_video_bitmap **bitmap, unsigned int width, unsigned int height, enum grub_video_blit_format blit_format)
  1538. @end example
  1539. @item Description:
  1540. Creates a new bitmap with given dimensions and blitting format. Allocated bitmap data can then be modified freely and finally blitted with @code{grub_video_blit_bitmap} to rendering target.
  1541. @end itemize
  1542. @subsection grub_video_bitmap_destroy
  1543. @itemize
  1544. @item Prototype:
  1545. @example
  1546. grub_err_t grub_video_bitmap_destroy (struct grub_video_bitmap *bitmap);
  1547. @end example
  1548. @item Description:
  1549. When bitmap is no longer needed, it can be freed from memory using this command. @code{bitmap} is previously allocated bitmap with @code{grub_video_bitmap_create} or loaded with @code{grub_video_bitmap_load}.
  1550. @end itemize
  1551. @subsection grub_video_bitmap_load
  1552. @itemize
  1553. @item Prototype:
  1554. @example
  1555. grub_err_t grub_video_bitmap_load (struct grub_video_bitmap **bitmap, const char *filename);
  1556. @end example
  1557. @item Description:
  1558. Tries to load given bitmap (@code{filename}) using registered bitmap loaders. In case bitmap format is not recognized or supported error @code{GRUB_ERR_BAD_FILE_TYPE} is returned.
  1559. @end itemize
  1560. @subsection grub_video_bitmap_get_width
  1561. @itemize
  1562. @item Prototype:
  1563. @example
  1564. unsigned int grub_video_bitmap_get_width (struct grub_video_bitmap *bitmap);
  1565. @end example
  1566. @item Description:
  1567. Returns bitmap width.
  1568. @end itemize
  1569. @subsection grub_video_bitmap_get_height
  1570. @itemize
  1571. @item Prototype:
  1572. @example
  1573. unsigned int grub_video_bitmap_get_height (struct grub_video_bitmap *bitmap);
  1574. @end example
  1575. @item Description:
  1576. Return bitmap height.
  1577. @end itemize
  1578. @subsection grub_video_bitmap_get_mode_info
  1579. @itemize
  1580. @item Prototype:
  1581. @example
  1582. void grub_video_bitmap_get_mode_info (struct grub_video_bitmap *bitmap, struct grub_video_mode_info *mode_info);
  1583. @end example
  1584. @item Description:
  1585. Returns bitmap format details in form of @code{grub_video_mode_info}.
  1586. @end itemize
  1587. @subsection grub_video_bitmap_get_data
  1588. @itemize
  1589. @item Prototype:
  1590. @example
  1591. void *grub_video_bitmap_get_data (struct grub_video_bitmap *bitmap);
  1592. @end example
  1593. @item Description:
  1594. Return pointer to bitmap data. Contents of the pointed data can be freely modified. There is no extra protection against going off the bounds so you have to be carefull how to access the data.
  1595. @end itemize
  1596. @node PFF2 Font File Format
  1597. @chapter PFF2 Font File Format
  1598. @c Author: Colin D. Bennett <colin@gibibit.com>
  1599. @c Date: 8 January 2009
  1600. @menu
  1601. * Introduction::
  1602. * File Structure::
  1603. * Font Metrics::
  1604. @end menu
  1605. @node Introduction
  1606. @section Introduction
  1607. The goal of this format is to provide a bitmap font format that is simple to
  1608. use, compact, and cleanly supports Unicode.
  1609. @subsection Goals of the GRUB Font Format
  1610. @itemize
  1611. @item Simple to read and use.
  1612. Since GRUB will only be reading the font files,
  1613. we are more concerned with making the code to read the font simple than we
  1614. are with writing the font.
  1615. @item Compact storage.
  1616. The fonts will generally be stored in a small boot
  1617. partition where GRUB is located, and this may be on a removable storage
  1618. device such as a CD or USB flash drive where space is more limited than it
  1619. is on most hard drives.
  1620. @item Unicode.
  1621. GRUB should not have to deal with multiple character
  1622. encodings. The font should always use Unicode character codes for simple
  1623. internationalization.
  1624. @end itemize
  1625. @subsection Why Another Font Format?
  1626. There are many existing bitmap font formats that GRUB could use. However,
  1627. there are aspects of these formats that may make them less than suitable for
  1628. use in GRUB at this time:
  1629. @table @samp
  1630. @item BDF
  1631. Inefficient storage; uses ASCII to describe properties and
  1632. hexadecimal numbers in ASCII for the bitmap rows.
  1633. @item PCF
  1634. Many format variations such as byte order and bitmap padding (rows
  1635. padded to byte, word, etc.) would result in more complex code to
  1636. handle the font format.
  1637. @end table
  1638. @node File Structure
  1639. @section File Structure
  1640. A file @strong{section} consists of a 4-byte name, a 32-bit big-endian length (not
  1641. including the name or length), and then @var{length} more section-type-specific
  1642. bytes.
  1643. The standard file extension for PFF2 font files is @file{.pf2}.
  1644. @subsection Section Types
  1645. @table @samp
  1646. @item FILE
  1647. @strong{File type ID} (ASCII string). This must be the first section in the file. It has length 4
  1648. and the contents are the four bytes of the ASCII string @samp{PFF2}.
  1649. @item NAME
  1650. @strong{Font name} (ASCII string). This is the full font name including family,
  1651. weight, style, and point size. For instance, "Helvetica Bold Italic 14".
  1652. @item FAMI
  1653. @strong{Font family name} (ASCII string). For instance, "Helvetica". This should
  1654. be included so that intelligent font substitution can take place.
  1655. @item WEIG
  1656. @strong{Font weight} (ASCII string). Valid values are @samp{bold} and @samp{normal}.
  1657. This should be included so that intelligent font substitution can take
  1658. place.
  1659. @item SLAN
  1660. @strong{Font slant} (ASCII string). Valid values are @samp{italic} and @samp{normal}.
  1661. This should be included so that intelligent font substitution can take
  1662. place.
  1663. @item PTSZ
  1664. @strong{Font point size} (uint16be).
  1665. @item MAXW
  1666. @strong{Maximum character width in pixels} (uint16be).
  1667. @item MAXH
  1668. @strong{Maximum character height in pixels} (uint16be).
  1669. @item ASCE
  1670. @strong{Ascent in pixels} (uint16be). @xref{Font Metrics}, for details.
  1671. @item DESC
  1672. @strong{Descent in pixels} (uint16be). @xref{Font Metrics}, for details.
  1673. @item CHIX
  1674. @strong{Character index.}
  1675. The character index begins with a 32-bit big-endian unsigned integer
  1676. indicating the total size of the section, not including this size value.
  1677. For each character, there is an instance of the following entry structure:
  1678. @itemize
  1679. @item @strong{Unicode code point.} (32-bit big-endian integer.)
  1680. @item @strong{Storage flags.} (byte.)
  1681. @itemize
  1682. @item Bits 2..0:
  1683. If equal to 000 binary, then the character data is stored
  1684. uncompressed beginning at the offset indicated by the character's
  1685. @strong{offset} value.
  1686. If equal to 001 binary, then the character data is stored within a
  1687. compressed character definition block that begins at the offset
  1688. within the file indicated by the character's @strong{offset} value.
  1689. @end itemize
  1690. @item @strong{Offset.} (32-bit big-endian integer.)
  1691. A marker that indicates the remainder of the file is data accessed via
  1692. the character index (CHIX) section. When reading this font file, the rest
  1693. of the file can be ignored when scanning the sections. The length should
  1694. be set to -1 (0xFFFFFFFF).
  1695. Supported data structures:
  1696. Character definition
  1697. Each character definition consists of:
  1698. @itemize
  1699. @item @strong{Width.}
  1700. Width of the bitmap in pixels. The bitmap's extents
  1701. represent the glyph's bounding box. @code{uint16be}.
  1702. @item @strong{Height.}
  1703. Height of the bitmap in pixels. The bitmap's extents
  1704. represent the glyph's bounding box. @code{uint16be}.
  1705. @item @strong{X offset.}
  1706. The number of pixels to shift the bitmap by
  1707. horizontally before drawing the character. @code{int16be}.
  1708. @item @strong{Y offset.}
  1709. The number of pixels to shift the bitmap by
  1710. vertically before drawing the character. @code{int16be}.
  1711. @item @strong{Device width.}
  1712. The number of pixels to advance horizontally from
  1713. this character's origin to the origin of the next character.
  1714. @code{int16be}.
  1715. @item @strong{Bitmap data.}
  1716. This is encoded as a string of bits. It is
  1717. organized as a row-major, top-down, left-to-right bitmap. The most
  1718. significant bit of each byte is taken to be the leftmost or uppermost
  1719. bit in the byte. For the sake of compact storage, rows are not padded
  1720. to byte boundaries (i.e., a single byte may contain bits belonging to
  1721. multiple rows). The last byte of the bitmap @strong{is} padded with zero
  1722. bits in the bits positions to the right of the last used bit if the
  1723. bitmap data does not fill the last byte.
  1724. The length of the @strong{bitmap data} field is (@var{width} * @var{height} + 7) / 8
  1725. using integer arithmetic, which is equivalent to ceil(@var{width} *
  1726. @var{height} / 8) using real number arithmetic.
  1727. It remains to be determined whether bitmap fonts usually make all
  1728. glyph bitmaps the same height, or if smaller glyphs are stored with
  1729. bitmaps having a lesser height. In the latter case, the baseline
  1730. would have to be used to calculate the location the bitmap should be
  1731. anchored at on screen.
  1732. @end itemize
  1733. @end itemize
  1734. @end table
  1735. @node Font Metrics
  1736. @section Font Metrics
  1737. @itemize
  1738. @item Ascent.
  1739. The distance from the baseline to the top of most characters.
  1740. Note that in some cases characters may extend above the ascent.
  1741. @item Descent.
  1742. The distance from the baseline to the bottom of most characters. Note that
  1743. in some cases characters may extend below the descent.
  1744. @item Leading.
  1745. The amount of space, in pixels, to leave between the descent of one line of
  1746. text and the ascent of the next line. This metrics is not specified in the
  1747. current file format; instead, the font rendering engine calculates a
  1748. reasonable leading value based on the other font metrics.
  1749. @item Horizonal leading.
  1750. The amount of space, in pixels, to leave horizontally between the left and
  1751. right edges of two adjacent glyphs. The @strong{device width} field determines
  1752. the effective leading value that is used to render the font.
  1753. @end itemize
  1754. @ifnottex
  1755. @image{font_char_metrics,,,,.png}
  1756. @end ifnottex
  1757. An illustration of how the various font metrics apply to characters.
  1758. @node Graphical Menu Software Design
  1759. @chapter Graphical Menu Software Design
  1760. @c By Colin D. Bennett <colin@gibibit.com>
  1761. @c Date: 17 August 2008
  1762. @menu
  1763. * Introduction_2::
  1764. * Startup Sequence::
  1765. * GUI Components::
  1766. * Command Line Window::
  1767. @end menu
  1768. @node Introduction_2
  1769. @section Introduction
  1770. The @samp{gfxmenu} module provides a graphical menu interface for GRUB 2. It
  1771. functions as an alternative to the menu interface provided by the @samp{normal}
  1772. module, which uses the grub terminal interface to display a menu on a
  1773. character-oriented terminal.
  1774. The graphical menu uses the GRUB video API, which is currently for the VESA
  1775. BIOS extensions (VBE) 2.0+. This is supported on the i386-pc platform.
  1776. However, the graphical menu itself does not depend on using VBE, so if another
  1777. GRUB video driver were implemented, the @samp{gfxmenu} graphical menu would work
  1778. on the new video driver as well.
  1779. @node Startup Sequence
  1780. @section Startup Sequence
  1781. @itemize
  1782. @item grub_enter_normal_mode [normal/main.c]
  1783. @item grub_normal_execute [normal/main.c]
  1784. @item read_config_file [normal/main.c]
  1785. @item (When @file{gfxmenu.mod} is loaded with @command{insmod}, it will call @code{grub_menu_viewer_register()} to register itself.)
  1786. @item GRUB_MOD_INIT (gfxmenu) [gfxmenu/gfxmenu.c]
  1787. @item grub_menu_viewer_register [kern/menu_viewer.c]
  1788. @item grub_menu_viewer_show_menu [kern/menu_viewer.c]
  1789. @item get_current_menu_viewer() [kern/menu_viewer.c]
  1790. @item show_menu() [gfxmenu/gfxmenu.c]
  1791. @item grub_gfxmenu_model_new [gfxmenu/model.c]
  1792. @item grub_gfxmenu_view_new [gfxmenu/view.c]
  1793. @item set_graphics_mode [gfxmenu/view.c]
  1794. @item grub_gfxmenu_view_load_theme [gfxmenu/theme_loader.c]
  1795. @end itemize
  1796. @node GUI Components
  1797. @section GUI Components
  1798. The graphical menu implements a GUI component system that supports a
  1799. container-based layout system. Components can be added to containers, and
  1800. containers (which are a type of component) can then be added to other
  1801. containers, to form a tree of components. Currently, the root component of
  1802. this tree is a @samp{canvas} component, which allows manual layout of its child
  1803. components.
  1804. Components (non-container):
  1805. @itemize
  1806. @item label
  1807. @item image
  1808. @item progress_bar
  1809. @item circular_progress
  1810. @item list (currently hard coded to be a boot menu list)
  1811. @end itemize
  1812. Containers:
  1813. @itemize
  1814. @item canvas
  1815. @item hbox
  1816. @item vbox
  1817. @end itemize
  1818. The GUI component instances are created by the theme loader in
  1819. @file{gfxmenu/theme_loader.c} when a theme is loaded. Theme files specify
  1820. statements such as @samp{+vbox@{ +label @{ text="Hello" @} +label@{ text="World" @} @}}
  1821. to add components to the component tree root. By nesting the component
  1822. creation statements in the theme file, the instantiated components are nested
  1823. the same way.
  1824. When a component is added to a container, that new child is considered @strong{owned}
  1825. by the container. Great care should be taken if the caller retains a
  1826. reference to the child component, since it will be destroyed if its parent
  1827. container is destroyed. A better choice instead of storing a pointer to the
  1828. child component is to use the component ID to find the desired component.
  1829. Component IDs do not have to be unique (it is often useful to have multiple
  1830. components with an ID of "__timeout__", for instance).
  1831. In order to access and use components in the component tree, there are two
  1832. functions (defined in @file{gfxmenu/gui_util.c}) that are particularly useful:
  1833. @itemize
  1834. @item @code{grub_gui_find_by_id (root, id, callback, userdata)}:
  1835. This function recursively traverses the component tree rooted at @var{root}, and
  1836. for every component that has an ID equal to @var{id}, calls the function pointed
  1837. to by @var{callback} with the matching component and the void pointer @var{userdata}
  1838. as arguments. The callback function can do whatever is desired to use the
  1839. component passed in.
  1840. @item @code{grub_gui_iterate_recursively (root, callback, userdata)}:
  1841. This function calls the function pointed to by @var{callback} for every
  1842. component that is a descendant of @var{root} in the component tree. When the
  1843. callback function is called, the component and the void pointer @var{userdata}
  1844. as arguments. The callback function can do whatever is desired to use the
  1845. component passed in.
  1846. @end itemize
  1847. @node Command Line Window
  1848. @section Command Line Window
  1849. The terminal window used to provide command line access within the graphical
  1850. menu is managed by @file{gfxmenu/view.c}. The @samp{gfxterm} terminal is used, and
  1851. it has been modified to allow rendering to an offscreen render target to allow
  1852. it to be composed into the double buffering system that the graphical menu
  1853. view uses. This is bad for performance, however, so it would probably be a
  1854. good idea to make it possible to temporarily disable double buffering as long
  1855. as the terminal window is visible. There are still unresolved problems that
  1856. occur when commands are executed from the terminal window that change the
  1857. graphics mode. It's possible that making @code{grub_video_restore()} return to
  1858. the graphics mode that was in use before @code{grub_video_setup()} was called
  1859. might fix some of the problems.
  1860. @node Verifiers framework
  1861. @chapter Verifiers framework
  1862. To register your own verifier call @samp{grub_verifier_register} with a structure
  1863. pointing to your functions.
  1864. The interface is inspired by the hash interface with @samp{init}/@samp{write}/@samp{fini}.
  1865. There are essentially 2 ways of using it, hashing and whole-file verification.
  1866. With the hashing approach:
  1867. During @samp{init} you decide whether you want to check the given file and init context.
  1868. In @samp{write} you update your hashing state.
  1869. In @samp{fini} you check that the hash matches the expected value/passes some check/...
  1870. With whole-file verification:
  1871. During @samp{init} you decide whether you want to check the given file and init context.
  1872. In @samp{write} you verify the file and return an error if it fails.
  1873. You don't have @samp{fini}.
  1874. Additional @samp{verify_string} receives various strings like kernel parameters
  1875. to verify. Returning no error means successful verification and an error stops
  1876. the current action.
  1877. Detailed description of the API:
  1878. Every time a file is opened your @samp{init} function is called with file descriptor
  1879. and file type. Your function can have the following outcomes:
  1880. @itemize
  1881. @item returning no error and setting @samp{*flags} to @samp{GRUB_VERIFY_FLAGS_DEFER_AUTH}.
  1882. In this case verification is deferred to other active verifiers. Verification
  1883. fails if nobody cares or selected verifier fails.
  1884. @item returning no error and setting @samp{*flags} to @samp{GRUB_VERIFY_FLAGS_SKIP_VERIFICATION}.
  1885. In this case your verifier will not be called anymore and it is assumed to have
  1886. skipped verification.
  1887. @item returning no error and not setting @samp{*flags} to @samp{GRUB_VERIFY_FLAGS_SKIP_VERIFICATION}
  1888. In this case verification is done as described in the following section.
  1889. @item returning an error. Then opening of the file will fail due to failed verification.
  1890. @end itemize
  1891. In the third case your @samp{write} will be called with chunks of the file. If
  1892. you need the whole file in a single chunk then during @samp{init} set the bit
  1893. @samp{GRUB_VERIFY_FLAGS_SINGLE_CHUNK} in @samp{*flags}. During @samp{init} you
  1894. may set @samp{*context} if you need additional context. At every iteration you
  1895. may return an error and the file will be considered as having failed the
  1896. verification. If you return no error then verification continues.
  1897. Optionally at the end of the file @samp{fini}, if it exists, is called with just
  1898. the context. If you return no error during any of @samp{init}, @samp{write} and
  1899. @samp{fini} then the file is considered as having succeded verification.
  1900. @node Lockdown framework
  1901. @chapter Lockdown framework
  1902. The GRUB can be locked down, which is a restricted mode where some operations
  1903. are not allowed. For instance, some commands cannot be used when the GRUB is
  1904. locked down.
  1905. The function
  1906. @code{grub_lockdown()} is used to lockdown GRUB and the function
  1907. @code{grub_is_lockdown()} function can be used to check whether lockdown is
  1908. enabled or not. When enabled, the function returns @samp{GRUB_LOCKDOWN_ENABLED}
  1909. and @samp{GRUB_LOCKDOWN_DISABLED} when is not enabled.
  1910. The following functions can be used to register the commands that can only be
  1911. used when lockdown is disabled:
  1912. @itemize
  1913. @item @code{grub_cmd_lockdown()} registers command which should not run when the
  1914. GRUB is in lockdown mode.
  1915. @item @code{grub_cmd_lockdown()} registers extended command which should not run
  1916. when the GRUB is in lockdown mode.
  1917. @end itemize
  1918. @node Copying This Manual
  1919. @appendix Copying This Manual
  1920. @menu
  1921. * GNU Free Documentation License:: License for copying this manual.
  1922. @end menu
  1923. @include fdl.texi
  1924. @node Index
  1925. @unnumbered Index
  1926. @c Currently, we use only the Concept Index.
  1927. @printindex cp
  1928. @bye