grub-dev.texi 93 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627162816291630163116321633163416351636163716381639164016411642164316441645164616471648164916501651165216531654165516561657165816591660166116621663166416651666166716681669167016711672167316741675167616771678167916801681168216831684168516861687168816891690169116921693169416951696169716981699170017011702170317041705170617071708170917101711171217131714171517161717171817191720172117221723172417251726172717281729173017311732173317341735173617371738173917401741174217431744174517461747174817491750175117521753175417551756175717581759176017611762176317641765176617671768176917701771177217731774177517761777177817791780178117821783178417851786178717881789179017911792179317941795179617971798179918001801180218031804180518061807180818091810181118121813181418151816181718181819182018211822182318241825182618271828182918301831183218331834183518361837183818391840184118421843184418451846184718481849185018511852185318541855185618571858185918601861186218631864186518661867186818691870187118721873187418751876187718781879188018811882188318841885188618871888188918901891189218931894189518961897189818991900190119021903190419051906190719081909191019111912191319141915191619171918191919201921192219231924192519261927192819291930193119321933193419351936193719381939194019411942194319441945194619471948194919501951195219531954195519561957195819591960196119621963196419651966196719681969197019711972197319741975197619771978197919801981198219831984198519861987198819891990199119921993199419951996199719981999200020012002200320042005200620072008200920102011201220132014201520162017201820192020202120222023202420252026202720282029203020312032203320342035203620372038203920402041204220432044204520462047204820492050205120522053205420552056205720582059206020612062206320642065206620672068206920702071207220732074207520762077207820792080208120822083208420852086208720882089209020912092209320942095209620972098209921002101210221032104210521062107210821092110211121122113211421152116211721182119212021212122212321242125212621272128212921302131213221332134213521362137213821392140214121422143214421452146214721482149215021512152215321542155215621572158215921602161216221632164216521662167216821692170217121722173217421752176217721782179218021812182218321842185218621872188218921902191219221932194219521962197219821992200220122022203220422052206220722082209221022112212221322142215221622172218221922202221222222232224222522262227222822292230223122322233223422352236223722382239224022412242224322442245224622472248224922502251225222532254225522562257225822592260226122622263226422652266226722682269227022712272227322742275227622772278227922802281228222832284228522862287228822892290229122922293229422952296229722982299230023012302230323042305230623072308230923102311231223132314231523162317231823192320232123222323232423252326232723282329233023312332233323342335233623372338233923402341234223432344234523462347234823492350235123522353235423552356235723582359236023612362236323642365236623672368236923702371237223732374237523762377237823792380238123822383238423852386238723882389239023912392239323942395239623972398239924002401240224032404240524062407240824092410241124122413241424152416241724182419242024212422242324242425242624272428242924302431243224332434243524362437243824392440244124422443244424452446244724482449
  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. * libtasn1::
  390. @end menu
  391. @node Gnulib
  392. @section Gnulib
  393. Gnulib is a source code library that provides basic functionality to
  394. programs and libraries. Many software packages make use of Gnulib
  395. to avoid reinventing the portability wheel.
  396. GRUB imports Gnulib using its @command{bootstrap} utility, identifying a
  397. particular Git commit in @file{bootstrap.conf}. To upgrade to a new Gnulib
  398. commit, set @code{GNULIB_REVISION} in @file{bootstrap.conf} to the new commit
  399. ID, then run @kbd{./bootstrap} and whatever else you need to make sure it
  400. works. Check for changes to Gnulib's @file{NEWS} file between the old and new
  401. commits; in some cases it will be necessary to adjust GRUB to match. You may
  402. also need to update the patches in @file{grub-core/lib/gnulib-patches/}.
  403. To add a new Gnulib module or remove one that is no longer needed, change
  404. @code{gnulib_modules} in @file{bootstrap.conf}. Again, run @kbd{./bootstrap}
  405. and whatever else you need to make sure it works.
  406. Bootstrapping from an older distribution containing gettext version < 0.18.3,
  407. will require a patch similar to this to be applied first before running the
  408. @command{./bootstrap} utility:
  409. @example
  410. diff --git a/bootstrap.conf b/bootstrap.conf
  411. index 988dda0..a3193a9 100644
  412. --- a/bootstrap.conf
  413. +++ b/bootstrap.conf
  414. @@ -67,7 +67,7 @@ SKIP_PO=t
  415. buildreq="\
  416. autoconf 2.63
  417. automake 1.11
  418. -gettext 0.18.3
  419. +gettext 0.17
  420. git 1.5.5
  421. tar -
  422. "
  423. diff --git a/configure.ac b/configure.ac
  424. index 08b518f..99f5b36 100644
  425. --- a/configure.ac
  426. +++ b/configure.ac
  427. @@ -362,7 +362,7 @@ AC_CHECK_PROG(HAVE_CXX, $CXX, yes, no)
  428. AC_GNU_SOURCE
  429. AM_GNU_GETTEXT([external])
  430. -AM_GNU_GETTEXT_VERSION([0.18.3])
  431. +AM_GNU_GETTEXT_VERSION([0.17])
  432. AC_SYS_LARGEFILE
  433. # Identify characteristics of the host architecture.
  434. @end example
  435. It will also be necessary to adjust the patches in
  436. @file{po/gettext-patches/} to apply to an older version of gettext.
  437. @node jsmn
  438. @section jsmn
  439. jsmn is a minimalistic JSON parser which is implemented in a single header file
  440. @file{jsmn.h}. To import a different version of the jsmn parser, you may simply
  441. download the @file{jsmn.h} header from the desired tag or commit to the target
  442. directory:
  443. @example
  444. curl -L https://raw.githubusercontent.com/zserge/jsmn/v1.1.0/jsmn.h \
  445. -o grub-core/lib/json/jsmn.h
  446. @end example
  447. @node minilzo
  448. @section minilzo
  449. miniLZO is a very lightweight subset of the LZO library intended for easy
  450. inclusion in other projects. It is generated automatically from the LZO
  451. source code and contains the most important LZO functions.
  452. To upgrade to a new version of the miniLZO library, download the release
  453. tarball and copy the files into the target directory:
  454. @example
  455. curl -L -O https://www.oberhumer.com/opensource/lzo/download/minilzo-2.10.tar.gz
  456. tar -zxf minilzo-2.10.tar.gz
  457. rm minilzo-2.10/testmini.c
  458. rm -r grub-core/lib/minilzo/*
  459. cp minilzo-2.10/*.[hc] grub-core/lib/minilzo
  460. rm -r minilzo-2.10*
  461. @end example
  462. @node libtasn1
  463. @section libtasn1
  464. libtasn1 is a library providing Abstract Syntax Notation One (ASN.1, as
  465. specified by the X.680 ITU-T recommendation) parsing and structures management,
  466. and Distinguished Encoding Rules (DER, as per X.690) encoding and decoding
  467. functions.
  468. To upgrade to a new version of the libtasn1 library, download the release
  469. tarball and copy the files into the target directory:
  470. @example
  471. curl -L -O https://ftp.gnu.org/gnu/libtasn1/libtasn1-4.19.0.tar.gz
  472. tar xvzf libtasn1-4.19.0.tar.gz
  473. rm -rf grub-core/lib/libtasn1
  474. mkdir -p grub-core/lib/libtasn1/lib
  475. mkdir -p grub-core/lib/libtasn1/tests
  476. cp libtasn1-4.19.0/@lbracechar{}README.md,COPYING@rbracechar{} grub-core/lib/libtasn1
  477. cp libtasn1-4.19.0/lib/@lbracechar{}coding.c,decoding.c,element.c,element.h,errors.c,gstr.c,gstr.h,int.h,parser_aux.c,parser_aux.h,structure.c,structure.h@rbracechar{} grub-core/lib/libtasn1/lib
  478. cp libtasn1-4.19.0/lib/includes/libtasn1.h grub-core/lib/libtasn1
  479. cp libtasn1-4.19.0/tests/@lbracechar{}CVE-2018-1000654-1_asn1_tab.h,CVE-2018-1000654-2_asn1_tab.h,CVE-2018-1000654.c,object-id-decoding.c,object-id-encoding.c,octet-string.c,reproducers.c,Test_overflow.c,Test_simple.c,Test_strings.c@rbracechar{} grub-core/lib/libtasn1/tests
  480. rm -rf libtasn1-4.19.0*
  481. @end example
  482. After upgrading the library, it may be necessary to apply the patches in
  483. @file{grub-core/lib/libtasn1-patches/} to adjust the code to be compatible with
  484. GRUB. These patches were needed to use the current version of libtasn1. The
  485. existing patches may not apply cleanly, apply at all, or even be needed for a
  486. newer version of the library, and other patches may be needed due to changes in
  487. the newer version. If existing patches need to be refreshed to apply cleanly,
  488. please include updated patches as part of the a patch set sent to the list.
  489. If new patches are needed or existing patches are not needed, also please send
  490. additions or removals as part of any patch set upgrading libtasn1.
  491. @node Debugging
  492. @chapter Debugging
  493. GRUB2 can be difficult to debug because it runs on the bare-metal and thus
  494. does not have the debugging facilities normally provided by an operating
  495. system. This chapter aims to provide useful information on some ways to
  496. debug GRUB2 for some architectures. It by no means intends to be exhaustive.
  497. The focus will be one x86_64 and i386 architectures. Luckily for some issues
  498. virtual machines have made the ability to debug GRUB2 much easier, and this
  499. chapter will focus debugging via the QEMU virtual machine. We will not be
  500. going over debugging of the userland tools (eg. grub-install), there are
  501. many tutorials on debugging programs in userland.
  502. You will need GDB and the QEMU binaries for your system, on Debian these
  503. can be installed with the @samp{gdb} and @samp{qemu-system-x86} packages.
  504. Also it is assumed that you have already successfully compiled GRUB2 from
  505. source for the target specified in the section below and have some
  506. familiarity with GDB. When GRUB2 is built it will create many different
  507. binaries. The ones of concern will be in the @file{grub-core}
  508. directory of the GRUB2 build dir. To aide in debugging we will want the
  509. debugging symbols generated during the build because these symbols are not
  510. kept in the binaries which get installed to the boot location. The build
  511. process outputs two sets of binaries, one without symbols which gets executed
  512. at boot, and another set of ELF images with debugging symbols. The built
  513. images with debugging symbols will have a @file{.image} suffix, and the ones
  514. without a @file{.img} suffix. Similarly, loadable modules with debugging
  515. symbols will have a @file{.module} suffix, and ones without a @file{.mod}
  516. suffix. In the case of the kernel the binary with symbols is named
  517. @file{kernel.exec}.
  518. In the following sections, information will be provided on debugging on
  519. various targets using @command{gdb} and the @samp{gdb_grub} GDB script.
  520. @menu
  521. * i386-pc::
  522. * x86_64-efi::
  523. @end menu
  524. @node i386-pc
  525. @section i386-pc
  526. The i386-pc target is a good place to start when first debugging GRUB2
  527. because in some respects it's easier than EFI platforms. The reason being
  528. that the initial load address is always known in advance. To start
  529. debugging GRUB2 first QEMU must be started in GDB stub mode. The following
  530. command is a simple illustration:
  531. @example
  532. qemu-system-i386 -drive file=disk.img,format=raw \
  533. -device virtio-scsi-pci,id=scsi0 -S -s
  534. @end example
  535. This will start a QEMU instance booting from @file{disk.img}. It will pause
  536. at start waiting for a GDB instance to attach to it. You should change
  537. @file{disk.img} to something more appropriate. A block device can be used,
  538. but you may need to run QEMU as a privileged user.
  539. To connect to this QEMU instance with GDB, the @code{target remote} GDB
  540. command must be used. We also need to load a binary image, preferably with
  541. symbols. This can be done using the GDB command @code{file kernel.exec}, if
  542. GDB is started from the @file{grub-core} directory in the GRUB2 build
  543. directory. GRUB2 developers have made this more simple by including a GDB
  544. script which does much of the setup. This file is at @file{grub-core/gdb_grub}
  545. in the build directory and is also installed via @command{make install}.
  546. When using a pre-built GRUB, the distribution may have a package which installs
  547. this GDB script along with debug symbol binaries, such as Debian's
  548. @samp{grub-pc-dbg} package. The GDB script is intended to be used
  549. like so, assuming that @samp{/path/to/script} is the path to the directory
  550. containing the gdb_grub script and debug symbol files:
  551. @example
  552. cd $(dirname /path/to/script/gdb_grub)
  553. gdb -x gdb_grub
  554. @end example
  555. Once GDB has been started with the @file{gdb_grub} script it will
  556. automatically connect to the QEMU instance. You can then do things you
  557. normally would in GDB like set a break point on @var{grub_main}.
  558. Setting breakpoints in modules is trickier since they haven't been loaded
  559. yet and are loaded at addresses determined at runtime. The module could be
  560. loaded to different addresses in different QEMU instances. The debug symbols
  561. in the modules @file{.module} binary, thus are always wrong, and GDB needs
  562. to be told where to load the symbols to. But this must happen at runtime
  563. after GRUB2 has determined where the module will get loaded. Luckily the
  564. @file{gdb_grub} script takes care of this with the @command{runtime_load_module}
  565. command, which configures GDB to watch for GRUB2 module loading and when
  566. it does add the module symbols with the appropriate offset.
  567. @node x86_64-efi
  568. @section x86_64-efi
  569. Using GDB to debug GRUB2 for the x86_64-efi target has some similarities with
  570. the i386-pc target. Please read and familiarize yourself with the @ref{i386-pc}
  571. section when reading this one. Extra care must be used to run QEMU such that it
  572. boots a UEFI firmware. This usually involves either using the @samp{-bios}
  573. option with a UEFI firmware blob (eg. @file{OVMF.fd}) or loading the firmware
  574. via pflash. This document will not go further into how to do this as there are
  575. ample resource on the web.
  576. Like all EFI implementations, on x86_64-efi the (U)EFI firmware that loads
  577. the GRUB2 EFI application determines at runtime where the application will
  578. be loaded. This means that we do not know where to tell GDB to load the
  579. symbols for the GRUB2 core until the (U)EFI firmware determines it. There are
  580. two good ways of figuring this out when running in QEMU: use a @ref{OVMF debug log,
  581. debug build of OVMF} and check the debug log, or have GRUB2 say where it is
  582. loaded. Neither of these are ideal because they both generally give the
  583. information after GRUB2 is already running, which makes debugging early boot
  584. infeasible. Technically, the first method does give the load address before
  585. GRUB2 is run, but without debugging the EFI firmware with symbols, the author
  586. currently does not know how to cause the OVMF firmware to pause at that point
  587. to use the load address before GRUB2 is run.
  588. Even after getting the application load address, the loading of core symbols
  589. is complicated by the fact that the debugging symbols for the kernel are in
  590. an ELF binary named @file{kernel.exec} while what is in memory are sections
  591. for the PE32+ EFI binary. When @command{grub-mkimage} creates the PE32+
  592. binary it condenses several segments from the ELF kernel binary into one
  593. .data section in the PE32+ binary. This must be taken into account to
  594. properly load the other non-text sections. Otherwise, GDB will work as
  595. expected when breaking on functions, but, for instance, global variables
  596. will point to the wrong address in memory and thus give incorrect values
  597. (which can be difficult to debug).
  598. Calculating the correct offsets for sections is taken care of automatically
  599. when loading the kernel symbols via the user-defined GDB command
  600. @command{dynamic_load_kernel_exec_symbols}, which takes one argument, the
  601. address where the text section is loaded as determined by one of the methods
  602. above. Alternatively, the command @command{dynamic_load_symbols} with the text
  603. section address as an agrument can be called to load the kernel symbols and set
  604. up loading the module symbols as they are loaded at runtime.
  605. In the author's experience, when debugging with QEMU and OVMF, to have
  606. debugging symbols loaded at the start of GRUB2 execution the GRUB2 EFI
  607. application must be run via QEMU at least once prior in order to get the
  608. load address. Two methods for obtaining the load address are described in
  609. two subsections below. Generally speaking, the load address does not change
  610. between QEMU runs. There are exceptions to this, namely that different
  611. GRUB2 EFI applications can be run at different addresses. Also, it has been
  612. observed that after running the EFI application for the first time, the
  613. second run will sometimes have a different load address, but subsequent
  614. runs of the same EFI application will have the same load address as the
  615. second run. And it's a near certainty that if the GRUB EFI binary has changed,
  616. eg. been recompiled, the load address will also be different.
  617. This ability to predict what the load address will be allows one to assume
  618. the load address on subsequent runs and thus load the symbols before GRUB2
  619. starts. The following command illustrates this, assuming that QEMU is
  620. running and waiting for a debugger connection and the current working
  621. directory is where @file{gdb_grub} resides:
  622. @example
  623. gdb -x gdb_grub -ex 'dynamic_load_symbols @var{address of .text section}'
  624. @end example
  625. If you load the symbols in this manner and, after continuing execution, do
  626. not see output showing the module symbols loading, then it is very likely
  627. that the load address was incorrect.
  628. Another thing to be aware of is how the loading of the GRUB image by the
  629. firmware affects previously set software breakpoints. On x86 platforms,
  630. software breakpoints are implemented by GDB by writing a special processor
  631. instruction at the location of the desired breakpoint. This special instruction
  632. when executed will stop the program execution and hand control to the
  633. debugger, GDB. GDB will first save the instruction bytes that are
  634. overwritten at the breakpoint and will put them back when the breakpoint
  635. is hit. If GRUB is being run for the first time in QEMU, the firmware will
  636. be loading the GRUB image into memory where every byte is already set to 0.
  637. This means that if a breakpoint is set before GRUB is loaded, GDB will save
  638. the 0-byte(s) where the the special instruction will go. Then when the firmware
  639. loads the GRUB image and because it is unaware of the debugger, it will
  640. write the GRUB image to memory, overwriting anything that was there previously ---
  641. notably in this case the instruction that implements the software breakpoint.
  642. This will be confusing for the person using GDB because GDB will show the
  643. breakpoint as set, but the brekapoint will never be hit. Furthermore, GDB
  644. then becomes confused, such that even deleting an recreating the breakpoint
  645. will not create usable breakpoints. The @file{gdb_grub} script takes care of
  646. this by saving the breakpoints just before they are overwritten, and then
  647. restores them at the start of GRUB execution. So breakpoints for GRUB can be
  648. set before GRUB is loaded, but be mindful of this effect if you are confused
  649. as to why breakpoints are not getting hit.
  650. Also note, that hardware breakpoints do not suffer this problem. They are
  651. implemented by having the breakpoint address in special debug registers on
  652. the CPU. So they can always be set freely without regard to whether GRUB has
  653. been loaded or not. The reason that hardware breakpoints aren't always used
  654. is because there are a limited number of them, usually around 4 on various
  655. CPUs, and specifically exactly 4 for x86 CPUs. The @file{gdb_grub} script goes
  656. out of its way to avoid using hardware breakpoints internally and uses them as
  657. briefly as possible when needed, thus allowing the user to have a maximal
  658. number at their disposal.
  659. @menu
  660. * OVMF debug log::
  661. * Using the gdbinfo command::
  662. @end menu
  663. @node OVMF debug log
  664. @subsection OVMF debug log
  665. In order to get the GRUB2 load address from OVMF, first, a debug build
  666. of OVMF must be obtained (@uref{https://github.com/retrage/edk2-nightly/raw/master/bin/DEBUGX64_OVMF.fd,
  667. here is one} which is not officially recommended). OVMF will output debug
  668. messages to a special serial device, which we must add to QEMU. The following
  669. QEMU command will run the debug OVMF and write the debug messages to a
  670. file named @file{debug.log}. It is assumed that @file{disk.img} is a disk
  671. image or block device that is set up to boot GRUB2 EFI.
  672. @example
  673. qemu-system-x86_64 -bios /path/to/debug/OVMF.fd \
  674. -drive file=disk.img,format=raw \
  675. -device virtio-scsi-pci,id=scsi0 \
  676. -debugcon file:debug.log -global isa-debugcon.iobase=0x402
  677. @end example
  678. If GRUB2 was started by the (U)EFI firmware, then in the @file{debug.log}
  679. file one of the last lines should be a log message like:
  680. @samp{Loading driver at 0x00006AEE000 EntryPoint=0x00006AEE756}. This
  681. means that the GRUB2 EFI application was loaded at @samp{0x00006AEE000} and
  682. its .text section is at @samp{0x00006AEE756}.
  683. @node Using the gdbinfo command
  684. @subsection Using the gdbinfo command
  685. On EFI platforms the command @command{gdbinfo} will output a string that
  686. is to be run in a GDB session running with the @file{gdb_grub} GDB script.
  687. @node Porting
  688. @chapter Porting
  689. GRUB2 is designed to be easily portable accross platforms. But because of the
  690. nature of bootloader every new port must be done separately. Here is how I did
  691. MIPS (loongson and ARC) and Xen ports. Note than this is more of suggestions,
  692. not absolute truth.
  693. First of all grab any architecture specifications you can find in public
  694. (please avoid NDA).
  695. First stage is ``Hello world''. I've done it outside of GRUB for simplicity.
  696. Your task is to have a small program which is loadable as bootloader and
  697. clearly shows its presence to you. If you have easily accessible console
  698. you can just print a message. If you have a mapped framebuffer you know address
  699. of, you can draw a square. If you have a debug facility, just hanging without
  700. crashing might be enough. For the first stage you can choose to load the
  701. bootloader across the network since format for network image is often easier
  702. than for local boot and it skips the need of small intermediary stages and
  703. nvram handling. Additionally you can often have a good idea of the needed
  704. format by running ``file'' on any netbootable executable for given platform.
  705. This program should probably have 2 parts: an assembler and C one. Assembler one
  706. handles BSS cleaning and other needed setup (on some platforms you may need
  707. to switch modes or copy the executable to its definitive position). So your code
  708. may look like (x86 assembly for illustration purposes)
  709. @example
  710. .globl _start
  711. _start:
  712. movl $_bss_start, %edi
  713. movl $_end, %ecx
  714. subl %edi, %ecx
  715. xorl %eax, %eax
  716. cld
  717. rep
  718. stosb
  719. call main
  720. @end example
  721. @example
  722. static const char msg[] = "Hello, world";
  723. void
  724. putchar (int c)
  725. @{
  726. ...
  727. @}
  728. void
  729. main (void)
  730. @{
  731. const char *ptr = msg;
  732. while (*ptr)
  733. putchar (*ptr++);
  734. while (1);
  735. @}
  736. @end example
  737. Sometimes you need a third file: assembly stubs for ABI-compatibility.
  738. Once this file is functional it's time to move it into GRUB2. The startup
  739. assembly file goes to grub-core/kern/$cpu/$platform/startup.S. You should also
  740. include grub/symbol.h and replace call to entry point with call to
  741. EXT_C(grub_main). The C file goes to grub-core/kern/$cpu/$platform/init.c
  742. and its entry point is renamed to void grub_machine_init (void). Keep final
  743. infinite loop for now. Stubs file if any goes to
  744. grub-core/kern/$cpu/$platform/callwrap.S. Sometimes either $cpu or $platform
  745. is dropped if file is used on several cpus respectivelyplatforms.
  746. Check those locations if they already have what you're looking for.
  747. Then modify in configure.ac the following parts:
  748. CPU names:
  749. @example
  750. case "$target_cpu" in
  751. i[[3456]]86) target_cpu=i386 ;;
  752. amd64) target_cpu=x86_64 ;;
  753. sparc) target_cpu=sparc64 ;;
  754. s390x) target_cpu=s390 ;;
  755. ...
  756. esac
  757. @end example
  758. Sometimes CPU have additional architecture names which don't influence booting.
  759. You might want to have some canonical name to avoid having bunch of identical
  760. platforms with different names.
  761. NOTE: it doesn't influence compile optimisations which depend solely on
  762. chosen compiler and compile options.
  763. @example
  764. if test "x$with_platform" = x; then
  765. case "$target_cpu"-"$target_vendor" in
  766. i386-apple) platform=efi ;;
  767. i386-*) platform=pc ;;
  768. x86_64-apple) platform=efi ;;
  769. x86_64-*) platform=pc ;;
  770. powerpc-*) platform=ieee1275 ;;
  771. ...
  772. esac
  773. else
  774. ...
  775. fi
  776. @end example
  777. This part deals with guessing the platform from CPU and vendor. Sometimes you
  778. need to use 32-bit mode for booting even if OS runs in 64-bit one. If so add
  779. your platform to:
  780. @example
  781. case "$target_cpu"-"$platform" in
  782. x86_64-efi) ;;
  783. x86_64-emu) ;;
  784. x86_64-*) target_cpu=i386 ;;
  785. powerpc64-ieee1275) target_cpu=powerpc ;;
  786. esac
  787. @end example
  788. Add your platform to the list of supported ones:
  789. @example
  790. case "$target_cpu"-"$platform" in
  791. i386-efi) ;;
  792. x86_64-efi) ;;
  793. i386-pc) ;;
  794. i386-multiboot) ;;
  795. i386-coreboot) ;;
  796. ...
  797. esac
  798. @end example
  799. If explicit -m32 or -m64 is needed add it to:
  800. @example
  801. case "$target_cpu" in
  802. i386 | powerpc) target_m32=1 ;;
  803. x86_64 | sparc64) target_m64=1 ;;
  804. esac
  805. @end example
  806. Finally you need to add a conditional to the following block:
  807. @example
  808. AM_CONDITIONAL([COND_mips_arc], [test x$target_cpu = xmips -a x$platform = xarc])
  809. AM_CONDITIONAL([COND_sparc64_ieee1275], [test x$target_cpu = xsparc64 -a x$platform = xieee1275])
  810. AM_CONDITIONAL([COND_powerpc_ieee1275], [test x$target_cpu = xpowerpc -a x$platform = xieee1275])
  811. @end example
  812. Next stop is gentpl.py. You need to add your platform to the list of supported
  813. ones (sorry that this list is duplicated):
  814. @example
  815. GRUB_PLATFORMS = [ "emu", "i386_pc", "i386_efi", "i386_qemu", "i386_coreboot",
  816. "i386_multiboot", "i386_ieee1275", "x86_64_efi",
  817. "mips_loongson", "sparc64_ieee1275",
  818. "powerpc_ieee1275", "mips_arc", "ia64_efi",
  819. "mips_qemu_mips", "s390_mainframe" ]
  820. @end example
  821. You may also want already to add new platform to one or several of available
  822. groups. In particular we always have a group for each CPU even when only
  823. one platform for given CPU is available.
  824. Then comes grub-core/Makefile.core.def. In the block ``kernel'' you'll need
  825. to define ldflags for your platform ($cpu_$platform_ldflags). You also need to
  826. declare startup asm file ($cpu_$platform_startup) as well as any other files
  827. (e.g. init.c and callwrap.S) (e.g. $cpu_$platform = kern/$cpu/$platform/init.c).
  828. At this stage you will also need to add dummy dl.c and cache.S with functions
  829. grub_err_t grub_arch_dl_check_header (void *ehdr), grub_err_t
  830. grub_arch_dl_relocate_symbols (grub_dl_t mod, void *ehdr) (dl.c) and
  831. void grub_arch_sync_caches (void *address, grub_size_t len) (cache.S). They
  832. won't be used for now.
  833. You will need to create directory include/$cpu/$platform and a file
  834. include/$cpu/types.h. The latter following this template:
  835. @example
  836. #ifndef GRUB_TYPES_CPU_HEADER
  837. #define GRUB_TYPES_CPU_HEADER 1
  838. /* The size of void *. */
  839. #define GRUB_TARGET_SIZEOF_VOID_P 4
  840. /* The size of long. */
  841. #define GRUB_TARGET_SIZEOF_LONG 4
  842. /* mycpu is big-endian. */
  843. #define GRUB_TARGET_WORDS_BIGENDIAN 1
  844. /* Alternatively: mycpu is little-endian. */
  845. #undef GRUB_TARGET_WORDS_BIGENDIAN
  846. #endif /* ! GRUB_TYPES_CPU_HEADER */
  847. @end example
  848. You will also need to add a dummy file to datetime and setjmp modules to
  849. avoid any of it having no files. It can be just completely empty at this stage.
  850. You'll need to make grub-mkimage.c (util/grub_mkimage.c) aware of the needed
  851. format. For most commonly used formats like ELF, PE, aout or raw the support
  852. is already present and you'll need to make it follow the existant code paths
  853. for your platform adding adjustments if necessary. When done compile:
  854. @example
  855. ./bootstrap
  856. ./configure --target=$cpu --with-platform=$platform TARGET_CC=.. OBJCOPY=... STRIP=...
  857. make > /dev/null
  858. @end example
  859. And create image
  860. @example
  861. ./grub-mkimage -d grub-core -O $format_id -o test.img
  862. @end example
  863. And it's time to test your test.img.
  864. If it works next stage is to have heap, console and timer.
  865. To have the heap working you need to determine which regions are suitable for
  866. heap usage, allocate them from firmware and map (if applicable). Then call
  867. grub_mm_init_region (void *start, grub_size_t s) for every of this region.
  868. As a shortcut for early port you can allocate right after _end or have
  869. a big static array for heap. If you do you'll probably need to come back to
  870. this later. As for output console you should distinguish between an array of
  871. text, terminfo or graphics-based console. Many of real-world examples don't
  872. fit perfectly into any of these categories but one of the models is easier
  873. to be used as base. In second and third case you should add your platform to
  874. terminfokernel respectively videoinkernel group. A good example of array of
  875. text is i386-pc (kern/i386/pc/init.c and term/i386/pc/console.c).
  876. Of terminfo is ieee1275 (kern/ieee1275/init.c and term/ieee1275/console.c).
  877. Of video is loongson (kern/mips/loongson/init.c). Note that terminfo has
  878. to be inited in 2 stages: one before (to get at least rudimentary console
  879. as early as possible) and another after the heap (to get full-featured console).
  880. For the input there are string of keys, terminfo and direct hardware. For string
  881. of keys look at i386-pc (same files), for terminfo ieee1275 (same files) and for
  882. hardware loongson (kern/mips/loongson/init.c and term/at_keyboard.c).
  883. For the timer you'll need to call grub_install_get_time_ms (...) with as sole
  884. argument a function returning a grub_uint64_t of a number of milliseconds
  885. elapsed since arbitrary point in the past.
  886. Once these steps accomplished you can remove the inifinite loop and you should
  887. be able to get to the minimal console. Next step is to have module loading
  888. working. For this you'll need to fill kern/$cpu/dl.c and kern/$cpu/cache.S
  889. with real handling of relocations and respectively the real sync of I and D
  890. caches. Also you'll need to decide where in the image to store the modules.
  891. Usual way is to have it concatenated at the end. In this case you'll need to
  892. modify startup.S to copy modules out of bss to let's say ALIGN_UP (_end, 8)
  893. before cleaning out bss. You'll probably find useful to add total_module_size
  894. field to startup.S. In init.c you need to set grub_modbase to the address
  895. where modules can be found. You may need grub_modules_get_end () to avoid
  896. declaring the space occupied by modules as usable for heap. You can test modules
  897. with:
  898. @example
  899. ./grub-mkimage -d grub-core -O $format_id -o test.img hello
  900. @end example
  901. and then running ``hello'' in the shell.
  902. Once this works, you should think of implementing disk access. Look around
  903. disk/ for examples.
  904. Then, very importantly, you probably need to implement the actual loader
  905. (examples available in loader/)
  906. Last step to have minimally usable port is to add support to grub-install to
  907. put GRUB in a place where firmware or platform will pick it up.
  908. Next steps are: filling datetime.c, setjmp.S, network (net/drivers),
  909. video (video/), halt (lib/), reboot (lib/).
  910. Please add your platform to Platform limitations and Supported kernels chapter
  911. in user documentation and mention any steps you skipped which result in reduced
  912. features or performance. Here is the quick checklist of features. Some of them
  913. are less important than others and skipping them is completely ok, just needs
  914. to be mentioned in user documentation.
  915. Checklist:
  916. @itemize
  917. @item Is heap big enough?
  918. @item Which charset is supported by console?
  919. @item Does platform have disk driver?
  920. @item Do you have network card support?
  921. @item Are you able to retrieve datetime (with date)?
  922. @item Are you able to set datetime (with date)?
  923. @item Is serial supported?
  924. @item Do you have direct disk support?
  925. @item Do you have direct keyboard support?
  926. @item Do you have USB support?
  927. @item Do you support loading through network?
  928. @item Do you support loading from disk?
  929. @item Do you support chainloading?
  930. @item Do you support network chainloading?
  931. @item Does cpuid command supports checking all
  932. CPU features that the user might want conditionalise on
  933. (64-bit mode, hypervisor,...)
  934. @item Do you support hints? How reliable are they?
  935. @item Does platform have ACPI? If so do ``acpi'' and ``lsacpi'' modules work?
  936. @item Do any of platform-specific operations mentioned in the relevant section of
  937. user manual makes sense on your platform?
  938. @item Does your platform support PCI? If so is there an appropriate driver for
  939. GRUB?
  940. @item Do you support badram?
  941. @end itemize
  942. @node Error Handling
  943. @chapter Error Handling
  944. Error handling in GRUB 2 is based on exception handling model. As C language
  945. doesn't directly support exceptions, exception handling behavior is emulated
  946. in software.
  947. When exception is raised, function must return to calling function. If calling
  948. function does not provide handling of the exception it must return back to its
  949. calling function and so on, until exception is handled. If exception is not
  950. handled before prompt is displayed, error message will be shown to user.
  951. Exception information is stored on @code{grub_errno} global variable. If
  952. @code{grub_errno} variable contains value @code{GRUB_ERR_NONE}, there is no active
  953. exception and application can continue normal processing. When @code{grub_errno} has
  954. other value, it is required that application code either handles this error or
  955. returns instantly to caller. If function is with return type @code{grub_err_t} is
  956. about to return @code{GRUB_ERR_NONE}, it should not set @code{grub_errno} to that
  957. value. Only set @code{grub_errno} in cases where there is error situation.
  958. Simple exception forwarder.
  959. @example
  960. grub_err_t
  961. forwarding_example (void)
  962. @{
  963. /* Call function that might cause exception. */
  964. foobar ();
  965. /* No special exception handler, just forward possible exceptions. */
  966. if (grub_errno != GRUB_ERR_NONE)
  967. @{
  968. return grub_errno;
  969. @}
  970. /* All is OK, do more processing. */
  971. /* Return OK signal, to caller. */
  972. return GRUB_ERR_NONE;
  973. @}
  974. @end example
  975. Error reporting has two components, the actual error code (of type
  976. @code{grub_err_t}) and textual message that will be displayed to user. List of
  977. valid error codes is listed in header file @file{include/grub/err.h}. Textual
  978. error message can contain any textual data. At time of writing, error message
  979. can contain up to 256 characters (including terminating NUL). To ease error
  980. reporting there is a helper function @code{grub_error} that allows easier
  981. formatting of error messages and should be used instead of writing directly to
  982. global variables.
  983. Example of error reporting.
  984. @example
  985. grub_err_t
  986. failing_example ()
  987. @{
  988. return grub_error (GRUB_ERR_FILE_NOT_FOUND,
  989. "Failed to read %s, tried %d times.",
  990. "test.txt",
  991. 10);
  992. @}
  993. @end example
  994. If there is a special reason that error code does not need to be taken account,
  995. @code{grub_errno} can be zeroed back to @code{GRUB_ERR_NONE}. In cases like this all
  996. previous error codes should have been handled correctly. This makes sure that
  997. there are no unhandled exceptions.
  998. Example of zeroing @code{grub_errno}.
  999. @example
  1000. grub_err_t
  1001. probe_example ()
  1002. @{
  1003. /* Try to probe device type 1. */
  1004. probe_for_device ();
  1005. if (grub_errno == GRUB_ERR_NONE)
  1006. @{
  1007. /* Device type 1 was found on system. */
  1008. register_device ();
  1009. return GRUB_ERR_NONE;
  1010. @}
  1011. /* Zero out error code. */
  1012. grub_errno = GRUB_ERR_NONE;
  1013. /* No device type 1 found, try to probe device type 2. */
  1014. probe_for_device2 ();
  1015. if (grub_errno == GRUB_ERR_NONE)
  1016. @{
  1017. /* Device type 2 was found on system. */
  1018. register_device2 ();
  1019. return GRUB_ERR_NONE;
  1020. @}
  1021. /* Zero out error code. */
  1022. grub_errno = GRUB_ERR_NONE;
  1023. /* Return custom error message. */
  1024. return grub_error (GRUB_ERR_UNKNOWN_DEVICE, "No device type 1 or 2 found.");
  1025. @}
  1026. @end example
  1027. Some times there is a need to continue processing even if there is a error
  1028. state in application. In situations like this, there is a needed to save old
  1029. error state and then call other functions that might fail. To aid in this,
  1030. there is a error stack implemented. Error state can be pushed to error stack
  1031. by calling function @code{grub_error_push ()}. When processing has been completed,
  1032. @code{grub_error_pop ()} can be used to pop error state from stack. Error stack
  1033. contains predefined amount of error stack items. Error stack is protected for
  1034. overflow and marks these situations so overflow error does not get unseen.
  1035. If there is no space available to store error message, it is simply discarded
  1036. and overflow will be marked as happened. When overflow happens, it most likely
  1037. will corrupt error stack consistency as for pushed error there is no matching
  1038. pop, but overflow message will be shown to inform user about the situation.
  1039. Overflow message will be shown at time when prompt is about to be drawn.
  1040. Example usage of error stack.
  1041. @example
  1042. /* Save possible old error message. */
  1043. grub_error_push ();
  1044. /* Do your stuff here. */
  1045. call_possibly_failing_function ();
  1046. if (grub_errno != GRUB_ERR_NONE)
  1047. @{
  1048. /* Inform rest of the code that there is error (grub_errno
  1049. is set). There is no pop here as we want both error states
  1050. to be displayed. */
  1051. return;
  1052. @}
  1053. /* Restore old error state by popping previous item from stack. */
  1054. grub_error_pop ();
  1055. @end example
  1056. @node Stack and heap size
  1057. @chapter Stack and heap size
  1058. On emu stack and heap are just normal host OS stack and heap. Stack is typically
  1059. 8 MiB although it's OS-dependent.
  1060. On i386-pc, i386-coreboot, i386-qemu and i386-multiboot the stack is 60KiB.
  1061. All available space between 1MiB and 4GiB marks is part of heap.
  1062. On *-xen stack is 4MiB. If compiled for x86-64 with GCC 4.4 or later addressable
  1063. space is unlimited. When compiled for x86-64 with older GCC version addressable
  1064. space is limited to 2GiB. When compiling for i386 addressable space is limited
  1065. to 4GiB. All addressable pages except the ones for stack, GRUB binary, special
  1066. pages and page table are in the heap.
  1067. On *-efi GRUB uses same stack as EFI. If compiled for x86-64 with GCC 4.4 or
  1068. later addressable space is unlimited. When compiled for x86-64 with older GCC
  1069. version addressable space is limited to 2GiB. For all other platforms addressable
  1070. space is limited to 4GiB. GRUB allocates pages from EFI for its heap, at most
  1071. 1.6 GiB.
  1072. On i386-ieee1275 and powerpc-ieee1275 GRUB uses same stack as IEEE1275.
  1073. On i386-ieee1275 and powerpc-ieee1275, GRUB will allocate 32MiB for its heap on
  1074. startup. It may allocate more at runtime, as long as at least 128MiB remain free
  1075. in OpenFirmware.
  1076. On sparc64-ieee1275 stack is 256KiB and heap is 2MiB.
  1077. On mips(el)-qemu_mips and mipsel-loongson stack is 2MiB (everything below
  1078. GRUB image) and everything above GRUB image (from 2MiB + kernel size)
  1079. until 256MiB is part of heap.
  1080. On mips-arc stack is 2MiB (everything below GRUB image) and everything above
  1081. GRUB image(from 2MiB + kernel size) until 128MiB is part of heap.
  1082. On mipsel-arc stack is 2MiB (everything below GRUB image which is not part
  1083. of ARC) and everything above GRUB image (from 7MiB + kernel size)
  1084. until 256MiB is part of heap.
  1085. On arm-uboot stack is 256KiB and heap is 2MiB.
  1086. In short:
  1087. @multitable @columnfractions .15 .25 .5
  1088. @headitem Platform @tab Stack @tab Heap
  1089. @item emu @tab 8 MiB @tab ?
  1090. @item i386-pc @tab 60 KiB @tab < 4 GiB
  1091. @item i386-coreboot @tab 60 KiB @tab < 4 GiB
  1092. @item i386-multiboot @tab 60 KiB @tab < 4 GiB
  1093. @item i386-qemu @tab 60 KiB @tab < 4 GiB
  1094. @item *-efi @tab ? @tab < 1.6 GiB
  1095. @item i386-ieee1275 @tab ? @tab < 32 MiB
  1096. @item powerpc-ieee1275 @tab ? @tab available memory - 128MiB
  1097. @item sparc64-ieee1275 @tab 256KiB @tab 2 MiB
  1098. @item arm-uboot @tab 256KiB @tab 2 MiB
  1099. @item mips(el)-qemu_mips @tab 2MiB @tab 253 MiB
  1100. @item mipsel-loongson @tab 2MiB @tab 253 MiB
  1101. @item mips-arc @tab 2MiB @tab 125 MiB
  1102. @item mipsel-arc @tab 2MiB @tab 248 MiB
  1103. @item x86_64-xen (GCC >= 4.4) @tab 4MiB @tab unlimited
  1104. @item x86_64-xen (GCC < 4.4) @tab 4MiB @tab < 2GiB
  1105. @item i386-xen @tab 4MiB @tab < 4GiB
  1106. @end multitable
  1107. @node BIOS port memory map
  1108. @chapter BIOS port memory map
  1109. @c By Yoshinori K Okuji
  1110. @multitable @columnfractions .15 .25 .5
  1111. @headitem Start @tab End @tab Usage
  1112. @item 0 @tab 0x1000 - 1 @tab BIOS and real mode interrupts
  1113. @item 0x07BE @tab 0x07FF @tab Partition table passed to another boot loader
  1114. @item ? @tab 0x2000 - 1 @tab Real mode stack
  1115. @item 0x7C00 @tab 0x7D00 - 1 @tab Boot sector
  1116. @item 0x8000 @tab ? @tab GRUB kernel
  1117. @item 0x68000 @tab 0x71000 - 1 @tab Disk buffer
  1118. @item ? @tab 0x80000 - 1 @tab Protected mode stack
  1119. @item ? @tab 0xA0000 - 1 @tab Extended BIOS Data Area
  1120. @item 0xA0000 @tab 0xC0000 - 1 @tab Video RAM
  1121. @item 0xC0000 @tab 0x100000 - 1 @tab BIOS
  1122. @item 0x100000 @tab ? @tab Heap and module code
  1123. @end multitable
  1124. @node Video Subsystem
  1125. @chapter Video Subsystem
  1126. @c By VesaJääskeläinen
  1127. This document contains specification for Video Subsystem for GRUB2.
  1128. Currently only the usage interface is described in this document.
  1129. Internal structure of how video drivers are registering and how video
  1130. driver manager works are not included here.
  1131. @menu
  1132. * Video API::
  1133. * Example usage of Video API::
  1134. * Bitmap API::
  1135. @end menu
  1136. @node Video API
  1137. @section Video API
  1138. @subsection grub_video_setup
  1139. @itemize
  1140. @item Prototype:
  1141. @example
  1142. grub_err_t
  1143. grub_video_setup (unsigned int width, unsigned int height, unsigned int mode_type);
  1144. @end example
  1145. @item Description:
  1146. 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}.
  1147. 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.
  1148. @end itemize
  1149. @subsection grub_video_restore
  1150. @itemize
  1151. @item Prototype:
  1152. @example
  1153. grub_err_t
  1154. grub_video_restore (void);
  1155. @end example
  1156. @item Description:
  1157. Video subsystem will deinitialize activated video driver to restore old state of video device. This can be used to switch back to text mode.
  1158. @end itemize
  1159. @subsection grub_video_get_info
  1160. @itemize
  1161. @item Prototype:
  1162. @example
  1163. grub_err_t
  1164. grub_video_get_info (struct grub_video_mode_info *mode_info);
  1165. @end example
  1166. @example
  1167. struct grub_video_mode_info
  1168. @{
  1169. /* Width of the screen. */
  1170. unsigned int width;
  1171. /* Height of the screen. */
  1172. unsigned int height;
  1173. /* Mode type bitmask. Contains information like is it Index color or
  1174. RGB mode. */
  1175. unsigned int mode_type;
  1176. /* Bits per pixel. */
  1177. unsigned int bpp;
  1178. /* Bytes per pixel. */
  1179. unsigned int bytes_per_pixel;
  1180. /* Pitch of one scanline. How many bytes there are for scanline. */
  1181. unsigned int pitch;
  1182. /* In index color mode, number of colors. In RGB mode this is 256. */
  1183. unsigned int number_of_colors;
  1184. /* Optimization hint how binary data is coded. */
  1185. enum grub_video_blit_format blit_format;
  1186. /* How many bits are reserved for red color. */
  1187. unsigned int red_mask_size;
  1188. /* What is location of red color bits. In Index Color mode, this is 0. */
  1189. unsigned int red_field_pos;
  1190. /* How many bits are reserved for green color. */
  1191. unsigned int green_mask_size;
  1192. /* What is location of green color bits. In Index Color mode, this is 0. */
  1193. unsigned int green_field_pos;
  1194. /* How many bits are reserved for blue color. */
  1195. unsigned int blue_mask_size;
  1196. /* What is location of blue color bits. In Index Color mode, this is 0. */
  1197. unsigned int blue_field_pos;
  1198. /* How many bits are reserved in color. */
  1199. unsigned int reserved_mask_size;
  1200. /* What is location of reserved color bits. In Index Color mode,
  1201. this is 0. */
  1202. unsigned int reserved_field_pos;
  1203. @};
  1204. @end example
  1205. @item Description:
  1206. 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.
  1207. @end itemize
  1208. @subsection grub_video_get_blit_format
  1209. @itemize
  1210. @item Prototype:
  1211. @example
  1212. enum grub_video_blit_format
  1213. grub_video_get_blit_format (struct grub_video_mode_info *mode_info);
  1214. @end example
  1215. @example
  1216. enum grub_video_blit_format
  1217. @{
  1218. /* Follow exactly field & mask information. */
  1219. GRUB_VIDEO_BLIT_FORMAT_RGBA,
  1220. /* Make optimization assumption. */
  1221. GRUB_VIDEO_BLIT_FORMAT_R8G8B8A8,
  1222. /* Follow exactly field & mask information. */
  1223. GRUB_VIDEO_BLIT_FORMAT_RGB,
  1224. /* Make optimization assumption. */
  1225. GRUB_VIDEO_BLIT_FORMAT_R8G8B8,
  1226. /* When needed, decode color or just use value as is. */
  1227. GRUB_VIDEO_BLIT_FORMAT_INDEXCOLOR
  1228. @};
  1229. @end example
  1230. @item Description:
  1231. 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.
  1232. @end itemize
  1233. @subsection grub_video_set_palette
  1234. @itemize
  1235. @item Prototype:
  1236. @example
  1237. grub_err_t
  1238. grub_video_set_palette (unsigned int start, unsigned int count, struct grub_video_palette_data *palette_data);
  1239. @end example
  1240. @example
  1241. struct grub_video_palette_data
  1242. @{
  1243. grub_uint8_t r; /* Red color value (0-255). */
  1244. grub_uint8_t g; /* Green color value (0-255). */
  1245. grub_uint8_t b; /* Blue color value (0-255). */
  1246. grub_uint8_t a; /* Reserved bits value (0-255). */
  1247. @};
  1248. @end example
  1249. @item Description:
  1250. 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.
  1251. @end itemize
  1252. @subsection grub_video_get_palette
  1253. @itemize
  1254. @item Prototype:
  1255. @example
  1256. grub_err_t
  1257. grub_video_get_palette (unsigned int start, unsigned int count, struct grub_video_palette_data *palette_data);
  1258. @end example
  1259. @example
  1260. struct grub_video_palette_data
  1261. @{
  1262. grub_uint8_t r; /* Red color value (0-255). */
  1263. grub_uint8_t g; /* Green color value (0-255). */
  1264. grub_uint8_t b; /* Blue color value (0-255). */
  1265. grub_uint8_t a; /* Reserved bits value (0-255). */
  1266. @};
  1267. @end example
  1268. @item Description:
  1269. 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.
  1270. @end itemize
  1271. @subsection grub_video_set_area_status
  1272. @itemize
  1273. @item Prototype:
  1274. @example
  1275. grub_err_t
  1276. grub_video_set_area_status (grub_video_area_status_t area_status);
  1277. @end example
  1278. @example
  1279. enum grub_video_area_status_t
  1280. @{
  1281. GRUB_VIDEO_AREA_DISABLED,
  1282. GRUB_VIDEO_AREA_ENABLED
  1283. @};
  1284. @end example
  1285. @item Description:
  1286. Used to set area drawing mode for redrawing the specified region. Draw commands
  1287. are performed in the intersection of the viewport and the region called area.
  1288. Coordinates remain related to the viewport. If draw commands try to draw over
  1289. the area, they are clipped.
  1290. Set status to DISABLED if you need to draw everything.
  1291. Set status to ENABLED and region to the desired rectangle to redraw everything
  1292. inside the region leaving everything else intact.
  1293. Should be used for redrawing of active elements.
  1294. @end itemize
  1295. @subsection grub_video_get_area_status
  1296. @itemize
  1297. @item Prototype:
  1298. @example
  1299. grub_err_r
  1300. grub_video_get_area_status (grub_video_area_status_t *area_status);
  1301. @end example
  1302. @item Description:
  1303. Used to query the area status.
  1304. @end itemize
  1305. @subsection grub_video_set_viewport
  1306. @itemize
  1307. @item Prototype:
  1308. @example
  1309. grub_err_t
  1310. grub_video_set_viewport (unsigned int x, unsigned int y, unsigned int width, unsigned int height);
  1311. @end example
  1312. @item Description:
  1313. 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.
  1314. @end itemize
  1315. @subsection grub_video_get_viewport
  1316. @itemize
  1317. @item Prototype:
  1318. @example
  1319. grub_err_t
  1320. grub_video_get_viewport (unsigned int *x, unsigned int *y, unsigned int *width, unsigned int *height);
  1321. @end example
  1322. @item Description:
  1323. Used to query current viewport dimensions. Software developer can use this to choose best way to render contents of the viewport.
  1324. @end itemize
  1325. @subsection grub_video_set_region
  1326. @itemize
  1327. @item Prototype:
  1328. @example
  1329. grub_err_t
  1330. grub_video_set_region (unsigned int x, unsigned int y, unsigned int width, unsigned int height);
  1331. @end example
  1332. @item Description:
  1333. Used to specify the region of the screen which should be redrawn. Use absolute
  1334. values. When the region is set and area status is ENABLE all draw commands will
  1335. be performed inside the interseption of region and viewport named area.
  1336. If draw commands try to draw over viewport, they are clipped. If developer
  1337. requests larger than possible region, width and height will be clamped to fit
  1338. screen. Should be used for redrawing of active elements.
  1339. @end itemize
  1340. @subsection grub_video_get_region
  1341. @itemize
  1342. @item Prototype:
  1343. @example
  1344. grub_err_t
  1345. grub_video_get_region (unsigned int *x, unsigned int *y, unsigned int *width, unsigned int *height);
  1346. @end example
  1347. @item Description:
  1348. Used to query current region dimensions.
  1349. @end itemize
  1350. @subsection grub_video_map_color
  1351. @itemize
  1352. @item Prototype:
  1353. @example
  1354. grub_video_color_t
  1355. grub_video_map_color (grub_uint32_t color_name);
  1356. @end example
  1357. @item Description:
  1358. 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.
  1359. @end itemize
  1360. @subsection grub_video_map_rgb
  1361. @itemize
  1362. @item Prototype:
  1363. @example
  1364. grub_video_color_t
  1365. grub_video_map_rgb (grub_uint8_t red, grub_uint8_t green, grub_uint8_t blue);
  1366. @end example
  1367. @item Description:
  1368. 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.
  1369. @end itemize
  1370. @subsection grub_video_map_rgba
  1371. @itemize
  1372. @item Prototype:
  1373. @example
  1374. grub_video_color_t
  1375. grub_video_map_rgba (grub_uint8_t red, grub_uint8_t green, grub_uint8_t blue, grub_uint8_t alpha);
  1376. @end example
  1377. @item Description:
  1378. 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.
  1379. @end itemize
  1380. @subsection grub_video_unmap_color
  1381. @itemize
  1382. @item Prototype:
  1383. @example
  1384. grub_err_t
  1385. 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);
  1386. @end example
  1387. @item Description:
  1388. 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).
  1389. @end itemize
  1390. @subsection grub_video_fill_rect
  1391. @itemize
  1392. @item Prototype:
  1393. @example
  1394. grub_err_t
  1395. grub_video_fill_rect (grub_video_color_t color, int x, int y, unsigned int width, unsigned int height);
  1396. @end example
  1397. @item Description:
  1398. 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.
  1399. 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.
  1400. @end itemize
  1401. @subsection grub_video_blit_glyph
  1402. @itemize
  1403. @item Prototype:
  1404. @example
  1405. grub_err_t
  1406. grub_video_blit_glyph (struct grub_font_glyph *glyph, grub_video_color_t color, int x, int y);
  1407. @end example
  1408. @example
  1409. struct grub_font_glyph @{
  1410. /* TBD. */
  1411. @};
  1412. @end example
  1413. @item Description:
  1414. 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.
  1415. @end itemize
  1416. @subsection grub_video_blit_bitmap
  1417. @itemize
  1418. @item Prototype:
  1419. @example
  1420. grub_err_t
  1421. 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);
  1422. @end example
  1423. @example
  1424. struct grub_video_bitmap
  1425. @{
  1426. /* TBD. */
  1427. @};
  1428. enum grub_video_blit_operators
  1429. @{
  1430. GRUB_VIDEO_BLIT_REPLACE,
  1431. GRUB_VIDEO_BLIT_BLEND
  1432. @};
  1433. @end example
  1434. @item Description:
  1435. 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.
  1436. Software developer should use @code{grub_video_bitmap_create} or @code{grub_video_bitmap_load} to create or load bitmap data.
  1437. @end itemize
  1438. @subsection grub_video_blit_render_target
  1439. @itemize
  1440. @item Prototype:
  1441. @example
  1442. grub_err_t
  1443. 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);
  1444. @end example
  1445. @example
  1446. struct grub_video_render_target @{
  1447. /* This is private data for video driver. Should not be accessed from elsewhere directly. */
  1448. @};
  1449. enum grub_video_blit_operators
  1450. @{
  1451. GRUB_VIDEO_BLIT_REPLACE,
  1452. GRUB_VIDEO_BLIT_BLEND
  1453. @};
  1454. @end example
  1455. @item Description:
  1456. 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.
  1457. @end itemize
  1458. @subsection grub_video_scroll
  1459. @itemize
  1460. @item Prototype:
  1461. @example
  1462. grub_err_t
  1463. grub_video_scroll (grub_video_color_t color, int dx, int dy);
  1464. @end example
  1465. @item Description:
  1466. 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.
  1467. @end itemize
  1468. @subsection grub_video_swap_buffers
  1469. @itemize
  1470. @item Prototype:
  1471. @example
  1472. grub_err_t
  1473. grub_video_swap_buffers (void);
  1474. @end example
  1475. @item Description:
  1476. 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.
  1477. @end itemize
  1478. @subsection grub_video_create_render_target
  1479. @itemize
  1480. @item Prototype:
  1481. @example
  1482. grub_err_t
  1483. grub_video_create_render_target (struct grub_video_render_target **result, unsigned int width, unsigned int height, unsigned int mode_type);
  1484. @end example
  1485. @example
  1486. struct grub_video_render_target @{
  1487. /* This is private data for video driver. Should not be accessed from elsewhere directly. */
  1488. @};
  1489. @end example
  1490. @item Description:
  1491. 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.
  1492. @end itemize
  1493. @subsection grub_video_delete_render_target
  1494. @itemize
  1495. @item Prototype:
  1496. @example
  1497. grub_err_t
  1498. grub_video_delete_render_target (struct grub_video_render_target *target);
  1499. @end example
  1500. @item Description:
  1501. 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.
  1502. @end itemize
  1503. @subsection grub_video_set_active_render_target
  1504. @itemize
  1505. @item Prototype:
  1506. @example
  1507. grub_err_t
  1508. grub_video_set_active_render_target (struct grub_video_render_target *target);
  1509. @end example
  1510. @item Description:
  1511. 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.
  1512. @end itemize
  1513. @subsection grub_video_get_active_render_target
  1514. @itemize
  1515. @item Prototype:
  1516. @example
  1517. grub_err_t
  1518. grub_video_get_active_render_target (struct grub_video_render_target **target);
  1519. @end example
  1520. @item Description:
  1521. 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}.
  1522. @end itemize
  1523. @node Example usage of Video API
  1524. @section Example usage of Video API
  1525. @subsection Example of screen setup
  1526. @example
  1527. grub_err_t rc;
  1528. /* Try to initialize video mode 1024 x 768 with direct RGB. */
  1529. rc = grub_video_setup (1024, 768, GRUB_VIDEO_MODE_TYPE_RGB);
  1530. if (rc != GRUB_ERR_NONE)
  1531. @{
  1532. /* Fall back to standard VGA Index Color mode. */
  1533. rc = grub_video_setup (640, 480, GRUB_VIDEO_MODE_TYPE_INDEX);
  1534. if (rc != GRUB_ERR_NONE)
  1535. @{
  1536. /* Handle error. */
  1537. @}
  1538. @}
  1539. @end example
  1540. @subsection Example of setting up console viewport
  1541. @example
  1542. grub_uint32_t x, y, width, height;
  1543. grub_video_color_t color;
  1544. struct grub_font_glyph glyph;
  1545. grub_err_t rc;
  1546. /* Query existing viewport. */
  1547. grub_video_get_viewport (&x, &y, &width, &height);
  1548. /* Fill background. */
  1549. color = grub_video_map_color (GRUB_COLOR_BACKGROUND);
  1550. grub_video_fill_rect (color, 0, 0, width, height);
  1551. /* Setup console viewport. */
  1552. grub_video_set_viewport (x + 10, y + 10, width - 20, height - 20);
  1553. grub_video_get_viewport (&x, &y, &width, &height);
  1554. color = grub_video_map_color (GRUB_COLOR_CONSOLE_BACKGROUND);
  1555. grub_video_fill_rect (color, 0, 0, width, height);
  1556. /* Draw text to viewport. */
  1557. color = grub_video_map_color (GRUB_COLOR_CONSOLE_TEXT);
  1558. grub_font_get_glyph ('X', &glyph);
  1559. grub_video_blit_glyph (&glyph, color, 0, 0);
  1560. @end example
  1561. @node Bitmap API
  1562. @section Bitmap API
  1563. @subsection grub_video_bitmap_create
  1564. @itemize
  1565. @item Prototype:
  1566. @example
  1567. 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)
  1568. @end example
  1569. @item Description:
  1570. 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.
  1571. @end itemize
  1572. @subsection grub_video_bitmap_destroy
  1573. @itemize
  1574. @item Prototype:
  1575. @example
  1576. grub_err_t grub_video_bitmap_destroy (struct grub_video_bitmap *bitmap);
  1577. @end example
  1578. @item Description:
  1579. 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}.
  1580. @end itemize
  1581. @subsection grub_video_bitmap_load
  1582. @itemize
  1583. @item Prototype:
  1584. @example
  1585. grub_err_t grub_video_bitmap_load (struct grub_video_bitmap **bitmap, const char *filename);
  1586. @end example
  1587. @item Description:
  1588. 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.
  1589. @end itemize
  1590. @subsection grub_video_bitmap_get_width
  1591. @itemize
  1592. @item Prototype:
  1593. @example
  1594. unsigned int grub_video_bitmap_get_width (struct grub_video_bitmap *bitmap);
  1595. @end example
  1596. @item Description:
  1597. Returns bitmap width.
  1598. @end itemize
  1599. @subsection grub_video_bitmap_get_height
  1600. @itemize
  1601. @item Prototype:
  1602. @example
  1603. unsigned int grub_video_bitmap_get_height (struct grub_video_bitmap *bitmap);
  1604. @end example
  1605. @item Description:
  1606. Return bitmap height.
  1607. @end itemize
  1608. @subsection grub_video_bitmap_get_mode_info
  1609. @itemize
  1610. @item Prototype:
  1611. @example
  1612. void grub_video_bitmap_get_mode_info (struct grub_video_bitmap *bitmap, struct grub_video_mode_info *mode_info);
  1613. @end example
  1614. @item Description:
  1615. Returns bitmap format details in form of @code{grub_video_mode_info}.
  1616. @end itemize
  1617. @subsection grub_video_bitmap_get_data
  1618. @itemize
  1619. @item Prototype:
  1620. @example
  1621. void *grub_video_bitmap_get_data (struct grub_video_bitmap *bitmap);
  1622. @end example
  1623. @item Description:
  1624. 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.
  1625. @end itemize
  1626. @node PFF2 Font File Format
  1627. @chapter PFF2 Font File Format
  1628. @c Author: Colin D. Bennett <colin@gibibit.com>
  1629. @c Date: 8 January 2009
  1630. @menu
  1631. * Introduction::
  1632. * File Structure::
  1633. * Font Metrics::
  1634. @end menu
  1635. @node Introduction
  1636. @section Introduction
  1637. The goal of this format is to provide a bitmap font format that is simple to
  1638. use, compact, and cleanly supports Unicode.
  1639. @subsection Goals of the GRUB Font Format
  1640. @itemize
  1641. @item Simple to read and use.
  1642. Since GRUB will only be reading the font files,
  1643. we are more concerned with making the code to read the font simple than we
  1644. are with writing the font.
  1645. @item Compact storage.
  1646. The fonts will generally be stored in a small boot
  1647. partition where GRUB is located, and this may be on a removable storage
  1648. device such as a CD or USB flash drive where space is more limited than it
  1649. is on most hard drives.
  1650. @item Unicode.
  1651. GRUB should not have to deal with multiple character
  1652. encodings. The font should always use Unicode character codes for simple
  1653. internationalization.
  1654. @end itemize
  1655. @subsection Why Another Font Format?
  1656. There are many existing bitmap font formats that GRUB could use. However,
  1657. there are aspects of these formats that may make them less than suitable for
  1658. use in GRUB at this time:
  1659. @table @samp
  1660. @item BDF
  1661. Inefficient storage; uses ASCII to describe properties and
  1662. hexadecimal numbers in ASCII for the bitmap rows.
  1663. @item PCF
  1664. Many format variations such as byte order and bitmap padding (rows
  1665. padded to byte, word, etc.) would result in more complex code to
  1666. handle the font format.
  1667. @end table
  1668. @node File Structure
  1669. @section File Structure
  1670. A file @strong{section} consists of a 4-byte name, a 32-bit big-endian length (not
  1671. including the name or length), and then @var{length} more section-type-specific
  1672. bytes.
  1673. The standard file extension for PFF2 font files is @file{.pf2}.
  1674. @subsection Section Types
  1675. @table @samp
  1676. @item FILE
  1677. @strong{File type ID} (ASCII string). This must be the first section in the file. It has length 4
  1678. and the contents are the four bytes of the ASCII string @samp{PFF2}.
  1679. @item NAME
  1680. @strong{Font name} (ASCII string). This is the full font name including family,
  1681. weight, style, and point size. For instance, "Helvetica Bold Italic 14".
  1682. @item FAMI
  1683. @strong{Font family name} (ASCII string). For instance, "Helvetica". This should
  1684. be included so that intelligent font substitution can take place.
  1685. @item WEIG
  1686. @strong{Font weight} (ASCII string). Valid values are @samp{bold} and @samp{normal}.
  1687. This should be included so that intelligent font substitution can take
  1688. place.
  1689. @item SLAN
  1690. @strong{Font slant} (ASCII string). Valid values are @samp{italic} and @samp{normal}.
  1691. This should be included so that intelligent font substitution can take
  1692. place.
  1693. @item PTSZ
  1694. @strong{Font point size} (uint16be).
  1695. @item MAXW
  1696. @strong{Maximum character width in pixels} (uint16be).
  1697. @item MAXH
  1698. @strong{Maximum character height in pixels} (uint16be).
  1699. @item ASCE
  1700. @strong{Ascent in pixels} (uint16be). @xref{Font Metrics}, for details.
  1701. @item DESC
  1702. @strong{Descent in pixels} (uint16be). @xref{Font Metrics}, for details.
  1703. @item CHIX
  1704. @strong{Character index.}
  1705. The character index begins with a 32-bit big-endian unsigned integer
  1706. indicating the total size of the section, not including this size value.
  1707. For each character, there is an instance of the following entry structure:
  1708. @itemize
  1709. @item @strong{Unicode code point.} (32-bit big-endian integer.)
  1710. @item @strong{Storage flags.} (byte.)
  1711. @itemize
  1712. @item Bits 2..0:
  1713. If equal to 000 binary, then the character data is stored
  1714. uncompressed beginning at the offset indicated by the character's
  1715. @strong{offset} value.
  1716. If equal to 001 binary, then the character data is stored within a
  1717. compressed character definition block that begins at the offset
  1718. within the file indicated by the character's @strong{offset} value.
  1719. @end itemize
  1720. @item @strong{Offset.} (32-bit big-endian integer.)
  1721. A marker that indicates the remainder of the file is data accessed via
  1722. the character index (CHIX) section. When reading this font file, the rest
  1723. of the file can be ignored when scanning the sections. The length should
  1724. be set to -1 (0xFFFFFFFF).
  1725. Supported data structures:
  1726. Character definition
  1727. Each character definition consists of:
  1728. @itemize
  1729. @item @strong{Width.}
  1730. Width of the bitmap in pixels. The bitmap's extents
  1731. represent the glyph's bounding box. @code{uint16be}.
  1732. @item @strong{Height.}
  1733. Height of the bitmap in pixels. The bitmap's extents
  1734. represent the glyph's bounding box. @code{uint16be}.
  1735. @item @strong{X offset.}
  1736. The number of pixels to shift the bitmap by
  1737. horizontally before drawing the character. @code{int16be}.
  1738. @item @strong{Y offset.}
  1739. The number of pixels to shift the bitmap by
  1740. vertically before drawing the character. @code{int16be}.
  1741. @item @strong{Device width.}
  1742. The number of pixels to advance horizontally from
  1743. this character's origin to the origin of the next character.
  1744. @code{int16be}.
  1745. @item @strong{Bitmap data.}
  1746. This is encoded as a string of bits. It is
  1747. organized as a row-major, top-down, left-to-right bitmap. The most
  1748. significant bit of each byte is taken to be the leftmost or uppermost
  1749. bit in the byte. For the sake of compact storage, rows are not padded
  1750. to byte boundaries (i.e., a single byte may contain bits belonging to
  1751. multiple rows). The last byte of the bitmap @strong{is} padded with zero
  1752. bits in the bits positions to the right of the last used bit if the
  1753. bitmap data does not fill the last byte.
  1754. The length of the @strong{bitmap data} field is (@var{width} * @var{height} + 7) / 8
  1755. using integer arithmetic, which is equivalent to ceil(@var{width} *
  1756. @var{height} / 8) using real number arithmetic.
  1757. It remains to be determined whether bitmap fonts usually make all
  1758. glyph bitmaps the same height, or if smaller glyphs are stored with
  1759. bitmaps having a lesser height. In the latter case, the baseline
  1760. would have to be used to calculate the location the bitmap should be
  1761. anchored at on screen.
  1762. @end itemize
  1763. @end itemize
  1764. @end table
  1765. @node Font Metrics
  1766. @section Font Metrics
  1767. @itemize
  1768. @item Ascent.
  1769. The distance from the baseline to the top of most characters.
  1770. Note that in some cases characters may extend above the ascent.
  1771. @item Descent.
  1772. The distance from the baseline to the bottom of most characters. Note that
  1773. in some cases characters may extend below the descent.
  1774. @item Leading.
  1775. The amount of space, in pixels, to leave between the descent of one line of
  1776. text and the ascent of the next line. This metrics is not specified in the
  1777. current file format; instead, the font rendering engine calculates a
  1778. reasonable leading value based on the other font metrics.
  1779. @item Horizonal leading.
  1780. The amount of space, in pixels, to leave horizontally between the left and
  1781. right edges of two adjacent glyphs. The @strong{device width} field determines
  1782. the effective leading value that is used to render the font.
  1783. @end itemize
  1784. @ifnottex
  1785. @image{font_char_metrics,,,,.png}
  1786. @end ifnottex
  1787. An illustration of how the various font metrics apply to characters.
  1788. @node Graphical Menu Software Design
  1789. @chapter Graphical Menu Software Design
  1790. @c By Colin D. Bennett <colin@gibibit.com>
  1791. @c Date: 17 August 2008
  1792. @menu
  1793. * Introduction_2::
  1794. * Startup Sequence::
  1795. * GUI Components::
  1796. * Command Line Window::
  1797. @end menu
  1798. @node Introduction_2
  1799. @section Introduction
  1800. The @samp{gfxmenu} module provides a graphical menu interface for GRUB 2. It
  1801. functions as an alternative to the menu interface provided by the @samp{normal}
  1802. module, which uses the grub terminal interface to display a menu on a
  1803. character-oriented terminal.
  1804. The graphical menu uses the GRUB video API, which is currently for the VESA
  1805. BIOS extensions (VBE) 2.0+. This is supported on the i386-pc platform.
  1806. However, the graphical menu itself does not depend on using VBE, so if another
  1807. GRUB video driver were implemented, the @samp{gfxmenu} graphical menu would work
  1808. on the new video driver as well.
  1809. @node Startup Sequence
  1810. @section Startup Sequence
  1811. @itemize
  1812. @item grub_enter_normal_mode [normal/main.c]
  1813. @item grub_normal_execute [normal/main.c]
  1814. @item read_config_file [normal/main.c]
  1815. @item (When @file{gfxmenu.mod} is loaded with @command{insmod}, it will call @code{grub_menu_viewer_register()} to register itself.)
  1816. @item GRUB_MOD_INIT (gfxmenu) [gfxmenu/gfxmenu.c]
  1817. @item grub_menu_viewer_register [kern/menu_viewer.c]
  1818. @item grub_menu_viewer_show_menu [kern/menu_viewer.c]
  1819. @item get_current_menu_viewer() [kern/menu_viewer.c]
  1820. @item show_menu() [gfxmenu/gfxmenu.c]
  1821. @item grub_gfxmenu_model_new [gfxmenu/model.c]
  1822. @item grub_gfxmenu_view_new [gfxmenu/view.c]
  1823. @item set_graphics_mode [gfxmenu/view.c]
  1824. @item grub_gfxmenu_view_load_theme [gfxmenu/theme_loader.c]
  1825. @end itemize
  1826. @node GUI Components
  1827. @section GUI Components
  1828. The graphical menu implements a GUI component system that supports a
  1829. container-based layout system. Components can be added to containers, and
  1830. containers (which are a type of component) can then be added to other
  1831. containers, to form a tree of components. Currently, the root component of
  1832. this tree is a @samp{canvas} component, which allows manual layout of its child
  1833. components.
  1834. Components (non-container):
  1835. @itemize
  1836. @item label
  1837. @item image
  1838. @item progress_bar
  1839. @item circular_progress
  1840. @item list (currently hard coded to be a boot menu list)
  1841. @end itemize
  1842. Containers:
  1843. @itemize
  1844. @item canvas
  1845. @item hbox
  1846. @item vbox
  1847. @end itemize
  1848. The GUI component instances are created by the theme loader in
  1849. @file{gfxmenu/theme_loader.c} when a theme is loaded. Theme files specify
  1850. statements such as @samp{+vbox@{ +label @{ text="Hello" @} +label@{ text="World" @} @}}
  1851. to add components to the component tree root. By nesting the component
  1852. creation statements in the theme file, the instantiated components are nested
  1853. the same way.
  1854. When a component is added to a container, that new child is considered @strong{owned}
  1855. by the container. Great care should be taken if the caller retains a
  1856. reference to the child component, since it will be destroyed if its parent
  1857. container is destroyed. A better choice instead of storing a pointer to the
  1858. child component is to use the component ID to find the desired component.
  1859. Component IDs do not have to be unique (it is often useful to have multiple
  1860. components with an ID of "__timeout__", for instance).
  1861. In order to access and use components in the component tree, there are two
  1862. functions (defined in @file{gfxmenu/gui_util.c}) that are particularly useful:
  1863. @itemize
  1864. @item @code{grub_gui_find_by_id (root, id, callback, userdata)}:
  1865. This function recursively traverses the component tree rooted at @var{root}, and
  1866. for every component that has an ID equal to @var{id}, calls the function pointed
  1867. to by @var{callback} with the matching component and the void pointer @var{userdata}
  1868. as arguments. The callback function can do whatever is desired to use the
  1869. component passed in.
  1870. @item @code{grub_gui_iterate_recursively (root, callback, userdata)}:
  1871. This function calls the function pointed to by @var{callback} for every
  1872. component that is a descendant of @var{root} in the component tree. When the
  1873. callback function is called, the component and the void pointer @var{userdata}
  1874. as arguments. The callback function can do whatever is desired to use the
  1875. component passed in.
  1876. @end itemize
  1877. @node Command Line Window
  1878. @section Command Line Window
  1879. The terminal window used to provide command line access within the graphical
  1880. menu is managed by @file{gfxmenu/view.c}. The @samp{gfxterm} terminal is used, and
  1881. it has been modified to allow rendering to an offscreen render target to allow
  1882. it to be composed into the double buffering system that the graphical menu
  1883. view uses. This is bad for performance, however, so it would probably be a
  1884. good idea to make it possible to temporarily disable double buffering as long
  1885. as the terminal window is visible. There are still unresolved problems that
  1886. occur when commands are executed from the terminal window that change the
  1887. graphics mode. It's possible that making @code{grub_video_restore()} return to
  1888. the graphics mode that was in use before @code{grub_video_setup()} was called
  1889. might fix some of the problems.
  1890. @node Verifiers framework
  1891. @chapter Verifiers framework
  1892. To register your own verifier call @samp{grub_verifier_register} with a structure
  1893. pointing to your functions.
  1894. The interface is inspired by the hash interface with @samp{init}/@samp{write}/@samp{fini}.
  1895. There are essentially 2 ways of using it, hashing and whole-file verification.
  1896. With the hashing approach:
  1897. During @samp{init} you decide whether you want to check the given file and init context.
  1898. In @samp{write} you update your hashing state.
  1899. In @samp{fini} you check that the hash matches the expected value/passes some check/...
  1900. With whole-file verification:
  1901. During @samp{init} you decide whether you want to check the given file and init context.
  1902. In @samp{write} you verify the file and return an error if it fails.
  1903. You don't have @samp{fini}.
  1904. Additional @samp{verify_string} receives various strings like kernel parameters
  1905. to verify. Returning no error means successful verification and an error stops
  1906. the current action.
  1907. Detailed description of the API:
  1908. Every time a file is opened your @samp{init} function is called with file descriptor
  1909. and file type. Your function can have the following outcomes:
  1910. @itemize
  1911. @item returning no error and setting @samp{*flags} to @samp{GRUB_VERIFY_FLAGS_DEFER_AUTH}.
  1912. In this case verification is deferred to other active verifiers. Verification
  1913. fails if nobody cares or selected verifier fails.
  1914. @item returning no error and setting @samp{*flags} to @samp{GRUB_VERIFY_FLAGS_SKIP_VERIFICATION}.
  1915. In this case your verifier will not be called anymore and it is assumed to have
  1916. skipped verification.
  1917. @item returning no error and not setting @samp{*flags} to @samp{GRUB_VERIFY_FLAGS_SKIP_VERIFICATION}
  1918. In this case verification is done as described in the following section.
  1919. @item returning an error. Then opening of the file will fail due to failed verification.
  1920. @end itemize
  1921. In the third case your @samp{write} will be called with chunks of the file. If
  1922. you need the whole file in a single chunk then during @samp{init} set the bit
  1923. @samp{GRUB_VERIFY_FLAGS_SINGLE_CHUNK} in @samp{*flags}. During @samp{init} you
  1924. may set @samp{*context} if you need additional context. At every iteration you
  1925. may return an error and the file will be considered as having failed the
  1926. verification. If you return no error then verification continues.
  1927. Optionally at the end of the file @samp{fini}, if it exists, is called with just
  1928. the context. If you return no error during any of @samp{init}, @samp{write} and
  1929. @samp{fini} then the file is considered as having succeded verification.
  1930. @node Lockdown framework
  1931. @chapter Lockdown framework
  1932. The GRUB can be locked down, which is a restricted mode where some operations
  1933. are not allowed. For instance, some commands cannot be used when the GRUB is
  1934. locked down.
  1935. The function
  1936. @code{grub_lockdown()} is used to lockdown GRUB and the function
  1937. @code{grub_is_lockdown()} function can be used to check whether lockdown is
  1938. enabled or not. When enabled, the function returns @samp{GRUB_LOCKDOWN_ENABLED}
  1939. and @samp{GRUB_LOCKDOWN_DISABLED} when is not enabled.
  1940. The following functions can be used to register the commands that can only be
  1941. used when lockdown is disabled:
  1942. @itemize
  1943. @item @code{grub_cmd_lockdown()} registers command which should not run when the
  1944. GRUB is in lockdown mode.
  1945. @item @code{grub_cmd_lockdown()} registers extended command which should not run
  1946. when the GRUB is in lockdown mode.
  1947. @end itemize
  1948. @node Copying This Manual
  1949. @appendix Copying This Manual
  1950. @menu
  1951. * GNU Free Documentation License:: License for copying this manual.
  1952. @end menu
  1953. @include fdl.texi
  1954. @node Index
  1955. @unnumbered Index
  1956. @c Currently, we use only the Concept Index.
  1957. @printindex cp
  1958. @bye