grub-dev.texi 78 KB

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