├── .gitignore ├── LICENSE ├── Makefile.am ├── README.md ├── autogen.sh ├── configure.ac ├── m4 └── ax_normalize_path.m4 ├── man └── kernel-install.xml ├── shell-completion ├── bash │ └── kernel-install └── zsh │ └── _kernel-install ├── src ├── kernel-install │ ├── 50-depmod.install │ ├── 90-loaderentry.install │ └── kernel-install └── sd-boot │ ├── boot.c │ ├── console.c │ ├── console.h │ ├── disk.c │ ├── disk.h │ ├── graphics.c │ ├── graphics.h │ ├── linux.c │ ├── linux.h │ ├── measure.c │ ├── measure.h │ ├── pefile.c │ ├── pefile.h │ ├── splash.c │ ├── splash.h │ ├── stub.c │ ├── util.c │ └── util.h └── test ├── splash.bmp └── test-efi-create-disk.sh /.gitignore: -------------------------------------------------------------------------------- 1 | *.efi 2 | *.efi.stub 3 | *.8 4 | *.a 5 | *.cache 6 | *.gch 7 | *.la 8 | *.lo 9 | *.log 10 | *.o 11 | *.so 12 | *.plist 13 | *.stamp 14 | *.swp 15 | *.trs 16 | *~ 17 | .config.args 18 | .deps/ 19 | .dirstamp 20 | *.tar.xz 21 | Makefile 22 | Makefile.in 23 | aclocal.m4 24 | config.h 25 | config.h.in 26 | config.log 27 | config.status 28 | configure 29 | stamp-* 30 | build-aux -------------------------------------------------------------------------------- /LICENSE: -------------------------------------------------------------------------------- 1 | GNU LESSER GENERAL PUBLIC LICENSE 2 | Version 2.1, February 1999 3 | 4 | Copyright (C) 1991, 1999 Free Software Foundation, Inc. 5 | 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA 6 | Everyone is permitted to copy and distribute verbatim copies 7 | of this license document, but changing it is not allowed. 8 | 9 | [This is the first released version of the Lesser GPL. It also counts 10 | as the successor of the GNU Library Public License, version 2, hence 11 | the version number 2.1.] 12 | 13 | Preamble 14 | 15 | The licenses for most software are designed to take away your 16 | freedom to share and change it. By contrast, the GNU General Public 17 | Licenses are intended to guarantee your freedom to share and change 18 | free software--to make sure the software is free for all its users. 19 | 20 | This license, the Lesser General Public License, applies to some 21 | specially designated software packages--typically libraries--of the 22 | Free Software Foundation and other authors who decide to use it. You 23 | can use it too, but we suggest you first think carefully about whether 24 | this license or the ordinary General Public License is the better 25 | strategy to use in any particular case, based on the explanations below. 26 | 27 | When we speak of free software, we are referring to freedom of use, 28 | not price. Our General Public Licenses are designed to make sure that 29 | you have the freedom to distribute copies of free software (and charge 30 | for this service if you wish); that you receive source code or can get 31 | it if you want it; that you can change the software and use pieces of 32 | it in new free programs; and that you are informed that you can do 33 | these things. 34 | 35 | To protect your rights, we need to make restrictions that forbid 36 | distributors to deny you these rights or to ask you to surrender these 37 | rights. These restrictions translate to certain responsibilities for 38 | you if you distribute copies of the library or if you modify it. 39 | 40 | For example, if you distribute copies of the library, whether gratis 41 | or for a fee, you must give the recipients all the rights that we gave 42 | you. You must make sure that they, too, receive or can get the source 43 | code. If you link other code with the library, you must provide 44 | complete object files to the recipients, so that they can relink them 45 | with the library after making changes to the library and recompiling 46 | it. And you must show them these terms so they know their rights. 47 | 48 | We protect your rights with a two-step method: (1) we copyright the 49 | library, and (2) we offer you this license, which gives you legal 50 | permission to copy, distribute and/or modify the library. 51 | 52 | To protect each distributor, we want to make it very clear that 53 | there is no warranty for the free library. Also, if the library is 54 | modified by someone else and passed on, the recipients should know 55 | that what they have is not the original version, so that the original 56 | author's reputation will not be affected by problems that might be 57 | introduced by others. 58 | 59 | Finally, software patents pose a constant threat to the existence of 60 | any free program. We wish to make sure that a company cannot 61 | effectively restrict the users of a free program by obtaining a 62 | restrictive license from a patent holder. Therefore, we insist that 63 | any patent license obtained for a version of the library must be 64 | consistent with the full freedom of use specified in this license. 65 | 66 | Most GNU software, including some libraries, is covered by the 67 | ordinary GNU General Public License. This license, the GNU Lesser 68 | General Public License, applies to certain designated libraries, and 69 | is quite different from the ordinary General Public License. We use 70 | this license for certain libraries in order to permit linking those 71 | libraries into non-free programs. 72 | 73 | When a program is linked with a library, whether statically or using 74 | a shared library, the combination of the two is legally speaking a 75 | combined work, a derivative of the original library. The ordinary 76 | General Public License therefore permits such linking only if the 77 | entire combination fits its criteria of freedom. The Lesser General 78 | Public License permits more lax criteria for linking other code with 79 | the library. 80 | 81 | We call this license the "Lesser" General Public License because it 82 | does Less to protect the user's freedom than the ordinary General 83 | Public License. It also provides other free software developers Less 84 | of an advantage over competing non-free programs. These disadvantages 85 | are the reason we use the ordinary General Public License for many 86 | libraries. However, the Lesser license provides advantages in certain 87 | special circumstances. 88 | 89 | For example, on rare occasions, there may be a special need to 90 | encourage the widest possible use of a certain library, so that it becomes 91 | a de-facto standard. To achieve this, non-free programs must be 92 | allowed to use the library. A more frequent case is that a free 93 | library does the same job as widely used non-free libraries. In this 94 | case, there is little to gain by limiting the free library to free 95 | software only, so we use the Lesser General Public License. 96 | 97 | In other cases, permission to use a particular library in non-free 98 | programs enables a greater number of people to use a large body of 99 | free software. For example, permission to use the GNU C Library in 100 | non-free programs enables many more people to use the whole GNU 101 | operating system, as well as its variant, the GNU/Linux operating 102 | system. 103 | 104 | Although the Lesser General Public License is Less protective of the 105 | users' freedom, it does ensure that the user of a program that is 106 | linked with the Library has the freedom and the wherewithal to run 107 | that program using a modified version of the Library. 108 | 109 | The precise terms and conditions for copying, distribution and 110 | modification follow. Pay close attention to the difference between a 111 | "work based on the library" and a "work that uses the library". The 112 | former contains code derived from the library, whereas the latter must 113 | be combined with the library in order to run. 114 | 115 | GNU LESSER GENERAL PUBLIC LICENSE 116 | TERMS AND CONDITIONS FOR COPYING, DISTRIBUTION AND MODIFICATION 117 | 118 | 0. This License Agreement applies to any software library or other 119 | program which contains a notice placed by the copyright holder or 120 | other authorized party saying it may be distributed under the terms of 121 | this Lesser General Public License (also called "this License"). 122 | Each licensee is addressed as "you". 123 | 124 | A "library" means a collection of software functions and/or data 125 | prepared so as to be conveniently linked with application programs 126 | (which use some of those functions and data) to form executables. 127 | 128 | The "Library", below, refers to any such software library or work 129 | which has been distributed under these terms. A "work based on the 130 | Library" means either the Library or any derivative work under 131 | copyright law: that is to say, a work containing the Library or a 132 | portion of it, either verbatim or with modifications and/or translated 133 | straightforwardly into another language. (Hereinafter, translation is 134 | included without limitation in the term "modification".) 135 | 136 | "Source code" for a work means the preferred form of the work for 137 | making modifications to it. For a library, complete source code means 138 | all the source code for all modules it contains, plus any associated 139 | interface definition files, plus the scripts used to control compilation 140 | and installation of the library. 141 | 142 | Activities other than copying, distribution and modification are not 143 | covered by this License; they are outside its scope. The act of 144 | running a program using the Library is not restricted, and output from 145 | such a program is covered only if its contents constitute a work based 146 | on the Library (independent of the use of the Library in a tool for 147 | writing it). Whether that is true depends on what the Library does 148 | and what the program that uses the Library does. 149 | 150 | 1. You may copy and distribute verbatim copies of the Library's 151 | complete source code as you receive it, in any medium, provided that 152 | you conspicuously and appropriately publish on each copy an 153 | appropriate copyright notice and disclaimer of warranty; keep intact 154 | all the notices that refer to this License and to the absence of any 155 | warranty; and distribute a copy of this License along with the 156 | Library. 157 | 158 | You may charge a fee for the physical act of transferring a copy, 159 | and you may at your option offer warranty protection in exchange for a 160 | fee. 161 | 162 | 2. You may modify your copy or copies of the Library or any portion 163 | of it, thus forming a work based on the Library, and copy and 164 | distribute such modifications or work under the terms of Section 1 165 | above, provided that you also meet all of these conditions: 166 | 167 | a) The modified work must itself be a software library. 168 | 169 | b) You must cause the files modified to carry prominent notices 170 | stating that you changed the files and the date of any change. 171 | 172 | c) You must cause the whole of the work to be licensed at no 173 | charge to all third parties under the terms of this License. 174 | 175 | d) If a facility in the modified Library refers to a function or a 176 | table of data to be supplied by an application program that uses 177 | the facility, other than as an argument passed when the facility 178 | is invoked, then you must make a good faith effort to ensure that, 179 | in the event an application does not supply such function or 180 | table, the facility still operates, and performs whatever part of 181 | its purpose remains meaningful. 182 | 183 | (For example, a function in a library to compute square roots has 184 | a purpose that is entirely well-defined independent of the 185 | application. Therefore, Subsection 2d requires that any 186 | application-supplied function or table used by this function must 187 | be optional: if the application does not supply it, the square 188 | root function must still compute square roots.) 189 | 190 | These requirements apply to the modified work as a whole. If 191 | identifiable sections of that work are not derived from the Library, 192 | and can be reasonably considered independent and separate works in 193 | themselves, then this License, and its terms, do not apply to those 194 | sections when you distribute them as separate works. But when you 195 | distribute the same sections as part of a whole which is a work based 196 | on the Library, the distribution of the whole must be on the terms of 197 | this License, whose permissions for other licensees extend to the 198 | entire whole, and thus to each and every part regardless of who wrote 199 | it. 200 | 201 | Thus, it is not the intent of this section to claim rights or contest 202 | your rights to work written entirely by you; rather, the intent is to 203 | exercise the right to control the distribution of derivative or 204 | collective works based on the Library. 205 | 206 | In addition, mere aggregation of another work not based on the Library 207 | with the Library (or with a work based on the Library) on a volume of 208 | a storage or distribution medium does not bring the other work under 209 | the scope of this License. 210 | 211 | 3. You may opt to apply the terms of the ordinary GNU General Public 212 | License instead of this License to a given copy of the Library. To do 213 | this, you must alter all the notices that refer to this License, so 214 | that they refer to the ordinary GNU General Public License, version 2, 215 | instead of to this License. (If a newer version than version 2 of the 216 | ordinary GNU General Public License has appeared, then you can specify 217 | that version instead if you wish.) Do not make any other change in 218 | these notices. 219 | 220 | Once this change is made in a given copy, it is irreversible for 221 | that copy, so the ordinary GNU General Public License applies to all 222 | subsequent copies and derivative works made from that copy. 223 | 224 | This option is useful when you wish to copy part of the code of 225 | the Library into a program that is not a library. 226 | 227 | 4. You may copy and distribute the Library (or a portion or 228 | derivative of it, under Section 2) in object code or executable form 229 | under the terms of Sections 1 and 2 above provided that you accompany 230 | it with the complete corresponding machine-readable source code, which 231 | must be distributed under the terms of Sections 1 and 2 above on a 232 | medium customarily used for software interchange. 233 | 234 | If distribution of object code is made by offering access to copy 235 | from a designated place, then offering equivalent access to copy the 236 | source code from the same place satisfies the requirement to 237 | distribute the source code, even though third parties are not 238 | compelled to copy the source along with the object code. 239 | 240 | 5. A program that contains no derivative of any portion of the 241 | Library, but is designed to work with the Library by being compiled or 242 | linked with it, is called a "work that uses the Library". Such a 243 | work, in isolation, is not a derivative work of the Library, and 244 | therefore falls outside the scope of this License. 245 | 246 | However, linking a "work that uses the Library" with the Library 247 | creates an executable that is a derivative of the Library (because it 248 | contains portions of the Library), rather than a "work that uses the 249 | library". The executable is therefore covered by this License. 250 | Section 6 states terms for distribution of such executables. 251 | 252 | When a "work that uses the Library" uses material from a header file 253 | that is part of the Library, the object code for the work may be a 254 | derivative work of the Library even though the source code is not. 255 | Whether this is true is especially significant if the work can be 256 | linked without the Library, or if the work is itself a library. The 257 | threshold for this to be true is not precisely defined by law. 258 | 259 | If such an object file uses only numerical parameters, data 260 | structure layouts and accessors, and small macros and small inline 261 | functions (ten lines or less in length), then the use of the object 262 | file is unrestricted, regardless of whether it is legally a derivative 263 | work. (Executables containing this object code plus portions of the 264 | Library will still fall under Section 6.) 265 | 266 | Otherwise, if the work is a derivative of the Library, you may 267 | distribute the object code for the work under the terms of Section 6. 268 | Any executables containing that work also fall under Section 6, 269 | whether or not they are linked directly with the Library itself. 270 | 271 | 6. As an exception to the Sections above, you may also combine or 272 | link a "work that uses the Library" with the Library to produce a 273 | work containing portions of the Library, and distribute that work 274 | under terms of your choice, provided that the terms permit 275 | modification of the work for the customer's own use and reverse 276 | engineering for debugging such modifications. 277 | 278 | You must give prominent notice with each copy of the work that the 279 | Library is used in it and that the Library and its use are covered by 280 | this License. You must supply a copy of this License. If the work 281 | during execution displays copyright notices, you must include the 282 | copyright notice for the Library among them, as well as a reference 283 | directing the user to the copy of this License. Also, you must do one 284 | of these things: 285 | 286 | a) Accompany the work with the complete corresponding 287 | machine-readable source code for the Library including whatever 288 | changes were used in the work (which must be distributed under 289 | Sections 1 and 2 above); and, if the work is an executable linked 290 | with the Library, with the complete machine-readable "work that 291 | uses the Library", as object code and/or source code, so that the 292 | user can modify the Library and then relink to produce a modified 293 | executable containing the modified Library. (It is understood 294 | that the user who changes the contents of definitions files in the 295 | Library will not necessarily be able to recompile the application 296 | to use the modified definitions.) 297 | 298 | b) Use a suitable shared library mechanism for linking with the 299 | Library. A suitable mechanism is one that (1) uses at run time a 300 | copy of the library already present on the user's computer system, 301 | rather than copying library functions into the executable, and (2) 302 | will operate properly with a modified version of the library, if 303 | the user installs one, as long as the modified version is 304 | interface-compatible with the version that the work was made with. 305 | 306 | c) Accompany the work with a written offer, valid for at 307 | least three years, to give the same user the materials 308 | specified in Subsection 6a, above, for a charge no more 309 | than the cost of performing this distribution. 310 | 311 | d) If distribution of the work is made by offering access to copy 312 | from a designated place, offer equivalent access to copy the above 313 | specified materials from the same place. 314 | 315 | e) Verify that the user has already received a copy of these 316 | materials or that you have already sent this user a copy. 317 | 318 | For an executable, the required form of the "work that uses the 319 | Library" must include any data and utility programs needed for 320 | reproducing the executable from it. However, as a special exception, 321 | the materials to be distributed need not include anything that is 322 | normally distributed (in either source or binary form) with the major 323 | components (compiler, kernel, and so on) of the operating system on 324 | which the executable runs, unless that component itself accompanies 325 | the executable. 326 | 327 | It may happen that this requirement contradicts the license 328 | restrictions of other proprietary libraries that do not normally 329 | accompany the operating system. Such a contradiction means you cannot 330 | use both them and the Library together in an executable that you 331 | distribute. 332 | 333 | 7. You may place library facilities that are a work based on the 334 | Library side-by-side in a single library together with other library 335 | facilities not covered by this License, and distribute such a combined 336 | library, provided that the separate distribution of the work based on 337 | the Library and of the other library facilities is otherwise 338 | permitted, and provided that you do these two things: 339 | 340 | a) Accompany the combined library with a copy of the same work 341 | based on the Library, uncombined with any other library 342 | facilities. This must be distributed under the terms of the 343 | Sections above. 344 | 345 | b) Give prominent notice with the combined library of the fact 346 | that part of it is a work based on the Library, and explaining 347 | where to find the accompanying uncombined form of the same work. 348 | 349 | 8. You may not copy, modify, sublicense, link with, or distribute 350 | the Library except as expressly provided under this License. Any 351 | attempt otherwise to copy, modify, sublicense, link with, or 352 | distribute the Library is void, and will automatically terminate your 353 | rights under this License. However, parties who have received copies, 354 | or rights, from you under this License will not have their licenses 355 | terminated so long as such parties remain in full compliance. 356 | 357 | 9. You are not required to accept this License, since you have not 358 | signed it. However, nothing else grants you permission to modify or 359 | distribute the Library or its derivative works. These actions are 360 | prohibited by law if you do not accept this License. Therefore, by 361 | modifying or distributing the Library (or any work based on the 362 | Library), you indicate your acceptance of this License to do so, and 363 | all its terms and conditions for copying, distributing or modifying 364 | the Library or works based on it. 365 | 366 | 10. Each time you redistribute the Library (or any work based on the 367 | Library), the recipient automatically receives a license from the 368 | original licensor to copy, distribute, link with or modify the Library 369 | subject to these terms and conditions. You may not impose any further 370 | restrictions on the recipients' exercise of the rights granted herein. 371 | You are not responsible for enforcing compliance by third parties with 372 | this License. 373 | 374 | 11. If, as a consequence of a court judgment or allegation of patent 375 | infringement or for any other reason (not limited to patent issues), 376 | conditions are imposed on you (whether by court order, agreement or 377 | otherwise) that contradict the conditions of this License, they do not 378 | excuse you from the conditions of this License. If you cannot 379 | distribute so as to satisfy simultaneously your obligations under this 380 | License and any other pertinent obligations, then as a consequence you 381 | may not distribute the Library at all. For example, if a patent 382 | license would not permit royalty-free redistribution of the Library by 383 | all those who receive copies directly or indirectly through you, then 384 | the only way you could satisfy both it and this License would be to 385 | refrain entirely from distribution of the Library. 386 | 387 | If any portion of this section is held invalid or unenforceable under any 388 | particular circumstance, the balance of the section is intended to apply, 389 | and the section as a whole is intended to apply in other circumstances. 390 | 391 | It is not the purpose of this section to induce you to infringe any 392 | patents or other property right claims or to contest validity of any 393 | such claims; this section has the sole purpose of protecting the 394 | integrity of the free software distribution system which is 395 | implemented by public license practices. Many people have made 396 | generous contributions to the wide range of software distributed 397 | through that system in reliance on consistent application of that 398 | system; it is up to the author/donor to decide if he or she is willing 399 | to distribute software through any other system and a licensee cannot 400 | impose that choice. 401 | 402 | This section is intended to make thoroughly clear what is believed to 403 | be a consequence of the rest of this License. 404 | 405 | 12. If the distribution and/or use of the Library is restricted in 406 | certain countries either by patents or by copyrighted interfaces, the 407 | original copyright holder who places the Library under this License may add 408 | an explicit geographical distribution limitation excluding those countries, 409 | so that distribution is permitted only in or among countries not thus 410 | excluded. In such case, this License incorporates the limitation as if 411 | written in the body of this License. 412 | 413 | 13. The Free Software Foundation may publish revised and/or new 414 | versions of the Lesser General Public License from time to time. 415 | Such new versions will be similar in spirit to the present version, 416 | but may differ in detail to address new problems or concerns. 417 | 418 | Each version is given a distinguishing version number. If the Library 419 | specifies a version number of this License which applies to it and 420 | "any later version", you have the option of following the terms and 421 | conditions either of that version or of any later version published by 422 | the Free Software Foundation. If the Library does not specify a 423 | license version number, you may choose any version ever published by 424 | the Free Software Foundation. 425 | 426 | 14. If you wish to incorporate parts of the Library into other free 427 | programs whose distribution conditions are incompatible with these, 428 | write to the author to ask for permission. For software which is 429 | copyrighted by the Free Software Foundation, write to the Free 430 | Software Foundation; we sometimes make exceptions for this. Our 431 | decision will be guided by the two goals of preserving the free status 432 | of all derivatives of our free software and of promoting the sharing 433 | and reuse of software generally. 434 | 435 | NO WARRANTY 436 | 437 | 15. BECAUSE THE LIBRARY IS LICENSED FREE OF CHARGE, THERE IS NO 438 | WARRANTY FOR THE LIBRARY, TO THE EXTENT PERMITTED BY APPLICABLE LAW. 439 | EXCEPT WHEN OTHERWISE STATED IN WRITING THE COPYRIGHT HOLDERS AND/OR 440 | OTHER PARTIES PROVIDE THE LIBRARY "AS IS" WITHOUT WARRANTY OF ANY 441 | KIND, EITHER EXPRESSED OR IMPLIED, INCLUDING, BUT NOT LIMITED TO, THE 442 | IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR 443 | PURPOSE. THE ENTIRE RISK AS TO THE QUALITY AND PERFORMANCE OF THE 444 | LIBRARY IS WITH YOU. SHOULD THE LIBRARY PROVE DEFECTIVE, YOU ASSUME 445 | THE COST OF ALL NECESSARY SERVICING, REPAIR OR CORRECTION. 446 | 447 | 16. IN NO EVENT UNLESS REQUIRED BY APPLICABLE LAW OR AGREED TO IN 448 | WRITING WILL ANY COPYRIGHT HOLDER, OR ANY OTHER PARTY WHO MAY MODIFY 449 | AND/OR REDISTRIBUTE THE LIBRARY AS PERMITTED ABOVE, BE LIABLE TO YOU 450 | FOR DAMAGES, INCLUDING ANY GENERAL, SPECIAL, INCIDENTAL OR 451 | CONSEQUENTIAL DAMAGES ARISING OUT OF THE USE OR INABILITY TO USE THE 452 | LIBRARY (INCLUDING BUT NOT LIMITED TO LOSS OF DATA OR DATA BEING 453 | RENDERED INACCURATE OR LOSSES SUSTAINED BY YOU OR THIRD PARTIES OR A 454 | FAILURE OF THE LIBRARY TO OPERATE WITH ANY OTHER SOFTWARE), EVEN IF 455 | SUCH HOLDER OR OTHER PARTY HAS BEEN ADVISED OF THE POSSIBILITY OF SUCH 456 | DAMAGES. 457 | 458 | END OF TERMS AND CONDITIONS 459 | 460 | How to Apply These Terms to Your New Libraries 461 | 462 | If you develop a new library, and you want it to be of the greatest 463 | possible use to the public, we recommend making it free software that 464 | everyone can redistribute and change. You can do so by permitting 465 | redistribution under these terms (or, alternatively, under the terms of the 466 | ordinary General Public License). 467 | 468 | To apply these terms, attach the following notices to the library. It is 469 | safest to attach them to the start of each source file to most effectively 470 | convey the exclusion of warranty; and each file should have at least the 471 | "copyright" line and a pointer to where the full notice is found. 472 | 473 | 474 | Copyright (C) 475 | 476 | This library is free software; you can redistribute it and/or 477 | modify it under the terms of the GNU Lesser General Public 478 | License as published by the Free Software Foundation; either 479 | version 2.1 of the License, or (at your option) any later version. 480 | 481 | This library is distributed in the hope that it will be useful, 482 | but WITHOUT ANY WARRANTY; without even the implied warranty of 483 | MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU 484 | Lesser General Public License for more details. 485 | 486 | You should have received a copy of the GNU Lesser General Public 487 | License along with this library; if not, write to the Free Software 488 | Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA 489 | 490 | Also add information on how to contact you by electronic and paper mail. 491 | 492 | You should also get your employer (if you work as a programmer) or your 493 | school, if any, to sign a "copyright disclaimer" for the library, if 494 | necessary. Here is a sample; alter the names: 495 | 496 | Yoyodyne, Inc., hereby disclaims all copyright interest in the 497 | library `Frob' (a library for tweaking knobs) written by James Random Hacker. 498 | 499 | , 1 April 1990 500 | Ty Coon, President of Vice 501 | 502 | That's all there is to it! 503 | -------------------------------------------------------------------------------- /Makefile.am: -------------------------------------------------------------------------------- 1 | # This file is part of systemd-boot 2 | # 3 | # Copyright (C) 2013-2016 Karel Zak 4 | # Copyright (C) 2016 Michal Sekletar 5 | # 6 | # systemd-boot is free software; you can redistribute it and/or modify it 7 | # under the terms of the GNU Lesser General Public License as published by 8 | # the Free Software Foundation; either version 2.1 of the License, or 9 | # (at your option) any later version. 10 | # 11 | # systemd-boot is distributed in the hope that it will be useful, but 12 | # WITHOUT ANY WARRANTY; without even the implied warranty of 13 | # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU 14 | # Lesser General Public License for more details. 15 | # 16 | # You should have received a copy of the GNU Lesser General Public License 17 | # along with systemd-boot; If not, see . 18 | 19 | systemd_bootlibdir = $(prefix)/lib/systemd-boot 20 | kernelinstalldir = $(prefix)/lib/kernel/install.d 21 | bashcompletiondir=@bashcompletiondir@ 22 | zshcompletiondir=@zshcompletiondir@ 23 | 24 | dist_bin_SCRIPTS = \ 25 | src/kernel-install/kernel-install 26 | 27 | dist_kernelinstall_SCRIPTS = \ 28 | src/kernel-install/50-depmod.install \ 29 | src/kernel-install/90-loaderentry.install 30 | 31 | dist_bashcompletion_data = \ 32 | shell-completion/bash/kernel-install 33 | 34 | dist_zshcompletion_data = \ 35 | shell-completion/zsh/_kernel-install 36 | 37 | if ENABLE_BASH_COMPLETION 38 | dist_bashcompletion_DATA = $(dist_bashcompletion_data) 39 | endif 40 | if ENABLE_ZSH_COMPLETION 41 | dist_zshcompletion_DATA = $(dist_zshcompletion_data) 42 | endif 43 | 44 | EXTRA_DIST = autogen.sh README.md LICENSE 45 | CLEANFILES = 46 | 47 | DISTCHECK_CONFIGURE_FLAGS = \ 48 | --with-bashcompletiondir=$$dc_install_base/$(bashcompletiondir) \ 49 | --with-zshcompletiondir=$$dc_install_base/$(zshcompletiondir) 50 | 51 | if ENABLE_MANPAGES 52 | %.8: %.xml 53 | $(AM_V_GEN)$(XSLTPROC) -o $@ --nonet \ 54 | --stringparam man.output.quietly 1 \ 55 | --stringparam man.th.extra1.suppress 1 \ 56 | --stringparam man.authors.section.enabled 0 \ 57 | --stringparam man.copyright.section.enabled 0 \ 58 | http://docbook.sourceforge.net/release/xsl/current/manpages/docbook.xsl $< 59 | 60 | dist_man_MANS = man/kernel-install.8 61 | endif 62 | 63 | EXTRA_DIST += man/kernel-install.xml 64 | CLEANFILES += man/kernel-install.8 65 | 66 | # ------------------------------------------------------------------------------ 67 | # EFI compilation -- this part of the build system uses custom make rules and 68 | # bypasses regular automake to provide absolute control on compiler and linker 69 | # flags. 70 | EFI_MACHINE_TYPE_NAME = @MACHINE_TYPE_NAME@ 71 | 72 | efi_cppflags = \ 73 | $(EFI_CPPFLAGS) \ 74 | -I$(top_builddir) -include config.h \ 75 | -I$(EFI_INC_DIR)/efi \ 76 | -I$(EFI_INC_DIR)/efi/$(ARCH) \ 77 | -DEFI_MACHINE_TYPE_NAME=\"$(EFI_MACHINE_TYPE_NAME)\" 78 | 79 | efi_cflags = \ 80 | $(EFI_CFLAGS) \ 81 | -Wall \ 82 | -Wextra \ 83 | -std=gnu90 \ 84 | -nostdinc \ 85 | -ggdb -O0 \ 86 | -fpic \ 87 | -fshort-wchar \ 88 | -nostdinc \ 89 | -ffreestanding \ 90 | -fno-strict-aliasing \ 91 | -fno-stack-protector \ 92 | -Wsign-compare \ 93 | -mno-sse \ 94 | -mno-mmx 95 | 96 | if ARCH_X86_64 97 | efi_cflags += \ 98 | -mno-red-zone \ 99 | -DEFI_FUNCTION_WRAPPER \ 100 | -DGNU_EFI_USE_MS_ABI 101 | endif 102 | 103 | efi_ldflags = \ 104 | $(EFI_LDFLAGS) \ 105 | -T $(EFI_LDS_DIR)/elf_$(ARCH)_efi.lds \ 106 | -shared \ 107 | -Bsymbolic \ 108 | -nostdlib \ 109 | -znocombreloc \ 110 | -L $(EFI_LIB_DIR) \ 111 | $(EFI_LDS_DIR)/crt0-efi-$(ARCH).o 112 | 113 | # ------------------------------------------------------------------------------ 114 | systemd_boot_headers = \ 115 | src/sd-boot/util.h \ 116 | src/sd-boot/console.h \ 117 | src/sd-boot/graphics.h \ 118 | src/sd-boot/pefile.h \ 119 | src/sd-boot/measure.h \ 120 | src/sd-boot/disk.h 121 | 122 | systemd_boot_sources = \ 123 | src/sd-boot/util.c \ 124 | src/sd-boot/console.c \ 125 | src/sd-boot/graphics.c \ 126 | src/sd-boot/pefile.c \ 127 | src/sd-boot/disk.c \ 128 | src/sd-boot/measure.c \ 129 | src/sd-boot/boot.c 130 | 131 | systemd_boot_objects = $(addprefix $(top_builddir)/,$(systemd_boot_sources:.c=.o)) 132 | systemd_boot_solib = $(top_builddir)/src/sd-boot/systemd_boot.so 133 | systemd_boot = systemd-boot$(EFI_MACHINE_TYPE_NAME).efi 134 | 135 | systemd_bootlib_DATA = $(systemd_boot) 136 | CLEANFILES += \ 137 | $(systemd_boot_objects) \ 138 | $(systemd_boot_solib) \ 139 | $(systemd_boot) 140 | 141 | EXTRA_DIST += \ 142 | $(systemd_boot_sources) \ 143 | $(systemd_boot_headers) 144 | 145 | $(top_builddir)/src/sd-boot/%.o: $(top_srcdir)/src/sd-boot/%.c $(addprefix $(top_srcdir)/,$(systemd_boot_headers)) 146 | @$(MKDIR_P) $(top_builddir)/src/sd-boot 147 | $(AM_V_CC)$(CC) $(efi_cppflags) $(efi_cflags) -c $< -o $@ 148 | 149 | $(systemd_boot_solib): $(systemd_boot_objects) 150 | $(AM_V_CCLD)$(LD) $(efi_ldflags) $(systemd_boot_objects) \ 151 | -o $@ -lefi -lgnuefi $(shell $(CC) -print-libgcc-file-name); \ 152 | nm -D -u $@ | grep ' U ' && exit 1 || : 153 | .DELETE_ON_ERROR: $(sustemd_boot_solib) 154 | 155 | $(systemd_boot): $(systemd_boot_solib) 156 | $(AM_V_GEN) objcopy -j .text -j .sdata -j .data -j .dynamic \ 157 | -j .dynsym -j .rel -j .rela -j .reloc \ 158 | --target=efi-app-$(ARCH) $< $@ 159 | 160 | # ------------------------------------------------------------------------------ 161 | stub_headers = \ 162 | src/sd-boot/util.h \ 163 | src/sd-boot/pefile.h \ 164 | src/sd-boot/disk.h \ 165 | src/sd-boot/graphics.h \ 166 | src/sd-boot/splash.h \ 167 | src/sd-boot/measure.h \ 168 | src/sd-boot/linux.h 169 | 170 | stub_sources = \ 171 | src/sd-boot/util.c \ 172 | src/sd-boot/pefile.c \ 173 | src/sd-boot/disk.c \ 174 | src/sd-boot/graphics.c \ 175 | src/sd-boot/splash.c \ 176 | src/sd-boot/linux.c \ 177 | src/sd-boot/measure.c \ 178 | src/sd-boot/stub.c 179 | 180 | stub_objects = $(addprefix $(top_builddir)/,$(stub_sources:.c=.o)) 181 | stub_solib = $(top_builddir)/src/sd-boot/stub.so 182 | stub = linux$(EFI_MACHINE_TYPE_NAME).efi.stub 183 | 184 | systemd_bootlib_DATA += $(stub) 185 | CLEANFILES += \ 186 | $(stub_objects) \ 187 | $(stub_solib) \ 188 | $(stub) 189 | 190 | EXTRA_DIST += \ 191 | $(stub_sources) \ 192 | $(stub_headers) \ 193 | test/splash.bmp 194 | 195 | $(top_builddir)/src/sd-boot/%.o: $(top_srcdir)/src/sd-boot/%.c $(addprefix $(top_srcdir)/,$(stub_headers)) 196 | @$(MKDIR_P) $(top_builddir)/src/sd-boot 197 | $(AM_V_CC)$(CC) $(efi_cppflags) $(efi_cflags) -c $< -o $@ 198 | 199 | $(stub_solib): $(stub_objects) 200 | $(AM_V_CCLD)$(LD) $(efi_ldflags) $(stub_objects) \ 201 | -o $@ -lefi -lgnuefi $(shell $(CC) -print-libgcc-file-name); \ 202 | nm -D -u $@ | grep ' U ' && exit 1 || : 203 | .DELETE_ON_ERROR: $(systemd_boot_solib) 204 | 205 | $(stub): $(stub_solib) 206 | $(AM_V_GEN) objcopy -j .text -j .sdata -j .data -j .dynamic \ 207 | -j .dynsym -j .rel -j .rela -j .reloc \ 208 | --target=efi-app-$(ARCH) $< $@ 209 | 210 | # ------------------------------------------------------------------------------ 211 | CLEANFILES += test-disk.img 212 | EXTRA_DIST += test/test-efi-create-disk.sh 213 | 214 | test-efi-disk.img: systemd_boot$(EFI_MACHINE_TYPE_NAME).efi test/test-efi-create-disk.sh 215 | $(AM_V_GEN)test/test-efi-create-disk.sh 216 | 217 | qemu: test-efi-disk.img 218 | $(QEMU) -machine accel=kvm -m 1024 -bios $(QEMU_BIOS) -snapshot test-efi-disk.img 219 | 220 | install-tree: all 221 | rm -rf $(abs_srcdir)/install-tree 222 | $(MAKE) install DESTDIR=$(abs_srcdir)/install-tree 223 | tree $(abs_srcdir)/install-tree 224 | -------------------------------------------------------------------------------- /README.md: -------------------------------------------------------------------------------- 1 | # systemd-boot 2 | 3 | systemd-boot is a simple UEFI Boot Manager. Its main job is to lunch 4 | selected boot menu entry. systemd-boot leverages APIs provided by UEFI firmware 5 | and offloads all the heavy lifting to a firmware (e.g. loading files from disk, 6 | executing binaries). This allows for very minimal implementation, but feature 7 | complete to support common usecases (desktop, laptop). 8 | 9 | systemd-boot fully supports The Freedesktop Boot Loader Specification [[1]](https://www.freedesktop.org/wiki/Specifications/BootLoaderSpec/). 10 | 11 | ## Installation 12 | 13 | ``` 14 | ./autogen.sh 15 | ./configure --prefix=/usr 16 | make 17 | sudo make install 18 | ``` 19 | 20 | Note that installation of sytemd-boot to the EFI System Partition must be 21 | handled separately. It is usually done by ```bootctl``` command line utility 22 | from systemd package. 23 | 24 | ### Dependencies 25 | 26 | * gnu-efi 27 | * xsltproc (optional) 28 | * pkg-config (optional) 29 | * qemu (optional) 30 | * OVMF (optional) 31 | 32 | ## Authors 33 | 34 | * Kay Sievers 35 | * Harald Hoyer 36 | * ... and many others (for complete list see git history at [[2]](https://www.github.com/systemd/systemd)) 37 | 38 | -------------------------------------------------------------------------------- /autogen.sh: -------------------------------------------------------------------------------- 1 | #!/bin/sh 2 | 3 | # This file is part of systemd-boot 4 | # 5 | # Copyright (C) 2013-2016 Karel Zak 6 | # Copyright (C) 2016 Michal Sekletar 7 | # 8 | # systemd-boot is free software; you can redistribute it and/or modify it 9 | # under the terms of the GNU Lesser General Public License as published by 10 | # the Free Software Foundation; either version 2.1 of the License, or 11 | # (at your option) any later version. 12 | # 13 | # systemd-boot is distributed in the hope that it will be useful, but 14 | # WITHOUT ANY WARRANTY; without even the implied warranty of 15 | # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU 16 | # Lesser General Public License for more details. 17 | # 18 | # You should have received a copy of the GNU Lesser General Public License 19 | # along with systemd; If not, see . 20 | 21 | set -e 22 | 23 | autoreconf --force --install --symlink 24 | 25 | args="--prefix=/usr" 26 | 27 | if [ "x$1" = "xc" ]; then 28 | ./configure $args 29 | make clean 30 | else 31 | echo 32 | echo "----------------------------------------------------------------" 33 | echo "Initialized build system. For a common configuration please run:" 34 | echo "----------------------------------------------------------------" 35 | echo 36 | echo "./configure $args" 37 | echo 38 | fi 39 | -------------------------------------------------------------------------------- /configure.ac: -------------------------------------------------------------------------------- 1 | # This file is part of systemd-boot 2 | # 3 | # Copyright (C) 2013-2016 Karel Zak 4 | # Copyright (C) 2016 Michal Sekeltar 5 | # 6 | # systemd-boot is free software; you can redistribute it and/or modify it 7 | # under the terms of the GNU Lesser General Public License as published by 8 | # the Free Software Foundation; either version 2.1 of the License, or 9 | # (at your option) any later version. 10 | # 11 | # systemd-boot is distributed in the hope that it will be useful, but 12 | # WITHOUT ANY WARRANTY; without even the implied warranty of 13 | # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU 14 | # Lesser General Public License for more details. 15 | # 16 | # You should have received a copy of the GNU Lesser General Public License 17 | # along with systemd-boot; If not, see . 18 | 19 | AC_PREREQ([2.64]) 20 | AC_INIT([systemd-boot], 21 | [232], 22 | [http://github.com/systemd/systemd-boot/issues], 23 | [systemd-boot], 24 | [http://github.com/systemd/systemd-boot]) 25 | 26 | AC_CONFIG_AUX_DIR([build-aux]) 27 | AC_CONFIG_MACRO_DIR([m4]) 28 | AC_CONFIG_SRCDIR([src/sd-boot/boot.c]) 29 | AC_CONFIG_HEADERS([config.h]) 30 | 31 | AM_INIT_AUTOMAKE([foreign 1.11 -Wall -Wno-portability silent-rules tar-pax no-dist-gzip dist-xz subdir-objects]) 32 | AC_CANONICAL_HOST 33 | 34 | # Figure out target architecture and EFI machine type 35 | ARCH="$host_cpu" 36 | 37 | AS_IF([test x"$host_cpu" = x"x86_64"], [MACHINE_TYPE_NAME=x64]) 38 | AS_CASE([$host_cpu], [i*86], [MACHINE_TYPE_NAME=ia32]) 39 | 40 | AM_CONDITIONAL(ARCH_X86_64, [test x"$MACHINE_TYPE_NAME" = "x64"]) 41 | AM_CONDITIONAL(ARCH_IA32, [test x"$MACHINE_TYPE_NAME" = "ia32"]) 42 | 43 | AC_SUBST([ARCH]) 44 | AC_SUBST([MACHINE_TYPE_NAME]) 45 | AC_DEFINE_UNQUOTED([MACHINE_TYPE_NAME], [$MACHINE_TYPE_NAME], [EFI machine type]) 46 | 47 | AC_SYS_LARGEFILE 48 | AC_SYS_LONG_FILE_NAMES 49 | 50 | AC_PROG_CC 51 | AC_PROG_MKDIR_P 52 | AC_PATH_PROG([QEMU], [qemu-system-$ARCH]) 53 | 54 | AC_PATH_PROG([XSLTPROC], [xsltproc]) 55 | 56 | PKG_PROG_PKG_CONFIG 57 | 58 | # ------------------------------------------------------------------------------ 59 | AC_ARG_WITH(ovmf, 60 | AS_HELP_STRING([--with-ovmf=PATH], [Path to OVMF firmware binary]), 61 | [OVMF_PATH="$withval"], 62 | [AC_MSG_WARN([*** No OVMF path specified, using defaults])]) 63 | 64 | # Search for OVMF firmware in default locations in case 65 | AS_IF([ test -z "$OVMF_PATH" ], [ 66 | AC_PATH_PROG([QEMU], [qemu-system-x86_64]) 67 | # Fedora 68 | AC_CHECK_FILE([/usr/share/edk2/ovmf/OVMF_CODE.fd], [QEMU_BIOS=/usr/share/edk2/ovmf/OVMF_CODE.fd]) 69 | # Debian 70 | AC_CHECK_FILE([/usr/share/qemu/OVMF.fd], [QEMU_BIOS=/usr/share/qemu/OVMF.fd]) 71 | # Arch Linux 72 | AC_CHECK_FILE([/usr/share/ovmf/${MACHINE_TYPE_NAME}/ovmf_${MACHINE_TYPE_NAME}.bin], [QEMU_BIOS=/usr/share/ovmf/${MACHINE_TYPE_NAME}/ovmf_${MACHINE_TYPE_NAME}.bin]) 73 | # SUSE 74 | AC_CHECK_FILE([/usr/share/qemu/ovmf-${ARCH}.bin], [QEMU_BIOS=/usr/share/qemu/ovmf-${ARCH}.bin]) 75 | AC_SUBST([QEMU_BIOS]) 76 | ]) 77 | 78 | # ------------------------------------------------------------------------------ 79 | CPPFLAGS="$CPPFLAGS -I/usr/include/efi/$ARCH" 80 | AC_CHECK_HEADER([efi/efi.h], [], [AC_MSG_ERROR([*** gnu-efi headers not found])]) 81 | 82 | efiroot=$(echo $(cd /usr/lib/$(gcc -print-multi-os-directory); pwd)) 83 | EFI_LIB_DIR="$efiroot" 84 | AC_ARG_WITH(efi-libdir, 85 | AS_HELP_STRING([--with-efi-libdir=PATH], [Path to efi lib directory]), 86 | [EFI_LIB_DIR="$withval"], [EFI_LIB_DIR="$efiroot"] 87 | ) 88 | AC_SUBST([EFI_LIB_DIR]) 89 | 90 | # ------------------------------------------------------------------------------ 91 | AC_ARG_WITH(efi-ldsdir, 92 | AS_HELP_STRING([--with-efi-ldsdir=PATH], [Path to efi lds directory]), 93 | [EFI_LDS_DIR="$withval"], 94 | [ 95 | for EFI_LDS_DIR in "${efiroot}/gnuefi" "${efiroot}"; do 96 | for lds in ${EFI_LDS_DIR}/elf_${ARCH}_efi.lds; do 97 | test -f ${lds} && break 2 98 | done 99 | done 100 | ] 101 | ) 102 | AC_SUBST([EFI_LDS_DIR]) 103 | 104 | # ------------------------------------------------------------------------------ 105 | AC_ARG_WITH(efi-includedir, 106 | AS_HELP_STRING([--with-efi-includedir=PATH], [Path to efi include directory]), 107 | [EFI_INC_DIR="$withval"], [EFI_INC_DIR="/usr/include"] 108 | ) 109 | AC_SUBST([EFI_INC_DIR]) 110 | 111 | # ------------------------------------------------------------------------------ 112 | have_tpm=no 113 | AC_ARG_ENABLE([tpm], AS_HELP_STRING([--enable-tpm], [Enable optional TPM support]), 114 | [case "${enableval}" in 115 | yes) have_tpm=yes ;; 116 | no) have_tpm=no ;; 117 | *) AC_MSG_ERROR(bad value ${enableval} for --enable-tpm) ;; 118 | esac], 119 | [have_tpm=no]) 120 | 121 | AS_IF([test "x${have_tpm}" != xno], AC_DEFINE(SD_BOOT_LOG_TPM, 1, [Define if TPM should be used to log events and extend the registers])) 122 | 123 | AC_ARG_WITH(tpm-pcrindex, 124 | AS_HELP_STRING([--with-tpm-pcrindex=], 125 | [TPM PCR register number to use]), 126 | [SD_TPM_PCR="$withval"], 127 | [SD_TPM_PCR="8"]) 128 | 129 | AC_DEFINE_UNQUOTED(SD_TPM_PCR, [$SD_TPM_PCR], [TPM PCR register number to use]) 130 | 131 | # ------------------------------------------------------------------------------ 132 | AC_ARG_WITH([bashcompletiondir], 133 | AS_HELP_STRING([--with-bashcompletiondir=DIR], [bash completions directory]), 134 | [], 135 | [AS_IF([$($PKG_CONFIG --exists bash-completion)], [ 136 | with_bashcompletiondir=$($PKG_CONFIG --variable=completionsdir bash-completion) 137 | ] , [ 138 | with_bashcompletiondir=${datadir}/bash-completion/completions 139 | ])]) 140 | AM_CONDITIONAL(ENABLE_BASH_COMPLETION, [test "$with_bashcompletiondir" != "no"]) 141 | AX_NORMALIZE_PATH([with_bashcompletiondir]) 142 | AC_SUBST([bashcompletiondir], [$with_bashcompletiondir]) 143 | 144 | # ------------------------------------------------------------------------------ 145 | AC_ARG_WITH([zshcompletiondir], 146 | AS_HELP_STRING([--with-zshcompletiondir=DIR], [zsh completions directory]), 147 | [], [with_zshcompletiondir=${datadir}/zsh/site-functions]) 148 | AM_CONDITIONAL(ENABLE_ZSH_COMPLETION, [test "$with_zshcompletiondir" != "no"]) 149 | AX_NORMALIZE_PATH([with_zshcompletiondir]) 150 | AC_SUBST([zshcompletiondir], [$with_zshcompletiondir]) 151 | 152 | # ------------------------------------------------------------------------------ 153 | have_manpages=no 154 | AC_ARG_ENABLE(manpages, AS_HELP_STRING([--disable-manpages], [disable manpages])) 155 | AS_IF([test "x$enable_manpages" != xno], [ 156 | AS_IF([test "x$enable_manpages" = xyes -a "x$XSLTPROC" = x], [ 157 | AC_MSG_ERROR([*** Manpages requested but xsltproc not found]) 158 | ]) 159 | AS_IF([test "x$XSLTPROC" != x], [have_manpages=yes]) 160 | ]) 161 | AM_CONDITIONAL(ENABLE_MANPAGES, [test "x$have_manpages" = "xyes"]) 162 | 163 | # ------------------------------------------------------------------------------ 164 | AC_CONFIG_FILES([ 165 | Makefile 166 | ]) 167 | 168 | AC_OUTPUT 169 | AC_MSG_RESULT([ 170 | $PACKAGE_NAME $VERSION 171 | 172 | prefix: ${prefix} 173 | datarootdir: ${datarootdir} 174 | Bash completions dir: ${with_bashcompletiondir} 175 | Zsh completions dir: ${with_zshcompletiondir} 176 | 177 | arch: $ARCH 178 | EFI machine type: $MACHINE_TYPE_NAME 179 | EFI libdir: ${EFI_LIB_DIR} 180 | EFI ldsdir: ${EFI_LDS_DIR} 181 | EFI includedir: ${EFI_INC_DIR} 182 | TPM: ${have_tpm} 183 | 184 | QEMU: ${QEMU} 185 | QEMU OVMF: ${QEMU_BIOS} 186 | ]) 187 | -------------------------------------------------------------------------------- /m4/ax_normalize_path.m4: -------------------------------------------------------------------------------- 1 | # =========================================================================== 2 | # http://www.gnu.org/software/autoconf-archive/ax_normalize_path.html 3 | # =========================================================================== 4 | # 5 | # SYNOPSIS 6 | # 7 | # AX_NORMALIZE_PATH(VARNAME, [REFERENCE_STRING]) 8 | # 9 | # DESCRIPTION 10 | # 11 | # Perform some cleanups on the value of $VARNAME (interpreted as a path): 12 | # 13 | # - empty paths are changed to '.' 14 | # - trailing slashes are removed 15 | # - repeated slashes are squeezed except a leading doubled slash '//' 16 | # (which might indicate a networked disk on some OS). 17 | # 18 | # REFERENCE_STRING is used to turn '/' into '\' and vice-versa: if 19 | # REFERENCE_STRING contains some backslashes, all slashes and backslashes 20 | # are turned into backslashes, otherwise they are all turned into slashes. 21 | # 22 | # This makes processing of DOS filenames quite easier, because you can 23 | # turn a filename to the Unix notation, make your processing, and turn it 24 | # back to original notation. 25 | # 26 | # filename='A:\FOO\\BAR\' 27 | # old_filename="$filename" 28 | # # Switch to the unix notation 29 | # AX_NORMALIZE_PATH([filename], ["/"]) 30 | # # now we have $filename = 'A:/FOO/BAR' and we can process it as if 31 | # # it was a Unix path. For instance let's say that you want 32 | # # to append '/subpath': 33 | # filename="$filename/subpath" 34 | # # finally switch back to the original notation 35 | # AX_NORMALIZE_PATH([filename], ["$old_filename"]) 36 | # # now $filename equals to 'A:\FOO\BAR\subpath' 37 | # 38 | # One good reason to make all path processing with the unix convention is 39 | # that backslashes have a special meaning in many cases. For instance 40 | # 41 | # expr 'A:\FOO' : 'A:\Foo' 42 | # 43 | # will return 0 because the second argument is a regex in which 44 | # backslashes have to be backslashed. In other words, to have the two 45 | # strings to match you should write this instead: 46 | # 47 | # expr 'A:\Foo' : 'A:\\Foo' 48 | # 49 | # Such behavior makes DOS filenames extremely unpleasant to work with. So 50 | # temporary turn your paths to the Unix notation, and revert them to the 51 | # original notation after the processing. See the macro 52 | # AX_COMPUTE_RELATIVE_PATHS for a concrete example of this. 53 | # 54 | # REFERENCE_STRING defaults to $VARIABLE, this means that slashes will be 55 | # converted to backslashes if $VARIABLE already contains some backslashes 56 | # (see $thirddir below). 57 | # 58 | # firstdir='/usr/local//share' 59 | # seconddir='C:\Program Files\\' 60 | # thirddir='C:\home/usr/' 61 | # AX_NORMALIZE_PATH([firstdir]) 62 | # AX_NORMALIZE_PATH([seconddir]) 63 | # AX_NORMALIZE_PATH([thirddir]) 64 | # # $firstdir = '/usr/local/share' 65 | # # $seconddir = 'C:\Program Files' 66 | # # $thirddir = 'C:\home\usr' 67 | # 68 | # LICENSE 69 | # 70 | # Copyright (c) 2008 Alexandre Duret-Lutz 71 | # 72 | # This program is free software; you can redistribute it and/or modify it 73 | # under the terms of the GNU General Public License as published by the 74 | # Free Software Foundation; either version 2 of the License, or (at your 75 | # option) any later version. 76 | # 77 | # This program is distributed in the hope that it will be useful, but 78 | # WITHOUT ANY WARRANTY; without even the implied warranty of 79 | # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General 80 | # Public License for more details. 81 | # 82 | # You should have received a copy of the GNU General Public License along 83 | # with this program. If not, see . 84 | # 85 | # As a special exception, the respective Autoconf Macro's copyright owner 86 | # gives unlimited permission to copy, distribute and modify the configure 87 | # scripts that are the output of Autoconf when processing the Macro. You 88 | # need not follow the terms of the GNU General Public License when using 89 | # or distributing such scripts, even though portions of the text of the 90 | # Macro appear in them. The GNU General Public License (GPL) does govern 91 | # all other use of the material that constitutes the Autoconf Macro. 92 | # 93 | # This special exception to the GPL applies to versions of the Autoconf 94 | # Macro released by the Autoconf Archive. When you make and distribute a 95 | # modified version of the Autoconf Macro, you may extend this special 96 | # exception to the GPL to apply to your modified version as well. 97 | 98 | #serial 5 99 | 100 | AU_ALIAS([ADL_NORMALIZE_PATH], [AX_NORMALIZE_PATH]) 101 | AC_DEFUN([AX_NORMALIZE_PATH], 102 | [case ":[$]$1:" in 103 | # change empty paths to '.' 104 | ::) $1='.' ;; 105 | # strip trailing slashes 106 | :*[[\\/]]:) $1=`echo "[$]$1" | sed 's,[[\\/]]*[$],,'` ;; 107 | :*:) ;; 108 | esac 109 | # squeze repeated slashes 110 | case ifelse($2,,"[$]$1",$2) in 111 | # if the path contains any backslashes, turn slashes into backslashes 112 | *\\*) $1=`echo "[$]$1" | sed 's,\(.\)[[\\/]][[\\/]]*,\1\\\\,g'` ;; 113 | # if the path contains slashes, also turn backslashes into slashes 114 | *) $1=`echo "[$]$1" | sed 's,\(.\)[[\\/]][[\\/]]*,\1/,g'` ;; 115 | esac]) 116 | -------------------------------------------------------------------------------- /man/kernel-install.xml: -------------------------------------------------------------------------------- 1 | 2 | 4 | 5 | 23 | 24 | 25 | 26 | 27 | kernel-install 28 | systemd 29 | 30 | 31 | 32 | Developer 33 | Harald 34 | Hoyer 35 | harald@redhat.com 36 | 37 | 38 | 39 | 40 | 41 | kernel-install 42 | 8 43 | 44 | 45 | 46 | kernel-install 47 | Add and remove kernel and initramfs images to and from /boot 48 | 49 | 50 | 51 | 52 | kernel-install 53 | COMMAND 54 | KERNEL-VERSION 55 | KERNEL-IMAGE 56 | 57 | 58 | 59 | 60 | Description 61 | 62 | kernel-install is used to install and remove kernel and 63 | initramfs images to and from /boot. 64 | 65 | 66 | kernel-install will execute the files 67 | located in the directory /usr/lib/kernel/install.d/ 68 | and the local administration directory /etc/kernel/install.d/. 69 | All files are collectively sorted and executed in lexical order, regardless of the directory in 70 | which they live. However, files with identical filenames replace each other. 71 | Files in /etc/kernel/install.d/ take precedence over files with the same name 72 | in /usr/lib/kernel/install.d/. This can be used to override a system-supplied 73 | executables with a local file if needed; a symbolic link in /etc/kernel/install.d/ 74 | with the same name as an executable in /usr/lib/kernel/install.d/, 75 | pointing to /dev/null, disables the executable entirely. Executables must have the 76 | extension .install; other extensions are ignored. 77 | 78 | An executable should return 0 on success. It may also 79 | return 77 to cause the whole operation to terminate 80 | (executables later in lexical order will be skipped). 81 | 82 | 83 | 84 | Commands 85 | The following commands are understood: 86 | 87 | 88 | add KERNEL-VERSION KERNEL-IMAGE 89 | 90 | kernel-install creates the directory 91 | /boot/MACHINE-ID/KERNEL-VERSION/ 92 | and calls executables from 93 | /usr/lib/kernel/install.d/*.install and 94 | /etc/kernel/install.d/*.install with 95 | the arguments 96 | add KERNEL-VERSION \ 97 | /boot/MACHINE-ID/KERNEL-VERSION/ KERNEL-IMAGE 98 | 99 | 100 | The kernel-install plugin 50-depmod.install runs depmod for the KERNEL-VERSION. 101 | 102 | The kernel-install plugin 103 | 90-loaderentry.install copies 104 | KERNEL-IMAGE to 105 | /boot/MACHINE-ID/KERNEL-VERSION/linux. 106 | It also creates a boot loader entry according to the boot 107 | loader specification in 108 | /boot/loader/entries/MACHINE-ID-KERNEL-VERSION.conf. 109 | The title of the entry is the 110 | PRETTY_NAME parameter specified 111 | in /etc/os-release or 112 | /usr/lib/os-release (if the former is 113 | missing), or "Linux 114 | KERNEL-VERSION", if unset. If 115 | the file initrd is found next to the 116 | linux file, the initrd will be added to 117 | the configuration. 118 | 119 | 120 | 121 | remove KERNEL-VERSION 122 | 123 | Calls executables from /usr/lib/kernel/install.d/*.install 124 | and /etc/kernel/install.d/*.install with the arguments 125 | remove KERNEL-VERSION /boot/MACHINE-ID/KERNEL-VERSION/ 126 | 127 | 128 | kernel-install removes the entire directory 129 | /boot/MACHINE-ID/KERNEL-VERSION/ afterwards. 130 | 131 | The kernel-install plugin 90-loaderentry.install removes the file 132 | /boot/loader/entries/MACHINE-ID-KERNEL-VERSION.conf. 133 | 134 | 135 | 136 | 137 | 138 | 139 | 140 | 141 | Exit status 142 | If every executable returns 0 or 77, 0 is returned, and a non-zero failure code otherwise. 143 | 144 | 145 | 146 | Files 147 | 148 | 149 | 150 | /usr/lib/kernel/install.d/*.install 151 | /etc/kernel/install.d/*.install 152 | 153 | 154 | Drop-in files which are executed by kernel-install. 155 | 156 | 157 | 158 | 159 | /etc/kernel/cmdline 160 | /proc/cmdline 161 | 162 | 163 | The content of the file /etc/kernel/cmdline specifies the kernel command line to use. 164 | If that file does not exist, /proc/cmdline is used. 165 | 166 | 167 | 168 | 169 | /etc/machine-id 170 | 171 | 172 | The content of the file specifies the machine identification MACHINE-ID. 173 | 174 | 175 | 176 | 177 | /etc/os-release 178 | /usr/lib/os-release 179 | 180 | 181 | The content of the file specifies the operating system title PRETTY_NAME. 182 | 183 | 184 | 185 | 186 | 187 | 188 | See Also 189 | 190 | machine-id5, 191 | os-release5, 192 | Boot loader specification 193 | 194 | 195 | 196 | 197 | -------------------------------------------------------------------------------- /shell-completion/bash/kernel-install: -------------------------------------------------------------------------------- 1 | # kernel-install(8) completion -*- shell-script -*- 2 | # 3 | # This file is part of systemd. 4 | # 5 | # Copyright 2013 Kay Sievers 6 | # Copyright 2013 Harald Hoyer 7 | # 8 | # systemd is free software; you can redistribute it and/or modify it 9 | # under the terms of the GNU Lesser General Public License as published by 10 | # the Free Software Foundation; either version 2.1 of the License, or 11 | # (at your option) any later version. 12 | # 13 | # systemd is distributed in the hope that it will be useful, but 14 | # WITHOUT ANY WARRANTY; without even the implied warranty of 15 | # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU 16 | # General Public License for more details. 17 | # 18 | # You should have received a copy of the GNU Lesser General Public License 19 | # along with systemd; If not, see . 20 | 21 | _kernel_install() { 22 | local comps 23 | local MACHINE_ID 24 | local cur=${COMP_WORDS[COMP_CWORD]} 25 | 26 | case $COMP_CWORD in 27 | 1) 28 | comps="add remove" 29 | ;; 30 | 2) 31 | comps=$(cd /lib/modules; echo [0-9]*) 32 | if [[ ${COMP_WORDS[1]} == "remove" ]] && [[ -f /etc/machine-id ]]; then 33 | read MACHINE_ID < /etc/machine-id 34 | if [[ $MACHINE_ID ]] && ( [[ -d /boot/$MACHINE_ID ]] || [[ -L /boot/$MACHINE_ID ]] ); then 35 | comps=$(cd "/boot/$MACHINE_ID"; echo [0-9]*) 36 | fi 37 | fi 38 | ;; 39 | 3) 40 | [[ "$cur" ]] || cur=/boot/vmlinuz-${COMP_WORDS[2]} 41 | comps=$(compgen -f -- "$cur") 42 | compopt -o filenames 43 | ;; 44 | esac 45 | 46 | COMPREPLY=( $(compgen -W '$comps' -- "$cur") ) 47 | return 0 48 | } 49 | 50 | complete -F _kernel_install kernel-install 51 | -------------------------------------------------------------------------------- /shell-completion/zsh/_kernel-install: -------------------------------------------------------------------------------- 1 | #compdef kernel-install 2 | 3 | _images(){ 4 | if [[ "$words[2]" == "remove" ]]; then 5 | _message 'No more options' 6 | else 7 | _path_files -W /boot/ -P /boot/ -g "vmlinuz-*" 8 | fi 9 | } 10 | 11 | _kernels(){ 12 | read _MACHINE_ID < /etc/machine-id 13 | _kernel=( /lib/modules/[0-9]* ) 14 | if [[ "$cmd" == "remove" && -n "$_MACHINE_ID" ]]; then 15 | _kernel=( "/boot/$_MACHINE_ID"/[0-9]* ) 16 | fi 17 | _kernel=( ${_kernel##*/} ) 18 | _describe "installed kernels" _kernel 19 | } 20 | 21 | _arguments \ 22 | '1::add or remove:(add remove)' \ 23 | '2::kernel versions:_kernels' \ 24 | '3::kernel images:_images' 25 | 26 | #vim: set ft=zsh sw=4 ts=4 et 27 | -------------------------------------------------------------------------------- /src/kernel-install/50-depmod.install: -------------------------------------------------------------------------------- 1 | #!/bin/bash 2 | # -*- mode: shell-script; indent-tabs-mode: nil; sh-basic-offset: 4; -*- 3 | # ex: ts=8 sw=4 sts=4 et filetype=sh 4 | 5 | [[ $1 == "add" ]] || exit 0 6 | [[ $2 ]] || exit 1 7 | 8 | exec depmod -a "$2" 9 | -------------------------------------------------------------------------------- /src/kernel-install/90-loaderentry.install: -------------------------------------------------------------------------------- 1 | #!/bin/bash 2 | # -*- mode: shell-script; indent-tabs-mode: nil; sh-basic-offset: 4; -*- 3 | # ex: ts=8 sw=4 sts=4 et filetype=sh 4 | 5 | COMMAND="$1" 6 | KERNEL_VERSION="$2" 7 | BOOT_DIR_ABS="$3" 8 | KERNEL_IMAGE="$4" 9 | 10 | if [[ -f /etc/machine-id ]]; then 11 | read MACHINE_ID < /etc/machine-id 12 | fi 13 | 14 | if ! [[ $MACHINE_ID ]]; then 15 | exit 1 16 | fi 17 | 18 | BOOT_DIR="/$MACHINE_ID/$KERNEL_VERSION" 19 | BOOT_ROOT=${BOOT_DIR_ABS%$BOOT_DIR} 20 | LOADER_ENTRY="$BOOT_ROOT/loader/entries/$MACHINE_ID-$KERNEL_VERSION.conf" 21 | 22 | if [[ $COMMAND == remove ]]; then 23 | exec rm -f "$LOADER_ENTRY" 24 | fi 25 | 26 | if ! [[ $COMMAND == add ]]; then 27 | exit 1 28 | fi 29 | 30 | if ! [[ $KERNEL_IMAGE ]]; then 31 | exit 1 32 | fi 33 | 34 | if [[ -f /etc/os-release ]]; then 35 | . /etc/os-release 36 | elif [[ -f /usr/lib/os-release ]]; then 37 | . /usr/lib/os-release 38 | fi 39 | 40 | if ! [[ $PRETTY_NAME ]]; then 41 | PRETTY_NAME="Linux $KERNEL_VERSION" 42 | fi 43 | 44 | declare -a BOOT_OPTIONS 45 | 46 | if [[ -f /etc/kernel/cmdline ]]; then 47 | read -r -d '' -a BOOT_OPTIONS < /etc/kernel/cmdline 48 | fi 49 | 50 | if ! [[ ${BOOT_OPTIONS[*]} ]]; then 51 | read -r -d '' -a line < /proc/cmdline 52 | for i in "${line[@]}"; do 53 | [[ "${i#initrd=*}" != "$i" ]] && continue 54 | BOOT_OPTIONS+=("$i") 55 | done 56 | fi 57 | 58 | if ! [[ ${BOOT_OPTIONS[*]} ]]; then 59 | echo "Could not determine the kernel command line parameters." >&2 60 | echo "Please specify the kernel command line in /etc/kernel/cmdline!" >&2 61 | exit 1 62 | fi 63 | 64 | cp "$KERNEL_IMAGE" "$BOOT_DIR_ABS/linux" && 65 | chown root:root "$BOOT_DIR_ABS/linux" && 66 | chmod 0644 "$BOOT_DIR_ABS/linux" || { 67 | echo "Could not copy '$KERNEL_IMAGE to '$BOOT_DIR_ABS/linux'." >&2 68 | exit 1 69 | } 70 | 71 | mkdir -p "${LOADER_ENTRY%/*}" || { 72 | echo "Could not create loader entry directory '${LOADER_ENTRY%/*}'." >&2 73 | exit 1 74 | } 75 | 76 | { 77 | echo "title $PRETTY_NAME" 78 | echo "version $KERNEL_VERSION" 79 | echo "machine-id $MACHINE_ID" 80 | echo "options ${BOOT_OPTIONS[*]}" 81 | echo "linux $BOOT_DIR/linux" 82 | [[ -f $BOOT_DIR_ABS/initrd ]] && \ 83 | echo "initrd $BOOT_DIR/initrd" 84 | : 85 | } > "$LOADER_ENTRY" || { 86 | echo "Could not create loader entry '$LOADER_ENTRY'." >&2 87 | exit 1 88 | } 89 | exit 0 90 | -------------------------------------------------------------------------------- /src/kernel-install/kernel-install: -------------------------------------------------------------------------------- 1 | #!/bin/bash 2 | # -*- mode: shell-script; indent-tabs-mode: nil; sh-basic-offset: 4; -*- 3 | # ex: ts=8 sw=4 sts=4 et filetype=sh 4 | # 5 | # This file is part of systemd. 6 | # 7 | # Copyright 2013 Harald Hoyer 8 | # 9 | # systemd is free software; you can redistribute it and/or modify it 10 | # under the terms of the GNU Lesser General Public License as published by 11 | # the Free Software Foundation; either version 2.1 of the License, or 12 | # (at your option) any later version. 13 | # 14 | # systemd is distributed in the hope that it will be useful, but 15 | # WITHOUT ANY WARRANTY; without even the implied warranty of 16 | # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU 17 | # General Public License for more details. 18 | # 19 | # You should have received a copy of the GNU Lesser General Public License 20 | # along with systemd; If not, see . 21 | 22 | SKIP_REMAINING=77 23 | 24 | usage() 25 | { 26 | echo "Usage:" 27 | echo " $0 add KERNEL-VERSION KERNEL-IMAGE" 28 | echo " $0 remove KERNEL-VERSION" 29 | } 30 | 31 | dropindirs_sort() 32 | { 33 | local suffix=$1; shift 34 | local -a files 35 | local f d i 36 | 37 | readarray -t files < <( 38 | for d in "$@"; do 39 | for i in "$d/"*"$suffix"; do 40 | if [[ -e "$i" ]]; then 41 | echo "${i##*/}" 42 | fi 43 | done 44 | done | sort -Vu 45 | ) 46 | 47 | for f in "${files[@]}"; do 48 | for d in "$@"; do 49 | if [[ -e "$d/$f" ]]; then 50 | echo "$d/$f" 51 | continue 2 52 | fi 53 | done 54 | done 55 | } 56 | 57 | export LC_COLLATE=C 58 | 59 | for i in "$@"; do 60 | if [ "$i" == "--help" -o "$i" == "-h" ]; then 61 | usage 62 | exit 0 63 | fi 64 | done 65 | 66 | if [[ "${0##*/}" == 'installkernel' ]]; then 67 | COMMAND='add' 68 | else 69 | COMMAND="$1" 70 | shift 71 | fi 72 | 73 | KERNEL_VERSION="$1" 74 | KERNEL_IMAGE="$2" 75 | 76 | if [[ -f /etc/machine-id ]]; then 77 | read MACHINE_ID < /etc/machine-id 78 | fi 79 | 80 | if ! [[ $MACHINE_ID ]]; then 81 | echo "Could not determine your machine ID from /etc/machine-id." >&2 82 | echo "Please run 'systemd-machine-id-setup' as root. See man:machine-id(5)" >&2 83 | exit 1 84 | fi 85 | 86 | if [[ ! $COMMAND ]] || [[ ! $KERNEL_VERSION ]]; then 87 | echo "Not enough arguments" >&2 88 | exit 1 89 | fi 90 | 91 | if [[ -d /efi/loader/entries ]] || [[ -d /efi/$MACHINE_ID ]]; then 92 | BOOT_DIR_ABS="/efi/$MACHINE_ID/$KERNEL_VERSION" 93 | elif [[ -d /boot/loader/entries ]] || [[ -d /boot/$MACHINE_ID ]]; then 94 | BOOT_DIR_ABS="/boot/$MACHINE_ID/$KERNEL_VERSION" 95 | elif [[ -d /boot/efi/loader/entries ]] || [[ -d /boot/efi/$MACHINE_ID ]]; then 96 | BOOT_DIR_ABS="/boot/efi/$MACHINE_ID/$KERNEL_VERSION" 97 | elif mountpoint -q /efi; then 98 | BOOT_DIR_ABS="/efi/$MACHINE_ID/$KERNEL_VERSION" 99 | elif mountpoint -q /boot/efi; then 100 | BOOT_DIR_ABS="/boot/efi/$MACHINE_ID/$KERNEL_VERSION" 101 | else 102 | BOOT_DIR_ABS="/boot/$MACHINE_ID/$KERNEL_VERSION" 103 | fi 104 | 105 | ret=0 106 | 107 | readarray -t PLUGINS < <( 108 | dropindirs_sort ".install" \ 109 | "/etc/kernel/install.d" \ 110 | "/usr/lib/kernel/install.d" 111 | ) 112 | 113 | case $COMMAND in 114 | add) 115 | if [[ ! "$KERNEL_IMAGE" ]]; then 116 | echo "Command 'add' requires an argument" >&2 117 | exit 1 118 | fi 119 | 120 | mkdir -p "$BOOT_DIR_ABS" || { 121 | echo "Could not create boot directory '$BOOT_DIR_ABS'." >&2 122 | exit 1 123 | } 124 | 125 | for f in "${PLUGINS[@]}"; do 126 | if [[ -x $f ]]; then 127 | "$f" add "$KERNEL_VERSION" "$BOOT_DIR_ABS" "$KERNEL_IMAGE" 128 | x=$? 129 | if [[ $x == $SKIP_REMAINING ]]; then 130 | exit 0 131 | fi 132 | ((ret+=$x)) 133 | fi 134 | done 135 | ;; 136 | 137 | remove) 138 | for f in "${PLUGINS[@]}"; do 139 | if [[ -x $f ]]; then 140 | "$f" remove "$KERNEL_VERSION" "$BOOT_DIR_ABS" 141 | x=$? 142 | if [[ $x == $SKIP_REMAINING ]]; then 143 | exit 0 144 | fi 145 | ((ret+=$x)) 146 | fi 147 | done 148 | 149 | rm -rf "$BOOT_DIR_ABS" 150 | ((ret+=$?)) 151 | ;; 152 | 153 | *) 154 | echo "Unknown command '$COMMAND'" >&2 155 | exit 1 156 | ;; 157 | esac 158 | 159 | exit $ret 160 | -------------------------------------------------------------------------------- /src/sd-boot/boot.c: -------------------------------------------------------------------------------- 1 | /*-*- Mode: C; c-basic-offset: 8; indent-tabs-mode: nil -*-*/ 2 | 3 | /* 4 | * This program is free software; you can redistribute it and/or modify it 5 | * under the terms of the GNU Lesser General Public License as published by 6 | * the Free Software Foundation; either version 2.1 of the License, or 7 | * (at your option) any later version. 8 | * 9 | * This program is distributed in the hope that it will be useful, but 10 | * WITHOUT ANY WARRANTY; without even the implied warranty of 11 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU 12 | * Lesser General Public License for more details. 13 | * 14 | * Copyright (C) 2012-2015 Kay Sievers 15 | * Copyright (C) 2012-2015 Harald Hoyer 16 | */ 17 | 18 | #include 19 | #include 20 | 21 | #include "console.h" 22 | #include "disk.h" 23 | #include "graphics.h" 24 | #include "linux.h" 25 | #include "pefile.h" 26 | #include "util.h" 27 | 28 | #ifndef EFI_OS_INDICATIONS_BOOT_TO_FW_UI 29 | #define EFI_OS_INDICATIONS_BOOT_TO_FW_UI 0x0000000000000001ULL 30 | #endif 31 | 32 | /* magic string to find in the binary image */ 33 | static const char __attribute__((used)) magic[] = "#### LoaderInfo: systemd-boot " VERSION " ####"; 34 | 35 | static const EFI_GUID global_guid = EFI_GLOBAL_VARIABLE; 36 | 37 | enum loader_type { 38 | LOADER_UNDEFINED, 39 | LOADER_EFI, 40 | LOADER_LINUX 41 | }; 42 | 43 | typedef struct { 44 | CHAR16 *file; 45 | CHAR16 *title_show; 46 | CHAR16 *title; 47 | CHAR16 *version; 48 | CHAR16 *machine_id; 49 | EFI_HANDLE *device; 50 | enum loader_type type; 51 | CHAR16 *loader; 52 | CHAR16 *options; 53 | CHAR16 key; 54 | EFI_STATUS (*call)(VOID); 55 | BOOLEAN no_autoselect; 56 | BOOLEAN non_unique; 57 | } ConfigEntry; 58 | 59 | typedef struct { 60 | ConfigEntry **entries; 61 | UINTN entry_count; 62 | INTN idx_default; 63 | INTN idx_default_efivar; 64 | UINTN timeout_sec; 65 | UINTN timeout_sec_config; 66 | INTN timeout_sec_efivar; 67 | CHAR16 *entry_default_pattern; 68 | CHAR16 *entry_oneshot; 69 | CHAR16 *options_edit; 70 | BOOLEAN no_editor; 71 | } Config; 72 | 73 | static VOID cursor_left(UINTN *cursor, UINTN *first) { 74 | if ((*cursor) > 0) 75 | (*cursor)--; 76 | else if ((*first) > 0) 77 | (*first)--; 78 | } 79 | 80 | static VOID cursor_right(UINTN *cursor, UINTN *first, UINTN x_max, UINTN len) { 81 | if ((*cursor)+1 < x_max) 82 | (*cursor)++; 83 | else if ((*first) + (*cursor) < len) 84 | (*first)++; 85 | } 86 | 87 | static BOOLEAN line_edit(CHAR16 *line_in, CHAR16 **line_out, UINTN x_max, UINTN y_pos) { 88 | CHAR16 *line; 89 | UINTN size; 90 | UINTN len; 91 | UINTN first; 92 | CHAR16 *print; 93 | UINTN cursor; 94 | UINTN clear; 95 | BOOLEAN exit; 96 | BOOLEAN enter; 97 | 98 | if (!line_in) 99 | line_in = L""; 100 | size = StrLen(line_in) + 1024; 101 | line = AllocatePool(size * sizeof(CHAR16)); 102 | StrCpy(line, line_in); 103 | len = StrLen(line); 104 | print = AllocatePool((x_max+1) * sizeof(CHAR16)); 105 | 106 | uefi_call_wrapper(ST->ConOut->EnableCursor, 2, ST->ConOut, TRUE); 107 | 108 | first = 0; 109 | cursor = 0; 110 | clear = 0; 111 | enter = FALSE; 112 | exit = FALSE; 113 | while (!exit) { 114 | EFI_STATUS err; 115 | UINT64 key; 116 | UINTN i; 117 | 118 | i = len - first; 119 | if (i >= x_max-1) 120 | i = x_max-1; 121 | CopyMem(print, line + first, i * sizeof(CHAR16)); 122 | while (clear > 0 && i < x_max-1) { 123 | clear--; 124 | print[i++] = ' '; 125 | } 126 | print[i] = '\0'; 127 | 128 | uefi_call_wrapper(ST->ConOut->SetCursorPosition, 3, ST->ConOut, 0, y_pos); 129 | uefi_call_wrapper(ST->ConOut->OutputString, 2, ST->ConOut, print); 130 | uefi_call_wrapper(ST->ConOut->SetCursorPosition, 3, ST->ConOut, cursor, y_pos); 131 | 132 | err = console_key_read(&key, TRUE); 133 | if (EFI_ERROR(err)) 134 | continue; 135 | 136 | switch (key) { 137 | case KEYPRESS(0, SCAN_ESC, 0): 138 | case KEYPRESS(EFI_CONTROL_PRESSED, 0, 'c'): 139 | case KEYPRESS(EFI_CONTROL_PRESSED, 0, 'g'): 140 | case KEYPRESS(EFI_CONTROL_PRESSED, 0, CHAR_CTRL('c')): 141 | case KEYPRESS(EFI_CONTROL_PRESSED, 0, CHAR_CTRL('g')): 142 | exit = TRUE; 143 | break; 144 | 145 | case KEYPRESS(0, SCAN_HOME, 0): 146 | case KEYPRESS(EFI_CONTROL_PRESSED, 0, 'a'): 147 | case KEYPRESS(EFI_CONTROL_PRESSED, 0, CHAR_CTRL('a')): 148 | /* beginning-of-line */ 149 | cursor = 0; 150 | first = 0; 151 | continue; 152 | 153 | case KEYPRESS(0, SCAN_END, 0): 154 | case KEYPRESS(EFI_CONTROL_PRESSED, 0, 'e'): 155 | case KEYPRESS(EFI_CONTROL_PRESSED, 0, CHAR_CTRL('e')): 156 | /* end-of-line */ 157 | cursor = len - first; 158 | if (cursor+1 >= x_max) { 159 | cursor = x_max-1; 160 | first = len - (x_max-1); 161 | } 162 | continue; 163 | 164 | case KEYPRESS(0, SCAN_DOWN, 0): 165 | case KEYPRESS(EFI_ALT_PRESSED, 0, 'f'): 166 | case KEYPRESS(EFI_CONTROL_PRESSED, SCAN_RIGHT, 0): 167 | /* forward-word */ 168 | while (line[first + cursor] && line[first + cursor] == ' ') 169 | cursor_right(&cursor, &first, x_max, len); 170 | while (line[first + cursor] && line[first + cursor] != ' ') 171 | cursor_right(&cursor, &first, x_max, len); 172 | uefi_call_wrapper(ST->ConOut->SetCursorPosition, 3, ST->ConOut, cursor, y_pos); 173 | continue; 174 | 175 | case KEYPRESS(0, SCAN_UP, 0): 176 | case KEYPRESS(EFI_ALT_PRESSED, 0, 'b'): 177 | case KEYPRESS(EFI_CONTROL_PRESSED, SCAN_LEFT, 0): 178 | /* backward-word */ 179 | if ((first + cursor) > 0 && line[first + cursor-1] == ' ') { 180 | cursor_left(&cursor, &first); 181 | while ((first + cursor) > 0 && line[first + cursor] == ' ') 182 | cursor_left(&cursor, &first); 183 | } 184 | while ((first + cursor) > 0 && line[first + cursor-1] != ' ') 185 | cursor_left(&cursor, &first); 186 | uefi_call_wrapper(ST->ConOut->SetCursorPosition, 3, ST->ConOut, cursor, y_pos); 187 | continue; 188 | 189 | case KEYPRESS(0, SCAN_RIGHT, 0): 190 | case KEYPRESS(EFI_CONTROL_PRESSED, 0, 'f'): 191 | case KEYPRESS(EFI_CONTROL_PRESSED, 0, CHAR_CTRL('f')): 192 | /* forward-char */ 193 | if (first + cursor == len) 194 | continue; 195 | cursor_right(&cursor, &first, x_max, len); 196 | uefi_call_wrapper(ST->ConOut->SetCursorPosition, 3, ST->ConOut, cursor, y_pos); 197 | continue; 198 | 199 | case KEYPRESS(0, SCAN_LEFT, 0): 200 | case KEYPRESS(EFI_CONTROL_PRESSED, 0, 'b'): 201 | case KEYPRESS(EFI_CONTROL_PRESSED, 0, CHAR_CTRL('b')): 202 | /* backward-char */ 203 | cursor_left(&cursor, &first); 204 | uefi_call_wrapper(ST->ConOut->SetCursorPosition, 3, ST->ConOut, cursor, y_pos); 205 | continue; 206 | 207 | case KEYPRESS(EFI_ALT_PRESSED, 0, 'd'): 208 | /* kill-word */ 209 | clear = 0; 210 | for (i = first + cursor; i < len && line[i] == ' '; i++) 211 | clear++; 212 | for (; i < len && line[i] != ' '; i++) 213 | clear++; 214 | 215 | for (i = first + cursor; i + clear < len; i++) 216 | line[i] = line[i + clear]; 217 | len -= clear; 218 | line[len] = '\0'; 219 | continue; 220 | 221 | case KEYPRESS(EFI_CONTROL_PRESSED, 0, 'w'): 222 | case KEYPRESS(EFI_CONTROL_PRESSED, 0, CHAR_CTRL('w')): 223 | case KEYPRESS(EFI_ALT_PRESSED, 0, CHAR_BACKSPACE): 224 | /* backward-kill-word */ 225 | clear = 0; 226 | if ((first + cursor) > 0 && line[first + cursor-1] == ' ') { 227 | cursor_left(&cursor, &first); 228 | clear++; 229 | while ((first + cursor) > 0 && line[first + cursor] == ' ') { 230 | cursor_left(&cursor, &first); 231 | clear++; 232 | } 233 | } 234 | while ((first + cursor) > 0 && line[first + cursor-1] != ' ') { 235 | cursor_left(&cursor, &first); 236 | clear++; 237 | } 238 | uefi_call_wrapper(ST->ConOut->SetCursorPosition, 3, ST->ConOut, cursor, y_pos); 239 | 240 | for (i = first + cursor; i + clear < len; i++) 241 | line[i] = line[i + clear]; 242 | len -= clear; 243 | line[len] = '\0'; 244 | continue; 245 | 246 | case KEYPRESS(0, SCAN_DELETE, 0): 247 | case KEYPRESS(EFI_CONTROL_PRESSED, 0, 'd'): 248 | case KEYPRESS(EFI_CONTROL_PRESSED, 0, CHAR_CTRL('d')): 249 | if (len == 0) 250 | continue; 251 | if (first + cursor == len) 252 | continue; 253 | for (i = first + cursor; i < len; i++) 254 | line[i] = line[i+1]; 255 | clear = 1; 256 | len--; 257 | continue; 258 | 259 | case KEYPRESS(EFI_CONTROL_PRESSED, 0, 'k'): 260 | case KEYPRESS(EFI_CONTROL_PRESSED, 0, CHAR_CTRL('k')): 261 | /* kill-line */ 262 | line[first + cursor] = '\0'; 263 | clear = len - (first + cursor); 264 | len = first + cursor; 265 | continue; 266 | 267 | case KEYPRESS(0, 0, CHAR_LINEFEED): 268 | case KEYPRESS(0, 0, CHAR_CARRIAGE_RETURN): 269 | if (StrCmp(line, line_in) != 0) { 270 | *line_out = line; 271 | line = NULL; 272 | } 273 | enter = TRUE; 274 | exit = TRUE; 275 | break; 276 | 277 | case KEYPRESS(0, 0, CHAR_BACKSPACE): 278 | if (len == 0) 279 | continue; 280 | if (first == 0 && cursor == 0) 281 | continue; 282 | for (i = first + cursor-1; i < len; i++) 283 | line[i] = line[i+1]; 284 | clear = 1; 285 | len--; 286 | if (cursor > 0) 287 | cursor--; 288 | if (cursor > 0 || first == 0) 289 | continue; 290 | /* show full line if it fits */ 291 | if (len < x_max) { 292 | cursor = first; 293 | first = 0; 294 | continue; 295 | } 296 | /* jump left to see what we delete */ 297 | if (first > 10) { 298 | first -= 10; 299 | cursor = 10; 300 | } else { 301 | cursor = first; 302 | first = 0; 303 | } 304 | continue; 305 | 306 | case KEYPRESS(0, 0, ' ') ... KEYPRESS(0, 0, '~'): 307 | case KEYPRESS(0, 0, 0x80) ... KEYPRESS(0, 0, 0xffff): 308 | if (len+1 == size) 309 | continue; 310 | for (i = len; i > first + cursor; i--) 311 | line[i] = line[i-1]; 312 | line[first + cursor] = KEYCHAR(key); 313 | len++; 314 | line[len] = '\0'; 315 | if (cursor+1 < x_max) 316 | cursor++; 317 | else if (first + cursor < len) 318 | first++; 319 | continue; 320 | } 321 | } 322 | 323 | uefi_call_wrapper(ST->ConOut->EnableCursor, 2, ST->ConOut, FALSE); 324 | FreePool(print); 325 | FreePool(line); 326 | return enter; 327 | } 328 | 329 | static UINTN entry_lookup_key(Config *config, UINTN start, CHAR16 key) { 330 | UINTN i; 331 | 332 | if (key == 0) 333 | return -1; 334 | 335 | /* select entry by number key */ 336 | if (key >= '1' && key <= '9') { 337 | i = key - '0'; 338 | if (i > config->entry_count) 339 | i = config->entry_count; 340 | return i-1; 341 | } 342 | 343 | /* find matching key in config entries */ 344 | for (i = start; i < config->entry_count; i++) 345 | if (config->entries[i]->key == key) 346 | return i; 347 | 348 | for (i = 0; i < start; i++) 349 | if (config->entries[i]->key == key) 350 | return i; 351 | 352 | return -1; 353 | } 354 | 355 | static VOID print_status(Config *config, CHAR16 *loaded_image_path) { 356 | UINT64 key; 357 | UINTN i; 358 | CHAR16 *s; 359 | CHAR8 *b; 360 | UINTN x; 361 | UINTN y; 362 | UINTN size; 363 | 364 | uefi_call_wrapper(ST->ConOut->SetAttribute, 2, ST->ConOut, EFI_LIGHTGRAY|EFI_BACKGROUND_BLACK); 365 | uefi_call_wrapper(ST->ConOut->ClearScreen, 1, ST->ConOut); 366 | 367 | Print(L"systemd-boot version: " VERSION "\n"); 368 | Print(L"architecture: " EFI_MACHINE_TYPE_NAME "\n"); 369 | Print(L"loaded image: %s\n", loaded_image_path); 370 | Print(L"UEFI specification: %d.%02d\n", ST->Hdr.Revision >> 16, ST->Hdr.Revision & 0xffff); 371 | Print(L"firmware vendor: %s\n", ST->FirmwareVendor); 372 | Print(L"firmware version: %d.%02d\n", ST->FirmwareRevision >> 16, ST->FirmwareRevision & 0xffff); 373 | 374 | if (uefi_call_wrapper(ST->ConOut->QueryMode, 4, ST->ConOut, ST->ConOut->Mode->Mode, &x, &y) == EFI_SUCCESS) 375 | Print(L"console size: %d x %d\n", x, y); 376 | 377 | if (efivar_get_raw(&global_guid, L"SecureBoot", &b, &size) == EFI_SUCCESS) { 378 | Print(L"SecureBoot: %s\n", yes_no(*b > 0)); 379 | FreePool(b); 380 | } 381 | 382 | if (efivar_get_raw(&global_guid, L"SetupMode", &b, &size) == EFI_SUCCESS) { 383 | Print(L"SetupMode: %s\n", *b > 0 ? L"setup" : L"user"); 384 | FreePool(b); 385 | } 386 | 387 | if (efivar_get_raw(&global_guid, L"OsIndicationsSupported", &b, &size) == EFI_SUCCESS) { 388 | Print(L"OsIndicationsSupported: %d\n", (UINT64)*b); 389 | FreePool(b); 390 | } 391 | Print(L"\n"); 392 | 393 | Print(L"timeout: %d\n", config->timeout_sec); 394 | if (config->timeout_sec_efivar >= 0) 395 | Print(L"timeout (EFI var): %d\n", config->timeout_sec_efivar); 396 | Print(L"timeout (config): %d\n", config->timeout_sec_config); 397 | if (config->entry_default_pattern) 398 | Print(L"default pattern: '%s'\n", config->entry_default_pattern); 399 | Print(L"editor: %s\n", yes_no(!config->no_editor)); 400 | Print(L"\n"); 401 | 402 | Print(L"config entry count: %d\n", config->entry_count); 403 | Print(L"entry selected idx: %d\n", config->idx_default); 404 | if (config->idx_default_efivar >= 0) 405 | Print(L"entry EFI var idx: %d\n", config->idx_default_efivar); 406 | Print(L"\n"); 407 | 408 | if (efivar_get_int(L"LoaderConfigTimeout", &i) == EFI_SUCCESS) 409 | Print(L"LoaderConfigTimeout: %d\n", i); 410 | if (config->entry_oneshot) 411 | Print(L"LoaderEntryOneShot: %s\n", config->entry_oneshot); 412 | if (efivar_get(L"LoaderDevicePartUUID", &s) == EFI_SUCCESS) { 413 | Print(L"LoaderDevicePartUUID: %s\n", s); 414 | FreePool(s); 415 | } 416 | if (efivar_get(L"LoaderEntryDefault", &s) == EFI_SUCCESS) { 417 | Print(L"LoaderEntryDefault: %s\n", s); 418 | FreePool(s); 419 | } 420 | 421 | Print(L"\n--- press key ---\n\n"); 422 | console_key_read(&key, TRUE); 423 | 424 | for (i = 0; i < config->entry_count; i++) { 425 | ConfigEntry *entry; 426 | 427 | if (key == KEYPRESS(0, SCAN_ESC, 0) || key == KEYPRESS(0, 0, 'q')) 428 | break; 429 | 430 | entry = config->entries[i]; 431 | Print(L"config entry: %d/%d\n", i+1, config->entry_count); 432 | if (entry->file) 433 | Print(L"file '%s'\n", entry->file); 434 | Print(L"title show '%s'\n", entry->title_show); 435 | if (entry->title) 436 | Print(L"title '%s'\n", entry->title); 437 | if (entry->version) 438 | Print(L"version '%s'\n", entry->version); 439 | if (entry->machine_id) 440 | Print(L"machine-id '%s'\n", entry->machine_id); 441 | if (entry->device) { 442 | EFI_DEVICE_PATH *device_path; 443 | CHAR16 *str; 444 | 445 | device_path = DevicePathFromHandle(entry->device); 446 | if (device_path) { 447 | str = DevicePathToStr(device_path); 448 | Print(L"device handle '%s'\n", str); 449 | FreePool(str); 450 | } 451 | } 452 | if (entry->loader) 453 | Print(L"loader '%s'\n", entry->loader); 454 | if (entry->options) 455 | Print(L"options '%s'\n", entry->options); 456 | Print(L"auto-select %s\n", yes_no(!entry->no_autoselect)); 457 | if (entry->call) 458 | Print(L"internal call yes\n"); 459 | 460 | Print(L"\n--- press key ---\n\n"); 461 | console_key_read(&key, TRUE); 462 | } 463 | 464 | uefi_call_wrapper(ST->ConOut->ClearScreen, 1, ST->ConOut); 465 | } 466 | 467 | static BOOLEAN menu_run(Config *config, ConfigEntry **chosen_entry, CHAR16 *loaded_image_path) { 468 | EFI_STATUS err; 469 | UINTN visible_max; 470 | UINTN idx_highlight; 471 | UINTN idx_highlight_prev; 472 | UINTN idx_first; 473 | UINTN idx_last; 474 | BOOLEAN refresh; 475 | BOOLEAN highlight; 476 | UINTN i; 477 | UINTN line_width; 478 | CHAR16 **lines; 479 | UINTN x_start; 480 | UINTN y_start; 481 | UINTN x_max; 482 | UINTN y_max; 483 | CHAR16 *status; 484 | CHAR16 *clearline; 485 | INTN timeout_remain; 486 | INT16 idx; 487 | BOOLEAN exit = FALSE; 488 | BOOLEAN run = TRUE; 489 | BOOLEAN wait = FALSE; 490 | 491 | graphics_mode(FALSE); 492 | uefi_call_wrapper(ST->ConIn->Reset, 2, ST->ConIn, FALSE); 493 | uefi_call_wrapper(ST->ConOut->EnableCursor, 2, ST->ConOut, FALSE); 494 | uefi_call_wrapper(ST->ConOut->SetAttribute, 2, ST->ConOut, EFI_LIGHTGRAY|EFI_BACKGROUND_BLACK); 495 | 496 | /* draw a single character to make ClearScreen work on some firmware */ 497 | uefi_call_wrapper(ST->ConOut->OutputString, 2, ST->ConOut, L" "); 498 | uefi_call_wrapper(ST->ConOut->ClearScreen, 1, ST->ConOut); 499 | 500 | err = uefi_call_wrapper(ST->ConOut->QueryMode, 4, ST->ConOut, ST->ConOut->Mode->Mode, &x_max, &y_max); 501 | if (EFI_ERROR(err)) { 502 | x_max = 80; 503 | y_max = 25; 504 | } 505 | 506 | /* we check 10 times per second for a keystroke */ 507 | if (config->timeout_sec > 0) 508 | timeout_remain = config->timeout_sec * 10; 509 | else 510 | timeout_remain = -1; 511 | 512 | idx_highlight = config->idx_default; 513 | idx_highlight_prev = 0; 514 | 515 | visible_max = y_max - 2; 516 | 517 | if ((UINTN)config->idx_default >= visible_max) 518 | idx_first = config->idx_default-1; 519 | else 520 | idx_first = 0; 521 | 522 | idx_last = idx_first + visible_max-1; 523 | 524 | refresh = TRUE; 525 | highlight = FALSE; 526 | 527 | /* length of the longest entry */ 528 | line_width = 5; 529 | for (i = 0; i < config->entry_count; i++) { 530 | UINTN entry_len; 531 | 532 | entry_len = StrLen(config->entries[i]->title_show); 533 | if (line_width < entry_len) 534 | line_width = entry_len; 535 | } 536 | if (line_width > x_max-6) 537 | line_width = x_max-6; 538 | 539 | /* offsets to center the entries on the screen */ 540 | x_start = (x_max - (line_width)) / 2; 541 | if (config->entry_count < visible_max) 542 | y_start = ((visible_max - config->entry_count) / 2) + 1; 543 | else 544 | y_start = 0; 545 | 546 | /* menu entries title lines */ 547 | lines = AllocatePool(sizeof(CHAR16 *) * config->entry_count); 548 | for (i = 0; i < config->entry_count; i++) { 549 | UINTN j, k; 550 | 551 | lines[i] = AllocatePool(((x_max+1) * sizeof(CHAR16))); 552 | for (j = 0; j < x_start; j++) 553 | lines[i][j] = ' '; 554 | 555 | for (k = 0; config->entries[i]->title_show[k] != '\0' && j < x_max; j++, k++) 556 | lines[i][j] = config->entries[i]->title_show[k]; 557 | 558 | for (; j < x_max; j++) 559 | lines[i][j] = ' '; 560 | lines[i][x_max] = '\0'; 561 | } 562 | 563 | status = NULL; 564 | clearline = AllocatePool((x_max+1) * sizeof(CHAR16)); 565 | for (i = 0; i < x_max; i++) 566 | clearline[i] = ' '; 567 | clearline[i] = 0; 568 | 569 | while (!exit) { 570 | UINT64 key; 571 | 572 | if (refresh) { 573 | for (i = 0; i < config->entry_count; i++) { 574 | if (i < idx_first || i > idx_last) 575 | continue; 576 | uefi_call_wrapper(ST->ConOut->SetCursorPosition, 3, ST->ConOut, 0, y_start + i - idx_first); 577 | if (i == idx_highlight) 578 | uefi_call_wrapper(ST->ConOut->SetAttribute, 2, ST->ConOut, 579 | EFI_BLACK|EFI_BACKGROUND_LIGHTGRAY); 580 | else 581 | uefi_call_wrapper(ST->ConOut->SetAttribute, 2, ST->ConOut, 582 | EFI_LIGHTGRAY|EFI_BACKGROUND_BLACK); 583 | uefi_call_wrapper(ST->ConOut->OutputString, 2, ST->ConOut, lines[i]); 584 | if ((INTN)i == config->idx_default_efivar) { 585 | uefi_call_wrapper(ST->ConOut->SetCursorPosition, 3, ST->ConOut, x_start-3, y_start + i - idx_first); 586 | uefi_call_wrapper(ST->ConOut->OutputString, 2, ST->ConOut, L"=>"); 587 | } 588 | } 589 | refresh = FALSE; 590 | } else if (highlight) { 591 | uefi_call_wrapper(ST->ConOut->SetCursorPosition, 3, ST->ConOut, 0, y_start + idx_highlight_prev - idx_first); 592 | uefi_call_wrapper(ST->ConOut->SetAttribute, 2, ST->ConOut, EFI_LIGHTGRAY|EFI_BACKGROUND_BLACK); 593 | uefi_call_wrapper(ST->ConOut->OutputString, 2, ST->ConOut, lines[idx_highlight_prev]); 594 | if ((INTN)idx_highlight_prev == config->idx_default_efivar) { 595 | uefi_call_wrapper(ST->ConOut->SetCursorPosition, 3, ST->ConOut, x_start-3, y_start + idx_highlight_prev - idx_first); 596 | uefi_call_wrapper(ST->ConOut->OutputString, 2, ST->ConOut, L"=>"); 597 | } 598 | 599 | uefi_call_wrapper(ST->ConOut->SetCursorPosition, 3, ST->ConOut, 0, y_start + idx_highlight - idx_first); 600 | uefi_call_wrapper(ST->ConOut->SetAttribute, 2, ST->ConOut, EFI_BLACK|EFI_BACKGROUND_LIGHTGRAY); 601 | uefi_call_wrapper(ST->ConOut->OutputString, 2, ST->ConOut, lines[idx_highlight]); 602 | if ((INTN)idx_highlight == config->idx_default_efivar) { 603 | uefi_call_wrapper(ST->ConOut->SetCursorPosition, 3, ST->ConOut, x_start-3, y_start + idx_highlight - idx_first); 604 | uefi_call_wrapper(ST->ConOut->OutputString, 2, ST->ConOut, L"=>"); 605 | } 606 | highlight = FALSE; 607 | } 608 | 609 | if (timeout_remain > 0) { 610 | FreePool(status); 611 | status = PoolPrint(L"Boot in %d sec.", (timeout_remain + 5) / 10); 612 | } 613 | 614 | /* print status at last line of screen */ 615 | if (status) { 616 | UINTN len; 617 | UINTN x; 618 | 619 | /* center line */ 620 | len = StrLen(status); 621 | if (len < x_max) 622 | x = (x_max - len) / 2; 623 | else 624 | x = 0; 625 | uefi_call_wrapper(ST->ConOut->SetAttribute, 2, ST->ConOut, EFI_LIGHTGRAY|EFI_BACKGROUND_BLACK); 626 | uefi_call_wrapper(ST->ConOut->SetCursorPosition, 3, ST->ConOut, 0, y_max-1); 627 | uefi_call_wrapper(ST->ConOut->OutputString, 2, ST->ConOut, clearline + (x_max - x)); 628 | uefi_call_wrapper(ST->ConOut->OutputString, 2, ST->ConOut, status); 629 | uefi_call_wrapper(ST->ConOut->OutputString, 2, ST->ConOut, clearline+1 + x + len); 630 | } 631 | 632 | err = console_key_read(&key, wait); 633 | if (EFI_ERROR(err)) { 634 | /* timeout reached */ 635 | if (timeout_remain == 0) { 636 | exit = TRUE; 637 | break; 638 | } 639 | 640 | /* sleep and update status */ 641 | if (timeout_remain > 0) { 642 | uefi_call_wrapper(BS->Stall, 1, 100 * 1000); 643 | timeout_remain--; 644 | continue; 645 | } 646 | 647 | /* timeout disabled, wait for next key */ 648 | wait = TRUE; 649 | continue; 650 | } 651 | 652 | timeout_remain = -1; 653 | 654 | /* clear status after keystroke */ 655 | if (status) { 656 | FreePool(status); 657 | status = NULL; 658 | uefi_call_wrapper(ST->ConOut->SetAttribute, 2, ST->ConOut, EFI_LIGHTGRAY|EFI_BACKGROUND_BLACK); 659 | uefi_call_wrapper(ST->ConOut->SetCursorPosition, 3, ST->ConOut, 0, y_max-1); 660 | uefi_call_wrapper(ST->ConOut->OutputString, 2, ST->ConOut, clearline+1); 661 | } 662 | 663 | idx_highlight_prev = idx_highlight; 664 | 665 | switch (key) { 666 | case KEYPRESS(0, SCAN_UP, 0): 667 | case KEYPRESS(0, 0, 'k'): 668 | if (idx_highlight > 0) 669 | idx_highlight--; 670 | break; 671 | 672 | case KEYPRESS(0, SCAN_DOWN, 0): 673 | case KEYPRESS(0, 0, 'j'): 674 | if (idx_highlight < config->entry_count-1) 675 | idx_highlight++; 676 | break; 677 | 678 | case KEYPRESS(0, SCAN_HOME, 0): 679 | case KEYPRESS(EFI_ALT_PRESSED, 0, '<'): 680 | if (idx_highlight > 0) { 681 | refresh = TRUE; 682 | idx_highlight = 0; 683 | } 684 | break; 685 | 686 | case KEYPRESS(0, SCAN_END, 0): 687 | case KEYPRESS(EFI_ALT_PRESSED, 0, '>'): 688 | if (idx_highlight < config->entry_count-1) { 689 | refresh = TRUE; 690 | idx_highlight = config->entry_count-1; 691 | } 692 | break; 693 | 694 | case KEYPRESS(0, SCAN_PAGE_UP, 0): 695 | if (idx_highlight > visible_max) 696 | idx_highlight -= visible_max; 697 | else 698 | idx_highlight = 0; 699 | break; 700 | 701 | case KEYPRESS(0, SCAN_PAGE_DOWN, 0): 702 | idx_highlight += visible_max; 703 | if (idx_highlight > config->entry_count-1) 704 | idx_highlight = config->entry_count-1; 705 | break; 706 | 707 | case KEYPRESS(0, 0, CHAR_LINEFEED): 708 | case KEYPRESS(0, 0, CHAR_CARRIAGE_RETURN): 709 | exit = TRUE; 710 | break; 711 | 712 | case KEYPRESS(0, SCAN_F1, 0): 713 | case KEYPRESS(0, 0, 'h'): 714 | case KEYPRESS(0, 0, '?'): 715 | status = StrDuplicate(L"(d)efault, (t/T)timeout, (e)dit, (v)ersion (Q)uit (P)rint (h)elp"); 716 | break; 717 | 718 | case KEYPRESS(0, 0, 'Q'): 719 | exit = TRUE; 720 | run = FALSE; 721 | break; 722 | 723 | case KEYPRESS(0, 0, 'd'): 724 | if (config->idx_default_efivar != (INTN)idx_highlight) { 725 | /* store the selected entry in a persistent EFI variable */ 726 | efivar_set(L"LoaderEntryDefault", config->entries[idx_highlight]->file, TRUE); 727 | config->idx_default_efivar = idx_highlight; 728 | status = StrDuplicate(L"Default boot entry selected."); 729 | } else { 730 | /* clear the default entry EFI variable */ 731 | efivar_set(L"LoaderEntryDefault", NULL, TRUE); 732 | config->idx_default_efivar = -1; 733 | status = StrDuplicate(L"Default boot entry cleared."); 734 | } 735 | refresh = TRUE; 736 | break; 737 | 738 | case KEYPRESS(0, 0, '-'): 739 | case KEYPRESS(0, 0, 'T'): 740 | if (config->timeout_sec_efivar > 0) { 741 | config->timeout_sec_efivar--; 742 | efivar_set_int(L"LoaderConfigTimeout", config->timeout_sec_efivar, TRUE); 743 | if (config->timeout_sec_efivar > 0) 744 | status = PoolPrint(L"Menu timeout set to %d sec.", config->timeout_sec_efivar); 745 | else 746 | status = StrDuplicate(L"Menu disabled. Hold down key at bootup to show menu."); 747 | } else if (config->timeout_sec_efivar <= 0){ 748 | config->timeout_sec_efivar = -1; 749 | efivar_set(L"LoaderConfigTimeout", NULL, TRUE); 750 | if (config->timeout_sec_config > 0) 751 | status = PoolPrint(L"Menu timeout of %d sec is defined by configuration file.", 752 | config->timeout_sec_config); 753 | else 754 | status = StrDuplicate(L"Menu disabled. Hold down key at bootup to show menu."); 755 | } 756 | break; 757 | 758 | case KEYPRESS(0, 0, '+'): 759 | case KEYPRESS(0, 0, 't'): 760 | if (config->timeout_sec_efivar == -1 && config->timeout_sec_config == 0) 761 | config->timeout_sec_efivar++; 762 | config->timeout_sec_efivar++; 763 | efivar_set_int(L"LoaderConfigTimeout", config->timeout_sec_efivar, TRUE); 764 | if (config->timeout_sec_efivar > 0) 765 | status = PoolPrint(L"Menu timeout set to %d sec.", 766 | config->timeout_sec_efivar); 767 | else 768 | status = StrDuplicate(L"Menu disabled. Hold down key at bootup to show menu."); 769 | break; 770 | 771 | case KEYPRESS(0, 0, 'e'): 772 | /* only the options of configured entries can be edited */ 773 | if (config->no_editor || config->entries[idx_highlight]->type == LOADER_UNDEFINED) 774 | break; 775 | uefi_call_wrapper(ST->ConOut->SetAttribute, 2, ST->ConOut, EFI_LIGHTGRAY|EFI_BACKGROUND_BLACK); 776 | uefi_call_wrapper(ST->ConOut->SetCursorPosition, 3, ST->ConOut, 0, y_max-1); 777 | uefi_call_wrapper(ST->ConOut->OutputString, 2, ST->ConOut, clearline+1); 778 | if (line_edit(config->entries[idx_highlight]->options, &config->options_edit, x_max-1, y_max-1)) 779 | exit = TRUE; 780 | uefi_call_wrapper(ST->ConOut->SetCursorPosition, 3, ST->ConOut, 0, y_max-1); 781 | uefi_call_wrapper(ST->ConOut->OutputString, 2, ST->ConOut, clearline+1); 782 | break; 783 | 784 | case KEYPRESS(0, 0, 'v'): 785 | status = PoolPrint(L"systemd-boot " VERSION " (" EFI_MACHINE_TYPE_NAME "), UEFI Specification %d.%02d, Vendor %s %d.%02d", 786 | ST->Hdr.Revision >> 16, ST->Hdr.Revision & 0xffff, 787 | ST->FirmwareVendor, ST->FirmwareRevision >> 16, ST->FirmwareRevision & 0xffff); 788 | break; 789 | 790 | case KEYPRESS(0, 0, 'P'): 791 | print_status(config, loaded_image_path); 792 | refresh = TRUE; 793 | break; 794 | 795 | case KEYPRESS(EFI_CONTROL_PRESSED, 0, 'l'): 796 | case KEYPRESS(EFI_CONTROL_PRESSED, 0, CHAR_CTRL('l')): 797 | refresh = TRUE; 798 | break; 799 | 800 | default: 801 | /* jump with a hotkey directly to a matching entry */ 802 | idx = entry_lookup_key(config, idx_highlight+1, KEYCHAR(key)); 803 | if (idx < 0) 804 | break; 805 | idx_highlight = idx; 806 | refresh = TRUE; 807 | } 808 | 809 | if (idx_highlight > idx_last) { 810 | idx_last = idx_highlight; 811 | idx_first = 1 + idx_highlight - visible_max; 812 | refresh = TRUE; 813 | } else if (idx_highlight < idx_first) { 814 | idx_first = idx_highlight; 815 | idx_last = idx_highlight + visible_max-1; 816 | refresh = TRUE; 817 | } 818 | 819 | if (!refresh && idx_highlight != idx_highlight_prev) 820 | highlight = TRUE; 821 | } 822 | 823 | *chosen_entry = config->entries[idx_highlight]; 824 | 825 | for (i = 0; i < config->entry_count; i++) 826 | FreePool(lines[i]); 827 | FreePool(lines); 828 | FreePool(clearline); 829 | 830 | uefi_call_wrapper(ST->ConOut->SetAttribute, 2, ST->ConOut, EFI_WHITE|EFI_BACKGROUND_BLACK); 831 | uefi_call_wrapper(ST->ConOut->ClearScreen, 1, ST->ConOut); 832 | return run; 833 | } 834 | 835 | static VOID config_add_entry(Config *config, ConfigEntry *entry) { 836 | if ((config->entry_count & 15) == 0) { 837 | UINTN i; 838 | 839 | i = config->entry_count + 16; 840 | if (config->entry_count == 0) 841 | config->entries = AllocatePool(sizeof(VOID *) * i); 842 | else 843 | config->entries = ReallocatePool(config->entries, 844 | sizeof(VOID *) * config->entry_count, sizeof(VOID *) * i); 845 | } 846 | config->entries[config->entry_count++] = entry; 847 | } 848 | 849 | static VOID config_entry_free(ConfigEntry *entry) { 850 | FreePool(entry->title_show); 851 | FreePool(entry->title); 852 | FreePool(entry->machine_id); 853 | FreePool(entry->loader); 854 | FreePool(entry->options); 855 | } 856 | 857 | static BOOLEAN is_digit(CHAR16 c) { 858 | return (c >= '0') && (c <= '9'); 859 | } 860 | 861 | static UINTN c_order(CHAR16 c) { 862 | if (c == '\0') 863 | return 0; 864 | if (is_digit(c)) 865 | return 0; 866 | else if ((c >= 'a') && (c <= 'z')) 867 | return c; 868 | else 869 | return c + 0x10000; 870 | } 871 | 872 | static INTN str_verscmp(CHAR16 *s1, CHAR16 *s2) { 873 | CHAR16 *os1 = s1; 874 | CHAR16 *os2 = s2; 875 | 876 | while (*s1 || *s2) { 877 | INTN first; 878 | 879 | while ((*s1 && !is_digit(*s1)) || (*s2 && !is_digit(*s2))) { 880 | INTN order; 881 | 882 | order = c_order(*s1) - c_order(*s2); 883 | if (order) 884 | return order; 885 | s1++; 886 | s2++; 887 | } 888 | 889 | while (*s1 == '0') 890 | s1++; 891 | while (*s2 == '0') 892 | s2++; 893 | 894 | first = 0; 895 | while (is_digit(*s1) && is_digit(*s2)) { 896 | if (first == 0) 897 | first = *s1 - *s2; 898 | s1++; 899 | s2++; 900 | } 901 | 902 | if (is_digit(*s1)) 903 | return 1; 904 | if (is_digit(*s2)) 905 | return -1; 906 | 907 | if (first) 908 | return first; 909 | } 910 | 911 | return StrCmp(os1, os2); 912 | } 913 | 914 | static CHAR8 *line_get_key_value(CHAR8 *content, CHAR8 *sep, UINTN *pos, CHAR8 **key_ret, CHAR8 **value_ret) { 915 | CHAR8 *line; 916 | UINTN linelen; 917 | CHAR8 *value; 918 | 919 | skip: 920 | line = content + *pos; 921 | if (*line == '\0') 922 | return NULL; 923 | 924 | linelen = 0; 925 | while (line[linelen] && !strchra((CHAR8 *)"\n\r", line[linelen])) 926 | linelen++; 927 | 928 | /* move pos to next line */ 929 | *pos += linelen; 930 | if (content[*pos]) 931 | (*pos)++; 932 | 933 | /* empty line */ 934 | if (linelen == 0) 935 | goto skip; 936 | 937 | /* terminate line */ 938 | line[linelen] = '\0'; 939 | 940 | /* remove leading whitespace */ 941 | while (strchra((CHAR8 *)" \t", *line)) { 942 | line++; 943 | linelen--; 944 | } 945 | 946 | /* remove trailing whitespace */ 947 | while (linelen > 0 && strchra(sep, line[linelen-1])) 948 | linelen--; 949 | line[linelen] = '\0'; 950 | 951 | if (*line == '#') 952 | goto skip; 953 | 954 | /* split key/value */ 955 | value = line; 956 | while (*value && !strchra(sep, *value)) 957 | value++; 958 | if (*value == '\0') 959 | goto skip; 960 | *value = '\0'; 961 | value++; 962 | while (*value && strchra(sep, *value)) 963 | value++; 964 | 965 | /* unquote */ 966 | if (value[0] == '\"' && line[linelen-1] == '\"') { 967 | value++; 968 | line[linelen-1] = '\0'; 969 | } 970 | 971 | *key_ret = line; 972 | *value_ret = value; 973 | return line; 974 | } 975 | 976 | static VOID config_defaults_load_from_file(Config *config, CHAR8 *content) { 977 | CHAR8 *line; 978 | UINTN pos = 0; 979 | CHAR8 *key, *value; 980 | 981 | line = content; 982 | while ((line = line_get_key_value(content, (CHAR8 *)" \t", &pos, &key, &value))) { 983 | if (strcmpa((CHAR8 *)"timeout", key) == 0) { 984 | CHAR16 *s; 985 | 986 | s = stra_to_str(value); 987 | config->timeout_sec_config = Atoi(s); 988 | config->timeout_sec = config->timeout_sec_config; 989 | FreePool(s); 990 | continue; 991 | } 992 | 993 | if (strcmpa((CHAR8 *)"default", key) == 0) { 994 | FreePool(config->entry_default_pattern); 995 | config->entry_default_pattern = stra_to_str(value); 996 | StrLwr(config->entry_default_pattern); 997 | continue; 998 | } 999 | 1000 | if (strcmpa((CHAR8 *)"editor", key) == 0) { 1001 | BOOLEAN on; 1002 | 1003 | if (EFI_ERROR(parse_boolean(value, &on))) 1004 | continue; 1005 | config->no_editor = !on; 1006 | } 1007 | } 1008 | } 1009 | 1010 | static VOID config_entry_add_from_file(Config *config, EFI_HANDLE *device, CHAR16 *file, CHAR8 *content, CHAR16 *loaded_image_path) { 1011 | ConfigEntry *entry; 1012 | CHAR8 *line; 1013 | UINTN pos = 0; 1014 | CHAR8 *key, *value; 1015 | UINTN len; 1016 | CHAR16 *initrd = NULL; 1017 | 1018 | entry = AllocateZeroPool(sizeof(ConfigEntry)); 1019 | 1020 | line = content; 1021 | while ((line = line_get_key_value(content, (CHAR8 *)" \t", &pos, &key, &value))) { 1022 | if (strcmpa((CHAR8 *)"title", key) == 0) { 1023 | FreePool(entry->title); 1024 | entry->title = stra_to_str(value); 1025 | continue; 1026 | } 1027 | 1028 | if (strcmpa((CHAR8 *)"version", key) == 0) { 1029 | FreePool(entry->version); 1030 | entry->version = stra_to_str(value); 1031 | continue; 1032 | } 1033 | 1034 | if (strcmpa((CHAR8 *)"machine-id", key) == 0) { 1035 | FreePool(entry->machine_id); 1036 | entry->machine_id = stra_to_str(value); 1037 | continue; 1038 | } 1039 | 1040 | if (strcmpa((CHAR8 *)"linux", key) == 0) { 1041 | FreePool(entry->loader); 1042 | entry->type = LOADER_LINUX; 1043 | entry->loader = stra_to_path(value); 1044 | entry->key = 'l'; 1045 | continue; 1046 | } 1047 | 1048 | if (strcmpa((CHAR8 *)"efi", key) == 0) { 1049 | entry->type = LOADER_EFI; 1050 | FreePool(entry->loader); 1051 | entry->loader = stra_to_path(value); 1052 | 1053 | /* do not add an entry for ourselves */ 1054 | if (StriCmp(entry->loader, loaded_image_path) == 0) { 1055 | entry->type = LOADER_UNDEFINED; 1056 | break; 1057 | } 1058 | continue; 1059 | } 1060 | 1061 | if (strcmpa((CHAR8 *)"architecture", key) == 0) { 1062 | /* do not add an entry for an EFI image of architecture not matching with that of the image */ 1063 | if (strcmpa((CHAR8 *)EFI_MACHINE_TYPE_NAME, value) != 0) { 1064 | entry->type = LOADER_UNDEFINED; 1065 | break; 1066 | } 1067 | continue; 1068 | } 1069 | 1070 | if (strcmpa((CHAR8 *)"initrd", key) == 0) { 1071 | CHAR16 *new; 1072 | 1073 | new = stra_to_path(value); 1074 | if (initrd) { 1075 | CHAR16 *s; 1076 | 1077 | s = PoolPrint(L"%s initrd=%s", initrd, new); 1078 | FreePool(initrd); 1079 | initrd = s; 1080 | } else 1081 | initrd = PoolPrint(L"initrd=%s", new); 1082 | FreePool(new); 1083 | continue; 1084 | } 1085 | 1086 | if (strcmpa((CHAR8 *)"options", key) == 0) { 1087 | CHAR16 *new; 1088 | 1089 | new = stra_to_str(value); 1090 | if (entry->options) { 1091 | CHAR16 *s; 1092 | 1093 | s = PoolPrint(L"%s %s", entry->options, new); 1094 | FreePool(entry->options); 1095 | entry->options = s; 1096 | } else { 1097 | entry->options = new; 1098 | new = NULL; 1099 | } 1100 | FreePool(new); 1101 | continue; 1102 | } 1103 | } 1104 | 1105 | if (entry->type == LOADER_UNDEFINED) { 1106 | config_entry_free(entry); 1107 | FreePool(initrd); 1108 | FreePool(entry); 1109 | return; 1110 | } 1111 | 1112 | /* add initrd= to options */ 1113 | if (entry->type == LOADER_LINUX && initrd) { 1114 | if (entry->options) { 1115 | CHAR16 *s; 1116 | 1117 | s = PoolPrint(L"%s %s", initrd, entry->options); 1118 | FreePool(entry->options); 1119 | entry->options = s; 1120 | } else { 1121 | entry->options = initrd; 1122 | initrd = NULL; 1123 | } 1124 | } 1125 | FreePool(initrd); 1126 | 1127 | entry->device = device; 1128 | entry->file = StrDuplicate(file); 1129 | len = StrLen(entry->file); 1130 | /* remove ".conf" */ 1131 | if (len > 5) 1132 | entry->file[len - 5] = '\0'; 1133 | StrLwr(entry->file); 1134 | 1135 | config_add_entry(config, entry); 1136 | } 1137 | 1138 | static VOID config_load_defaults(Config *config, EFI_FILE *root_dir) { 1139 | CHAR8 *content = NULL; 1140 | UINTN sec; 1141 | UINTN len; 1142 | EFI_STATUS err; 1143 | 1144 | len = file_read(root_dir, L"\\loader\\loader.conf", 0, 0, &content); 1145 | if (len > 0) 1146 | config_defaults_load_from_file(config, content); 1147 | FreePool(content); 1148 | 1149 | err = efivar_get_int(L"LoaderConfigTimeout", &sec); 1150 | if (!EFI_ERROR(err)) { 1151 | config->timeout_sec_efivar = sec; 1152 | config->timeout_sec = sec; 1153 | } else 1154 | config->timeout_sec_efivar = -1; 1155 | } 1156 | 1157 | static VOID config_load_entries(Config *config, EFI_HANDLE *device, EFI_FILE *root_dir, CHAR16 *loaded_image_path) { 1158 | EFI_FILE_HANDLE entries_dir; 1159 | EFI_STATUS err; 1160 | 1161 | err = uefi_call_wrapper(root_dir->Open, 5, root_dir, &entries_dir, L"\\loader\\entries", EFI_FILE_MODE_READ, 0ULL); 1162 | if (!EFI_ERROR(err)) { 1163 | for (;;) { 1164 | CHAR16 buf[256]; 1165 | UINTN bufsize; 1166 | EFI_FILE_INFO *f; 1167 | CHAR8 *content = NULL; 1168 | UINTN len; 1169 | 1170 | bufsize = sizeof(buf); 1171 | err = uefi_call_wrapper(entries_dir->Read, 3, entries_dir, &bufsize, buf); 1172 | if (bufsize == 0 || EFI_ERROR(err)) 1173 | break; 1174 | 1175 | f = (EFI_FILE_INFO *) buf; 1176 | if (f->FileName[0] == '.') 1177 | continue; 1178 | if (f->Attribute & EFI_FILE_DIRECTORY) 1179 | continue; 1180 | 1181 | len = StrLen(f->FileName); 1182 | if (len < 6) 1183 | continue; 1184 | if (StriCmp(f->FileName + len - 5, L".conf") != 0) 1185 | continue; 1186 | if (StrnCmp(f->FileName, L"auto-", 5) == 0) 1187 | continue; 1188 | 1189 | len = file_read(entries_dir, f->FileName, 0, 0, &content); 1190 | if (len > 0) 1191 | config_entry_add_from_file(config, device, f->FileName, content, loaded_image_path); 1192 | FreePool(content); 1193 | } 1194 | uefi_call_wrapper(entries_dir->Close, 1, entries_dir); 1195 | } 1196 | } 1197 | 1198 | static VOID config_sort_entries(Config *config) { 1199 | UINTN i; 1200 | 1201 | for (i = 1; i < config->entry_count; i++) { 1202 | BOOLEAN more; 1203 | UINTN k; 1204 | 1205 | more = FALSE; 1206 | for (k = 0; k < config->entry_count - i; k++) { 1207 | ConfigEntry *entry; 1208 | 1209 | if (str_verscmp(config->entries[k]->file, config->entries[k+1]->file) <= 0) 1210 | continue; 1211 | entry = config->entries[k]; 1212 | config->entries[k] = config->entries[k+1]; 1213 | config->entries[k+1] = entry; 1214 | more = TRUE; 1215 | } 1216 | if (!more) 1217 | break; 1218 | } 1219 | } 1220 | 1221 | static VOID config_default_entry_select(Config *config) { 1222 | CHAR16 *var; 1223 | EFI_STATUS err; 1224 | UINTN i; 1225 | 1226 | /* 1227 | * The EFI variable to specify a boot entry for the next, and only the 1228 | * next reboot. The variable is always cleared directly after it is read. 1229 | */ 1230 | err = efivar_get(L"LoaderEntryOneShot", &var); 1231 | if (!EFI_ERROR(err)) { 1232 | BOOLEAN found = FALSE; 1233 | 1234 | for (i = 0; i < config->entry_count; i++) { 1235 | if (StrCmp(config->entries[i]->file, var) == 0) { 1236 | config->idx_default = i; 1237 | found = TRUE; 1238 | break; 1239 | } 1240 | } 1241 | 1242 | config->entry_oneshot = StrDuplicate(var); 1243 | efivar_set(L"LoaderEntryOneShot", NULL, TRUE); 1244 | FreePool(var); 1245 | if (found) 1246 | return; 1247 | } 1248 | 1249 | /* 1250 | * The EFI variable to select the default boot entry overrides the 1251 | * configured pattern. The variable can be set and cleared by pressing 1252 | * the 'd' key in the loader selection menu, the entry is marked with 1253 | * an '*'. 1254 | */ 1255 | err = efivar_get(L"LoaderEntryDefault", &var); 1256 | if (!EFI_ERROR(err)) { 1257 | BOOLEAN found = FALSE; 1258 | 1259 | for (i = 0; i < config->entry_count; i++) { 1260 | if (StrCmp(config->entries[i]->file, var) == 0) { 1261 | config->idx_default = i; 1262 | config->idx_default_efivar = i; 1263 | found = TRUE; 1264 | break; 1265 | } 1266 | } 1267 | FreePool(var); 1268 | if (found) 1269 | return; 1270 | } 1271 | config->idx_default_efivar = -1; 1272 | 1273 | if (config->entry_count == 0) 1274 | return; 1275 | 1276 | /* 1277 | * Match the pattern from the end of the list to the start, find last 1278 | * entry (largest number) matching the given pattern. 1279 | */ 1280 | if (config->entry_default_pattern) { 1281 | i = config->entry_count; 1282 | while (i--) { 1283 | if (config->entries[i]->no_autoselect) 1284 | continue; 1285 | if (MetaiMatch(config->entries[i]->file, config->entry_default_pattern)) { 1286 | config->idx_default = i; 1287 | return; 1288 | } 1289 | } 1290 | } 1291 | 1292 | /* select the last suitable entry */ 1293 | i = config->entry_count; 1294 | while (i--) { 1295 | if (config->entries[i]->no_autoselect) 1296 | continue; 1297 | config->idx_default = i; 1298 | return; 1299 | } 1300 | 1301 | /* no entry found */ 1302 | config->idx_default = -1; 1303 | } 1304 | 1305 | /* generate a unique title, avoiding non-distinguishable menu entries */ 1306 | static VOID config_title_generate(Config *config) { 1307 | UINTN i, k; 1308 | BOOLEAN unique; 1309 | 1310 | /* set title */ 1311 | for (i = 0; i < config->entry_count; i++) { 1312 | CHAR16 *title; 1313 | 1314 | FreePool(config->entries[i]->title_show); 1315 | title = config->entries[i]->title; 1316 | if (!title) 1317 | title = config->entries[i]->file; 1318 | config->entries[i]->title_show = StrDuplicate(title); 1319 | } 1320 | 1321 | unique = TRUE; 1322 | for (i = 0; i < config->entry_count; i++) { 1323 | for (k = 0; k < config->entry_count; k++) { 1324 | if (i == k) 1325 | continue; 1326 | if (StrCmp(config->entries[i]->title_show, config->entries[k]->title_show) != 0) 1327 | continue; 1328 | 1329 | unique = FALSE; 1330 | config->entries[i]->non_unique = TRUE; 1331 | config->entries[k]->non_unique = TRUE; 1332 | } 1333 | } 1334 | if (unique) 1335 | return; 1336 | 1337 | /* add version to non-unique titles */ 1338 | for (i = 0; i < config->entry_count; i++) { 1339 | CHAR16 *s; 1340 | 1341 | if (!config->entries[i]->non_unique) 1342 | continue; 1343 | if (!config->entries[i]->version) 1344 | continue; 1345 | 1346 | s = PoolPrint(L"%s (%s)", config->entries[i]->title_show, config->entries[i]->version); 1347 | FreePool(config->entries[i]->title_show); 1348 | config->entries[i]->title_show = s; 1349 | config->entries[i]->non_unique = FALSE; 1350 | } 1351 | 1352 | unique = TRUE; 1353 | for (i = 0; i < config->entry_count; i++) { 1354 | for (k = 0; k < config->entry_count; k++) { 1355 | if (i == k) 1356 | continue; 1357 | if (StrCmp(config->entries[i]->title_show, config->entries[k]->title_show) != 0) 1358 | continue; 1359 | 1360 | unique = FALSE; 1361 | config->entries[i]->non_unique = TRUE; 1362 | config->entries[k]->non_unique = TRUE; 1363 | } 1364 | } 1365 | if (unique) 1366 | return; 1367 | 1368 | /* add machine-id to non-unique titles */ 1369 | for (i = 0; i < config->entry_count; i++) { 1370 | CHAR16 *s; 1371 | CHAR16 *m; 1372 | 1373 | if (!config->entries[i]->non_unique) 1374 | continue; 1375 | if (!config->entries[i]->machine_id) 1376 | continue; 1377 | 1378 | m = StrDuplicate(config->entries[i]->machine_id); 1379 | m[8] = '\0'; 1380 | s = PoolPrint(L"%s (%s)", config->entries[i]->title_show, m); 1381 | FreePool(config->entries[i]->title_show); 1382 | config->entries[i]->title_show = s; 1383 | config->entries[i]->non_unique = FALSE; 1384 | FreePool(m); 1385 | } 1386 | 1387 | unique = TRUE; 1388 | for (i = 0; i < config->entry_count; i++) { 1389 | for (k = 0; k < config->entry_count; k++) { 1390 | if (i == k) 1391 | continue; 1392 | if (StrCmp(config->entries[i]->title_show, config->entries[k]->title_show) != 0) 1393 | continue; 1394 | 1395 | unique = FALSE; 1396 | config->entries[i]->non_unique = TRUE; 1397 | config->entries[k]->non_unique = TRUE; 1398 | } 1399 | } 1400 | if (unique) 1401 | return; 1402 | 1403 | /* add file name to non-unique titles */ 1404 | for (i = 0; i < config->entry_count; i++) { 1405 | CHAR16 *s; 1406 | 1407 | if (!config->entries[i]->non_unique) 1408 | continue; 1409 | s = PoolPrint(L"%s (%s)", config->entries[i]->title_show, config->entries[i]->file); 1410 | FreePool(config->entries[i]->title_show); 1411 | config->entries[i]->title_show = s; 1412 | config->entries[i]->non_unique = FALSE; 1413 | } 1414 | } 1415 | 1416 | static BOOLEAN config_entry_add_call(Config *config, CHAR16 *title, EFI_STATUS (*call)(VOID)) { 1417 | ConfigEntry *entry; 1418 | 1419 | entry = AllocateZeroPool(sizeof(ConfigEntry)); 1420 | entry->title = StrDuplicate(title); 1421 | entry->call = call; 1422 | entry->no_autoselect = TRUE; 1423 | config_add_entry(config, entry); 1424 | return TRUE; 1425 | } 1426 | 1427 | static ConfigEntry *config_entry_add_loader(Config *config, EFI_HANDLE *device, 1428 | enum loader_type type,CHAR16 *file, CHAR16 key, CHAR16 *title, CHAR16 *loader) { 1429 | ConfigEntry *entry; 1430 | 1431 | entry = AllocateZeroPool(sizeof(ConfigEntry)); 1432 | entry->type = type; 1433 | entry->title = StrDuplicate(title); 1434 | entry->device = device; 1435 | entry->loader = StrDuplicate(loader); 1436 | entry->file = StrDuplicate(file); 1437 | StrLwr(entry->file); 1438 | entry->key = key; 1439 | config_add_entry(config, entry); 1440 | 1441 | return entry; 1442 | } 1443 | 1444 | static BOOLEAN config_entry_add_loader_auto(Config *config, EFI_HANDLE *device, EFI_FILE *root_dir, CHAR16 *loaded_image_path, 1445 | CHAR16 *file, CHAR16 key, CHAR16 *title, CHAR16 *loader) { 1446 | EFI_FILE_HANDLE handle; 1447 | ConfigEntry *entry; 1448 | EFI_STATUS err; 1449 | 1450 | /* do not add an entry for ourselves */ 1451 | if (loaded_image_path && StriCmp(loader, loaded_image_path) == 0) 1452 | return FALSE; 1453 | 1454 | /* check existence */ 1455 | err = uefi_call_wrapper(root_dir->Open, 5, root_dir, &handle, loader, EFI_FILE_MODE_READ, 0ULL); 1456 | if (EFI_ERROR(err)) 1457 | return FALSE; 1458 | uefi_call_wrapper(handle->Close, 1, handle); 1459 | 1460 | entry = config_entry_add_loader(config, device, LOADER_UNDEFINED, file, key, title, loader); 1461 | if (!entry) 1462 | return FALSE; 1463 | 1464 | /* do not boot right away into auto-detected entries */ 1465 | entry->no_autoselect = TRUE; 1466 | 1467 | return TRUE; 1468 | } 1469 | 1470 | static VOID config_entry_add_osx(Config *config) { 1471 | EFI_STATUS err; 1472 | UINTN handle_count = 0; 1473 | EFI_HANDLE *handles = NULL; 1474 | 1475 | err = LibLocateHandle(ByProtocol, &FileSystemProtocol, NULL, &handle_count, &handles); 1476 | if (!EFI_ERROR(err)) { 1477 | UINTN i; 1478 | 1479 | for (i = 0; i < handle_count; i++) { 1480 | EFI_FILE *root; 1481 | BOOLEAN found; 1482 | 1483 | root = LibOpenRoot(handles[i]); 1484 | if (!root) 1485 | continue; 1486 | found = config_entry_add_loader_auto(config, handles[i], root, NULL, L"auto-osx", 'a', L"OS X", 1487 | L"\\System\\Library\\CoreServices\\boot.efi"); 1488 | uefi_call_wrapper(root->Close, 1, root); 1489 | if (found) 1490 | break; 1491 | } 1492 | 1493 | FreePool(handles); 1494 | } 1495 | } 1496 | 1497 | static VOID config_entry_add_linux( Config *config, EFI_LOADED_IMAGE *loaded_image, EFI_FILE *root_dir) { 1498 | EFI_FILE_HANDLE linux_dir; 1499 | EFI_STATUS err; 1500 | ConfigEntry *entry; 1501 | 1502 | err = uefi_call_wrapper(root_dir->Open, 5, root_dir, &linux_dir, L"\\EFI\\Linux", EFI_FILE_MODE_READ, 0ULL); 1503 | if (!EFI_ERROR(err)) { 1504 | for (;;) { 1505 | CHAR16 buf[256]; 1506 | UINTN bufsize; 1507 | EFI_FILE_INFO *f; 1508 | CHAR8 *sections[] = { 1509 | (UINT8 *)".osrel", 1510 | (UINT8 *)".cmdline", 1511 | NULL 1512 | }; 1513 | UINTN offs[ELEMENTSOF(sections)-1] = {}; 1514 | UINTN szs[ELEMENTSOF(sections)-1] = {}; 1515 | UINTN addrs[ELEMENTSOF(sections)-1] = {}; 1516 | CHAR8 *content = NULL; 1517 | UINTN len; 1518 | CHAR8 *line; 1519 | UINTN pos = 0; 1520 | CHAR8 *key, *value; 1521 | CHAR16 *os_name = NULL; 1522 | CHAR16 *os_id = NULL; 1523 | CHAR16 *os_version = NULL; 1524 | CHAR16 *os_build = NULL; 1525 | 1526 | bufsize = sizeof(buf); 1527 | err = uefi_call_wrapper(linux_dir->Read, 3, linux_dir, &bufsize, buf); 1528 | if (bufsize == 0 || EFI_ERROR(err)) 1529 | break; 1530 | 1531 | f = (EFI_FILE_INFO *) buf; 1532 | if (f->FileName[0] == '.') 1533 | continue; 1534 | if (f->Attribute & EFI_FILE_DIRECTORY) 1535 | continue; 1536 | len = StrLen(f->FileName); 1537 | if (len < 5) 1538 | continue; 1539 | if (StriCmp(f->FileName + len - 4, L".efi") != 0) 1540 | continue; 1541 | 1542 | /* look for .osrel and .cmdline sections in the .efi binary */ 1543 | err = pefile_locate_sections(linux_dir, f->FileName, sections, addrs, offs, szs); 1544 | if (EFI_ERROR(err)) 1545 | continue; 1546 | 1547 | len = file_read(linux_dir, f->FileName, offs[0], szs[0], &content); 1548 | if (len <= 0) 1549 | continue; 1550 | 1551 | /* read properties from the embedded os-release file */ 1552 | line = content; 1553 | while ((line = line_get_key_value(content, (CHAR8 *)"=", &pos, &key, &value))) { 1554 | if (strcmpa((CHAR8 *)"PRETTY_NAME", key) == 0) { 1555 | FreePool(os_name); 1556 | os_name = stra_to_str(value); 1557 | continue; 1558 | } 1559 | 1560 | if (strcmpa((CHAR8 *)"ID", key) == 0) { 1561 | FreePool(os_id); 1562 | os_id = stra_to_str(value); 1563 | continue; 1564 | } 1565 | 1566 | if (strcmpa((CHAR8 *)"VERSION_ID", key) == 0) { 1567 | FreePool(os_version); 1568 | os_version = stra_to_str(value); 1569 | continue; 1570 | } 1571 | 1572 | if (strcmpa((CHAR8 *)"BUILD_ID", key) == 0) { 1573 | FreePool(os_build); 1574 | os_build = stra_to_str(value); 1575 | continue; 1576 | } 1577 | } 1578 | 1579 | if (os_name && os_id && (os_version || os_build)) { 1580 | CHAR16 *conf; 1581 | CHAR16 *path; 1582 | CHAR16 *cmdline; 1583 | 1584 | conf = PoolPrint(L"%s-%s", os_id, os_version ? : os_build); 1585 | path = PoolPrint(L"\\EFI\\Linux\\%s", f->FileName); 1586 | entry = config_entry_add_loader(config, loaded_image->DeviceHandle, LOADER_LINUX, conf, 'l', os_name, path); 1587 | 1588 | FreePool(content); 1589 | /* read the embedded cmdline file */ 1590 | len = file_read(linux_dir, f->FileName, offs[1], szs[1] - 1 , &content); 1591 | if (len > 0) { 1592 | cmdline = stra_to_str(content); 1593 | entry->options = cmdline; 1594 | cmdline = NULL; 1595 | } 1596 | FreePool(cmdline); 1597 | FreePool(conf); 1598 | FreePool(path); 1599 | } 1600 | 1601 | FreePool(os_name); 1602 | FreePool(os_id); 1603 | FreePool(os_version); 1604 | FreePool(os_build); 1605 | FreePool(content); 1606 | } 1607 | uefi_call_wrapper(linux_dir->Close, 1, linux_dir); 1608 | } 1609 | } 1610 | 1611 | static EFI_STATUS image_start(EFI_HANDLE parent_image, const Config *config, const ConfigEntry *entry) { 1612 | EFI_HANDLE image; 1613 | EFI_DEVICE_PATH *path; 1614 | CHAR16 *options; 1615 | EFI_STATUS err; 1616 | 1617 | path = FileDevicePath(entry->device, entry->loader); 1618 | if (!path) { 1619 | Print(L"Error getting device path."); 1620 | uefi_call_wrapper(BS->Stall, 1, 3 * 1000 * 1000); 1621 | return EFI_INVALID_PARAMETER; 1622 | } 1623 | 1624 | err = uefi_call_wrapper(BS->LoadImage, 6, FALSE, parent_image, path, NULL, 0, &image); 1625 | if (EFI_ERROR(err)) { 1626 | Print(L"Error loading %s: %r", entry->loader, err); 1627 | uefi_call_wrapper(BS->Stall, 1, 3 * 1000 * 1000); 1628 | goto out; 1629 | } 1630 | 1631 | if (config->options_edit) 1632 | options = config->options_edit; 1633 | else if (entry->options) 1634 | options = entry->options; 1635 | else 1636 | options = NULL; 1637 | if (options) { 1638 | EFI_LOADED_IMAGE *loaded_image; 1639 | 1640 | err = uefi_call_wrapper(BS->OpenProtocol, 6, image, &LoadedImageProtocol, (VOID **)&loaded_image, 1641 | parent_image, NULL, EFI_OPEN_PROTOCOL_GET_PROTOCOL); 1642 | if (EFI_ERROR(err)) { 1643 | Print(L"Error getting LoadedImageProtocol handle: %r", err); 1644 | uefi_call_wrapper(BS->Stall, 1, 3 * 1000 * 1000); 1645 | goto out_unload; 1646 | } 1647 | loaded_image->LoadOptions = options; 1648 | loaded_image->LoadOptionsSize = (StrLen(loaded_image->LoadOptions)+1) * sizeof(CHAR16); 1649 | } 1650 | 1651 | efivar_set_time_usec(L"LoaderTimeExecUSec", 0); 1652 | err = uefi_call_wrapper(BS->StartImage, 3, image, NULL, NULL); 1653 | out_unload: 1654 | uefi_call_wrapper(BS->UnloadImage, 1, image); 1655 | out: 1656 | FreePool(path); 1657 | return err; 1658 | } 1659 | 1660 | static EFI_STATUS reboot_into_firmware(VOID) { 1661 | CHAR8 *b; 1662 | UINTN size; 1663 | UINT64 osind; 1664 | EFI_STATUS err; 1665 | 1666 | osind = EFI_OS_INDICATIONS_BOOT_TO_FW_UI; 1667 | 1668 | err = efivar_get_raw(&global_guid, L"OsIndications", &b, &size); 1669 | if (!EFI_ERROR(err)) 1670 | osind |= (UINT64)*b; 1671 | FreePool(b); 1672 | 1673 | err = efivar_set_raw(&global_guid, L"OsIndications", (CHAR8 *)&osind, sizeof(UINT64), TRUE); 1674 | if (EFI_ERROR(err)) 1675 | return err; 1676 | 1677 | err = uefi_call_wrapper(RT->ResetSystem, 4, EfiResetCold, EFI_SUCCESS, 0, NULL); 1678 | Print(L"Error calling ResetSystem: %r", err); 1679 | uefi_call_wrapper(BS->Stall, 1, 3 * 1000 * 1000); 1680 | return err; 1681 | } 1682 | 1683 | static VOID config_free(Config *config) { 1684 | UINTN i; 1685 | 1686 | for (i = 0; i < config->entry_count; i++) 1687 | config_entry_free(config->entries[i]); 1688 | FreePool(config->entries); 1689 | FreePool(config->entry_default_pattern); 1690 | FreePool(config->options_edit); 1691 | FreePool(config->entry_oneshot); 1692 | } 1693 | 1694 | EFI_STATUS efi_main(EFI_HANDLE image, EFI_SYSTEM_TABLE *sys_table) { 1695 | CHAR16 *s; 1696 | CHAR8 *b; 1697 | UINTN size; 1698 | EFI_LOADED_IMAGE *loaded_image; 1699 | EFI_FILE *root_dir; 1700 | CHAR16 *loaded_image_path; 1701 | EFI_STATUS err; 1702 | Config config; 1703 | UINT64 init_usec; 1704 | BOOLEAN menu = FALSE; 1705 | CHAR16 uuid[37]; 1706 | 1707 | InitializeLib(image, sys_table); 1708 | init_usec = time_usec(); 1709 | efivar_set_time_usec(L"LoaderTimeInitUSec", init_usec); 1710 | efivar_set(L"LoaderInfo", L"systemd-boot " VERSION, FALSE); 1711 | s = PoolPrint(L"%s %d.%02d", ST->FirmwareVendor, ST->FirmwareRevision >> 16, ST->FirmwareRevision & 0xffff); 1712 | efivar_set(L"LoaderFirmwareInfo", s, FALSE); 1713 | FreePool(s); 1714 | s = PoolPrint(L"UEFI %d.%02d", ST->Hdr.Revision >> 16, ST->Hdr.Revision & 0xffff); 1715 | efivar_set(L"LoaderFirmwareType", s, FALSE); 1716 | FreePool(s); 1717 | 1718 | err = uefi_call_wrapper(BS->OpenProtocol, 6, image, &LoadedImageProtocol, (VOID **)&loaded_image, 1719 | image, NULL, EFI_OPEN_PROTOCOL_GET_PROTOCOL); 1720 | if (EFI_ERROR(err)) { 1721 | Print(L"Error getting a LoadedImageProtocol handle: %r ", err); 1722 | uefi_call_wrapper(BS->Stall, 1, 3 * 1000 * 1000); 1723 | return err; 1724 | } 1725 | 1726 | /* export the device path this image is started from */ 1727 | if (disk_get_part_uuid(loaded_image->DeviceHandle, uuid) == EFI_SUCCESS) 1728 | efivar_set(L"LoaderDevicePartUUID", uuid, FALSE); 1729 | 1730 | root_dir = LibOpenRoot(loaded_image->DeviceHandle); 1731 | if (!root_dir) { 1732 | Print(L"Unable to open root directory: %r ", err); 1733 | uefi_call_wrapper(BS->Stall, 1, 3 * 1000 * 1000); 1734 | return EFI_LOAD_ERROR; 1735 | } 1736 | 1737 | 1738 | /* the filesystem path to this image, to prevent adding ourselves to the menu */ 1739 | loaded_image_path = DevicePathToStr(loaded_image->FilePath); 1740 | efivar_set(L"LoaderImageIdentifier", loaded_image_path, FALSE); 1741 | 1742 | ZeroMem(&config, sizeof(Config)); 1743 | config_load_defaults(&config, root_dir); 1744 | 1745 | /* scan /EFI/Linux/ directory */ 1746 | config_entry_add_linux(&config, loaded_image, root_dir); 1747 | 1748 | /* scan /loader/entries/\*.conf files */ 1749 | config_load_entries(&config, loaded_image->DeviceHandle, root_dir, loaded_image_path); 1750 | 1751 | /* sort entries after version number */ 1752 | config_sort_entries(&config); 1753 | 1754 | /* if we find some well-known loaders, add them to the end of the list */ 1755 | config_entry_add_loader_auto(&config, loaded_image->DeviceHandle, root_dir, loaded_image_path, 1756 | L"auto-windows", 'w', L"Windows Boot Manager", L"\\EFI\\Microsoft\\Boot\\bootmgfw.efi"); 1757 | config_entry_add_loader_auto(&config, loaded_image->DeviceHandle, root_dir, loaded_image_path, 1758 | L"auto-efi-shell", 's', L"EFI Shell", L"\\shell" EFI_MACHINE_TYPE_NAME ".efi"); 1759 | config_entry_add_loader_auto(&config, loaded_image->DeviceHandle, root_dir, loaded_image_path, 1760 | L"auto-efi-default", '\0', L"EFI Default Loader", L"\\EFI\\Boot\\boot" EFI_MACHINE_TYPE_NAME ".efi"); 1761 | config_entry_add_osx(&config); 1762 | 1763 | if (efivar_get_raw(&global_guid, L"OsIndicationsSupported", &b, &size) == EFI_SUCCESS) { 1764 | UINT64 osind = (UINT64)*b; 1765 | 1766 | if (osind & EFI_OS_INDICATIONS_BOOT_TO_FW_UI) 1767 | config_entry_add_call(&config, L"Reboot Into Firmware Interface", reboot_into_firmware); 1768 | FreePool(b); 1769 | } 1770 | 1771 | if (config.entry_count == 0) { 1772 | Print(L"No loader found. Configuration files in \\loader\\entries\\*.conf are needed."); 1773 | uefi_call_wrapper(BS->Stall, 1, 3 * 1000 * 1000); 1774 | goto out; 1775 | } 1776 | 1777 | config_title_generate(&config); 1778 | 1779 | /* select entry by configured pattern or EFI LoaderDefaultEntry= variable*/ 1780 | config_default_entry_select(&config); 1781 | 1782 | /* if no configured entry to select from was found, enable the menu */ 1783 | if (config.idx_default == -1) { 1784 | config.idx_default = 0; 1785 | if (config.timeout_sec == 0) 1786 | config.timeout_sec = 10; 1787 | } 1788 | 1789 | /* select entry or show menu when key is pressed or timeout is set */ 1790 | if (config.timeout_sec == 0) { 1791 | UINT64 key; 1792 | 1793 | err = console_key_read(&key, FALSE); 1794 | if (!EFI_ERROR(err)) { 1795 | INT16 idx; 1796 | 1797 | /* find matching key in config entries */ 1798 | idx = entry_lookup_key(&config, config.idx_default, KEYCHAR(key)); 1799 | if (idx >= 0) 1800 | config.idx_default = idx; 1801 | else 1802 | menu = TRUE; 1803 | } 1804 | } else 1805 | menu = TRUE; 1806 | 1807 | for (;;) { 1808 | ConfigEntry *entry; 1809 | 1810 | entry = config.entries[config.idx_default]; 1811 | if (menu) { 1812 | efivar_set_time_usec(L"LoaderTimeMenuUSec", 0); 1813 | uefi_call_wrapper(BS->SetWatchdogTimer, 4, 0, 0x10000, 0, NULL); 1814 | if (!menu_run(&config, &entry, loaded_image_path)) 1815 | break; 1816 | 1817 | /* run special entry like "reboot" */ 1818 | if (entry->call) { 1819 | entry->call(); 1820 | continue; 1821 | } 1822 | } 1823 | 1824 | /* export the selected boot entry to the system */ 1825 | efivar_set(L"LoaderEntrySelected", entry->file, FALSE); 1826 | 1827 | uefi_call_wrapper(BS->SetWatchdogTimer, 4, 5 * 60, 0x10000, 0, NULL); 1828 | err = image_start(image, &config, entry); 1829 | if (EFI_ERROR(err)) { 1830 | graphics_mode(FALSE); 1831 | Print(L"\nFailed to execute %s (%s): %r\n", entry->title, entry->loader, err); 1832 | uefi_call_wrapper(BS->Stall, 1, 3 * 1000 * 1000); 1833 | goto out; 1834 | } 1835 | 1836 | menu = TRUE; 1837 | config.timeout_sec = 0; 1838 | } 1839 | err = EFI_SUCCESS; 1840 | out: 1841 | FreePool(loaded_image_path); 1842 | config_free(&config); 1843 | uefi_call_wrapper(root_dir->Close, 1, root_dir); 1844 | uefi_call_wrapper(BS->CloseProtocol, 4, image, &LoadedImageProtocol, image, NULL); 1845 | return err; 1846 | } 1847 | -------------------------------------------------------------------------------- /src/sd-boot/console.c: -------------------------------------------------------------------------------- 1 | /*-*- Mode: C; c-basic-offset: 8; indent-tabs-mode: nil -*-*/ 2 | 3 | /* 4 | * This program is free software; you can redistribute it and/or modify it 5 | * under the terms of the GNU Lesser General Public License as published by 6 | * the Free Software Foundation; either version 2.1 of the License, or 7 | * (at your option) any later version. 8 | * 9 | * This program is distributed in the hope that it will be useful, but 10 | * WITHOUT ANY WARRANTY; without even the implied warranty of 11 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU 12 | * Lesser General Public License for more details. 13 | * 14 | * Copyright (C) 2012-2013 Kay Sievers 15 | * Copyright (C) 2012 Harald Hoyer 16 | */ 17 | 18 | #include 19 | #include 20 | 21 | #include "console.h" 22 | #include "util.h" 23 | 24 | #define EFI_SIMPLE_TEXT_INPUT_EX_PROTOCOL_GUID \ 25 | { 0xdd9e7534, 0x7762, 0x4698, { 0x8c, 0x14, 0xf5, 0x85, 0x17, 0xa6, 0x25, 0xaa } } 26 | 27 | struct _EFI_SIMPLE_TEXT_INPUT_EX_PROTOCOL; 28 | 29 | typedef EFI_STATUS (EFIAPI *EFI_INPUT_RESET_EX)( 30 | struct _EFI_SIMPLE_TEXT_INPUT_EX_PROTOCOL *This, 31 | BOOLEAN ExtendedVerification 32 | ); 33 | 34 | typedef UINT8 EFI_KEY_TOGGLE_STATE; 35 | 36 | typedef struct { 37 | UINT32 KeyShiftState; 38 | EFI_KEY_TOGGLE_STATE KeyToggleState; 39 | } EFI_KEY_STATE; 40 | 41 | typedef struct { 42 | EFI_INPUT_KEY Key; 43 | EFI_KEY_STATE KeyState; 44 | } EFI_KEY_DATA; 45 | 46 | typedef EFI_STATUS (EFIAPI *EFI_INPUT_READ_KEY_EX)( 47 | struct _EFI_SIMPLE_TEXT_INPUT_EX_PROTOCOL *This, 48 | EFI_KEY_DATA *KeyData 49 | ); 50 | 51 | typedef EFI_STATUS (EFIAPI *EFI_SET_STATE)( 52 | struct _EFI_SIMPLE_TEXT_INPUT_EX_PROTOCOL *This, 53 | EFI_KEY_TOGGLE_STATE *KeyToggleState 54 | ); 55 | 56 | typedef EFI_STATUS (EFIAPI *EFI_KEY_NOTIFY_FUNCTION)( 57 | EFI_KEY_DATA *KeyData 58 | ); 59 | 60 | typedef EFI_STATUS (EFIAPI *EFI_REGISTER_KEYSTROKE_NOTIFY)( 61 | struct _EFI_SIMPLE_TEXT_INPUT_EX_PROTOCOL *This, 62 | EFI_KEY_DATA KeyData, 63 | EFI_KEY_NOTIFY_FUNCTION KeyNotificationFunction, 64 | VOID **NotifyHandle 65 | ); 66 | 67 | typedef EFI_STATUS (EFIAPI *EFI_UNREGISTER_KEYSTROKE_NOTIFY)( 68 | struct _EFI_SIMPLE_TEXT_INPUT_EX_PROTOCOL *This, 69 | VOID *NotificationHandle 70 | ); 71 | 72 | typedef struct _EFI_SIMPLE_TEXT_INPUT_EX_PROTOCOL { 73 | EFI_INPUT_RESET_EX Reset; 74 | EFI_INPUT_READ_KEY_EX ReadKeyStrokeEx; 75 | EFI_EVENT WaitForKeyEx; 76 | EFI_SET_STATE SetState; 77 | EFI_REGISTER_KEYSTROKE_NOTIFY RegisterKeyNotify; 78 | EFI_UNREGISTER_KEYSTROKE_NOTIFY UnregisterKeyNotify; 79 | } EFI_SIMPLE_TEXT_INPUT_EX_PROTOCOL; 80 | 81 | EFI_STATUS console_key_read(UINT64 *key, BOOLEAN wait) { 82 | EFI_GUID EfiSimpleTextInputExProtocolGuid = EFI_SIMPLE_TEXT_INPUT_EX_PROTOCOL_GUID; 83 | static EFI_SIMPLE_TEXT_INPUT_EX_PROTOCOL *TextInputEx; 84 | static BOOLEAN checked; 85 | UINTN index; 86 | EFI_INPUT_KEY k; 87 | EFI_STATUS err; 88 | 89 | if (!checked) { 90 | err = LibLocateProtocol(&EfiSimpleTextInputExProtocolGuid, (VOID **)&TextInputEx); 91 | if (EFI_ERROR(err)) 92 | TextInputEx = NULL; 93 | 94 | checked = TRUE; 95 | } 96 | 97 | /* wait until key is pressed */ 98 | if (wait) { 99 | if (TextInputEx) 100 | uefi_call_wrapper(BS->WaitForEvent, 3, 1, &TextInputEx->WaitForKeyEx, &index); 101 | else 102 | uefi_call_wrapper(BS->WaitForEvent, 3, 1, &ST->ConIn->WaitForKey, &index); 103 | } 104 | 105 | if (TextInputEx) { 106 | EFI_KEY_DATA keydata; 107 | UINT64 keypress; 108 | 109 | err = uefi_call_wrapper(TextInputEx->ReadKeyStrokeEx, 2, TextInputEx, &keydata); 110 | if (!EFI_ERROR(err)) { 111 | UINT32 shift = 0; 112 | 113 | /* do not distinguish between left and right keys */ 114 | if (keydata.KeyState.KeyShiftState & EFI_SHIFT_STATE_VALID) { 115 | if (keydata.KeyState.KeyShiftState & (EFI_RIGHT_CONTROL_PRESSED|EFI_LEFT_CONTROL_PRESSED)) 116 | shift |= EFI_CONTROL_PRESSED; 117 | if (keydata.KeyState.KeyShiftState & (EFI_RIGHT_ALT_PRESSED|EFI_LEFT_ALT_PRESSED)) 118 | shift |= EFI_ALT_PRESSED; 119 | }; 120 | 121 | /* 32 bit modifier keys + 16 bit scan code + 16 bit unicode */ 122 | keypress = KEYPRESS(shift, keydata.Key.ScanCode, keydata.Key.UnicodeChar); 123 | if (keypress > 0) { 124 | *key = keypress; 125 | return 0; 126 | } 127 | } 128 | } 129 | 130 | /* fallback for firmware which does not support SimpleTextInputExProtocol 131 | * 132 | * This is also called in case ReadKeyStrokeEx did not return a key, because 133 | * some broken firmwares offer SimpleTextInputExProtocol, but never acually 134 | * handle any key. */ 135 | err = uefi_call_wrapper(ST->ConIn->ReadKeyStroke, 2, ST->ConIn, &k); 136 | if (EFI_ERROR(err)) 137 | return err; 138 | 139 | *key = KEYPRESS(0, k.ScanCode, k.UnicodeChar); 140 | return 0; 141 | } 142 | -------------------------------------------------------------------------------- /src/sd-boot/console.h: -------------------------------------------------------------------------------- 1 | /*-*- Mode: C; c-basic-offset: 8; indent-tabs-mode: nil -*-*/ 2 | 3 | /* 4 | * This program is free software; you can redistribute it and/or modify it 5 | * under the terms of the GNU Lesser General Public License as published by 6 | * the Free Software Foundation; either version 2.1 of the License, or 7 | * (at your option) any later version. 8 | * 9 | * This program is distributed in the hope that it will be useful, but 10 | * WITHOUT ANY WARRANTY; without even the implied warranty of 11 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU 12 | * Lesser General Public License for more details. 13 | * 14 | * Copyright (C) 2012-2013 Kay Sievers 15 | * Copyright (C) 2012 Harald Hoyer 16 | */ 17 | 18 | #ifndef __SDBOOT_CONSOLE_H 19 | #define __SDBOOT_CONSOLE_H 20 | 21 | #define EFI_SHIFT_STATE_VALID 0x80000000 22 | #define EFI_RIGHT_CONTROL_PRESSED 0x00000004 23 | #define EFI_LEFT_CONTROL_PRESSED 0x00000008 24 | #define EFI_RIGHT_ALT_PRESSED 0x00000010 25 | #define EFI_LEFT_ALT_PRESSED 0x00000020 26 | 27 | #define EFI_CONTROL_PRESSED (EFI_RIGHT_CONTROL_PRESSED|EFI_LEFT_CONTROL_PRESSED) 28 | #define EFI_ALT_PRESSED (EFI_RIGHT_ALT_PRESSED|EFI_LEFT_ALT_PRESSED) 29 | #define KEYPRESS(keys, scan, uni) ((((UINT64)keys) << 32) | ((scan) << 16) | (uni)) 30 | #define KEYCHAR(k) ((k) & 0xffff) 31 | #define CHAR_CTRL(c) ((c) - 'a' + 1) 32 | 33 | EFI_STATUS console_key_read(UINT64 *key, BOOLEAN wait); 34 | #endif 35 | -------------------------------------------------------------------------------- /src/sd-boot/disk.c: -------------------------------------------------------------------------------- 1 | /*-*- Mode: C; c-basic-offset: 8; indent-tabs-mode: nil -*-*/ 2 | 3 | /* 4 | * This program is free software; you can redistribute it and/or modify it 5 | * under the terms of the GNU Lesser General Public License as published by 6 | * the Free Software Foundation; either version 2.1 of the License, or 7 | * (at your option) any later version. 8 | * 9 | * This program is distributed in the hope that it will be useful, but 10 | * WITHOUT ANY WARRANTY; without even the implied warranty of 11 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU 12 | * Lesser General Public License for more details. 13 | * 14 | * Copyright (C) 2015 Kay Sievers 15 | */ 16 | 17 | #include 18 | #include 19 | 20 | #include "util.h" 21 | 22 | EFI_STATUS disk_get_part_uuid(EFI_HANDLE *handle, CHAR16 uuid[37]) { 23 | EFI_DEVICE_PATH *device_path; 24 | EFI_STATUS r = EFI_NOT_FOUND; 25 | 26 | /* export the device path this image is started from */ 27 | device_path = DevicePathFromHandle(handle); 28 | if (device_path) { 29 | EFI_DEVICE_PATH *path, *paths; 30 | 31 | paths = UnpackDevicePath(device_path); 32 | for (path = paths; !IsDevicePathEnd(path); path = NextDevicePathNode(path)) { 33 | HARDDRIVE_DEVICE_PATH *drive; 34 | 35 | if (DevicePathType(path) != MEDIA_DEVICE_PATH) 36 | continue; 37 | if (DevicePathSubType(path) != MEDIA_HARDDRIVE_DP) 38 | continue; 39 | drive = (HARDDRIVE_DEVICE_PATH *)path; 40 | if (drive->SignatureType != SIGNATURE_TYPE_GUID) 41 | continue; 42 | 43 | GuidToString(uuid, (EFI_GUID *)&drive->Signature); 44 | r = EFI_SUCCESS; 45 | break; 46 | } 47 | FreePool(paths); 48 | } 49 | 50 | return r; 51 | } 52 | -------------------------------------------------------------------------------- /src/sd-boot/disk.h: -------------------------------------------------------------------------------- 1 | /*-*- Mode: C; c-basic-offset: 8; indent-tabs-mode: nil -*-*/ 2 | 3 | /* 4 | * This program is free software; you can redistribute it and/or modify it 5 | * under the terms of the GNU Lesser General Public License as published by 6 | * the Free Software Foundation; either version 2.1 of the License, or 7 | * (at your option) any later version. 8 | * 9 | * This program is distributed in the hope that it will be useful, but 10 | * WITHOUT ANY WARRANTY; without even the implied warranty of 11 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU 12 | * Lesser General Public License for more details. 13 | * 14 | * Copyright (C) 2015 Kay Sievers 15 | */ 16 | 17 | #ifndef __SDBOOT_DISK_H 18 | #define __SDBOOT_DISK_H 19 | 20 | EFI_STATUS disk_get_part_uuid(EFI_HANDLE *handle, CHAR16 uuid[37]); 21 | #endif 22 | -------------------------------------------------------------------------------- /src/sd-boot/graphics.c: -------------------------------------------------------------------------------- 1 | /*-*- Mode: C; c-basic-offset: 8; indent-tabs-mode: nil -*-*/ 2 | 3 | /* 4 | * This program is free software; you can redistribute it and/or modify it 5 | * under the terms of the GNU Lesser General Public License as published by 6 | * the Free Software Foundation; either version 2.1 of the License, or 7 | * (at your option) any later version. 8 | * 9 | * This program is distributed in the hope that it will be useful, but 10 | * WITHOUT ANY WARRANTY; without even the implied warranty of 11 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU 12 | * Lesser General Public License for more details. 13 | * 14 | * Copyright (C) 2012-2013 Kay Sievers 15 | * Copyright (C) 2012 Harald Hoyer 16 | * Copyright (C) 2013 Intel Corporation 17 | * Authored by Joonas Lahtinen 18 | */ 19 | 20 | #include 21 | #include 22 | 23 | #include "graphics.h" 24 | #include "util.h" 25 | 26 | EFI_STATUS graphics_mode(BOOLEAN on) { 27 | #define EFI_CONSOLE_CONTROL_PROTOCOL_GUID \ 28 | { 0xf42f7782, 0x12e, 0x4c12, { 0x99, 0x56, 0x49, 0xf9, 0x43, 0x4, 0xf7, 0x21 } }; 29 | 30 | struct _EFI_CONSOLE_CONTROL_PROTOCOL; 31 | 32 | typedef enum { 33 | EfiConsoleControlScreenText, 34 | EfiConsoleControlScreenGraphics, 35 | EfiConsoleControlScreenMaxValue, 36 | } EFI_CONSOLE_CONTROL_SCREEN_MODE; 37 | 38 | typedef EFI_STATUS (EFIAPI *EFI_CONSOLE_CONTROL_PROTOCOL_GET_MODE)( 39 | struct _EFI_CONSOLE_CONTROL_PROTOCOL *This, 40 | EFI_CONSOLE_CONTROL_SCREEN_MODE *Mode, 41 | BOOLEAN *UgaExists, 42 | BOOLEAN *StdInLocked 43 | ); 44 | 45 | typedef EFI_STATUS (EFIAPI *EFI_CONSOLE_CONTROL_PROTOCOL_SET_MODE)( 46 | struct _EFI_CONSOLE_CONTROL_PROTOCOL *This, 47 | EFI_CONSOLE_CONTROL_SCREEN_MODE Mode 48 | ); 49 | 50 | typedef EFI_STATUS (EFIAPI *EFI_CONSOLE_CONTROL_PROTOCOL_LOCK_STD_IN)( 51 | struct _EFI_CONSOLE_CONTROL_PROTOCOL *This, 52 | CHAR16 *Password 53 | ); 54 | 55 | typedef struct _EFI_CONSOLE_CONTROL_PROTOCOL { 56 | EFI_CONSOLE_CONTROL_PROTOCOL_GET_MODE GetMode; 57 | EFI_CONSOLE_CONTROL_PROTOCOL_SET_MODE SetMode; 58 | EFI_CONSOLE_CONTROL_PROTOCOL_LOCK_STD_IN LockStdIn; 59 | } EFI_CONSOLE_CONTROL_PROTOCOL; 60 | 61 | EFI_GUID ConsoleControlProtocolGuid = EFI_CONSOLE_CONTROL_PROTOCOL_GUID; 62 | EFI_CONSOLE_CONTROL_PROTOCOL *ConsoleControl = NULL; 63 | EFI_CONSOLE_CONTROL_SCREEN_MODE new; 64 | EFI_CONSOLE_CONTROL_SCREEN_MODE current; 65 | BOOLEAN uga_exists; 66 | BOOLEAN stdin_locked; 67 | EFI_STATUS err; 68 | 69 | err = LibLocateProtocol(&ConsoleControlProtocolGuid, (VOID **)&ConsoleControl); 70 | if (EFI_ERROR(err)) 71 | /* console control protocol is nonstandard and might not exist. */ 72 | return err == EFI_NOT_FOUND ? EFI_SUCCESS : err; 73 | 74 | /* check current mode */ 75 | err = uefi_call_wrapper(ConsoleControl->GetMode, 4, ConsoleControl, ¤t, &uga_exists, &stdin_locked); 76 | if (EFI_ERROR(err)) 77 | return err; 78 | 79 | /* do not touch the mode */ 80 | new = on ? EfiConsoleControlScreenGraphics : EfiConsoleControlScreenText; 81 | if (new == current) 82 | return EFI_SUCCESS; 83 | 84 | err = uefi_call_wrapper(ConsoleControl->SetMode, 2, ConsoleControl, new); 85 | 86 | /* some firmware enables the cursor when switching modes */ 87 | uefi_call_wrapper(ST->ConOut->EnableCursor, 2, ST->ConOut, FALSE); 88 | 89 | return err; 90 | } 91 | -------------------------------------------------------------------------------- /src/sd-boot/graphics.h: -------------------------------------------------------------------------------- 1 | /*-*- Mode: C; c-basic-offset: 8; indent-tabs-mode: nil -*-*/ 2 | 3 | /* 4 | * This program is free software; you can redistribute it and/or modify it 5 | * under the terms of the GNU Lesser General Public License as published by 6 | * the Free Software Foundation; either version 2.1 of the License, or 7 | * (at your option) any later version. 8 | * 9 | * This program is distributed in the hope that it will be useful, but 10 | * WITHOUT ANY WARRANTY; without even the implied warranty of 11 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU 12 | * Lesser General Public License for more details. 13 | * 14 | * Copyright (C) 2012-2013 Kay Sievers 15 | * Copyright (C) 2012 Harald Hoyer 16 | * Copyright (C) 2013 Intel Corporation 17 | * Authored by Joonas Lahtinen 18 | */ 19 | 20 | #ifndef __SDBOOT_GRAPHICS_H 21 | #define __SDBOOT_GRAPHICS_H 22 | 23 | EFI_STATUS graphics_mode(BOOLEAN on); 24 | #endif 25 | -------------------------------------------------------------------------------- /src/sd-boot/linux.c: -------------------------------------------------------------------------------- 1 | /*-*- Mode: C; c-basic-offset: 8; indent-tabs-mode: nil -*-*/ 2 | 3 | /* 4 | * This program is free software; you can redistribute it and/or modify it 5 | * under the terms of the GNU Lesser General Public License as published by 6 | * the Free Software Foundation; either version 2.1 of the License, or 7 | * (at your option) any later version. 8 | * 9 | * This program is distributed in the hope that it will be useful, but 10 | * WITHOUT ANY WARRANTY; without even the implied warranty of 11 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU 12 | * Lesser General Public License for more details. 13 | * 14 | * Copyright (C) 2015 Kay Sievers 15 | */ 16 | 17 | #include 18 | #include 19 | 20 | #include "linux.h" 21 | #include "util.h" 22 | 23 | #define SETUP_MAGIC 0x53726448 /* "HdrS" */ 24 | struct SetupHeader { 25 | UINT8 boot_sector[0x01f1]; 26 | UINT8 setup_secs; 27 | UINT16 root_flags; 28 | UINT32 sys_size; 29 | UINT16 ram_size; 30 | UINT16 video_mode; 31 | UINT16 root_dev; 32 | UINT16 signature; 33 | UINT16 jump; 34 | UINT32 header; 35 | UINT16 version; 36 | UINT16 su_switch; 37 | UINT16 setup_seg; 38 | UINT16 start_sys; 39 | UINT16 kernel_ver; 40 | UINT8 loader_id; 41 | UINT8 load_flags; 42 | UINT16 movesize; 43 | UINT32 code32_start; 44 | UINT32 ramdisk_start; 45 | UINT32 ramdisk_len; 46 | UINT32 bootsect_kludge; 47 | UINT16 heap_end; 48 | UINT8 ext_loader_ver; 49 | UINT8 ext_loader_type; 50 | UINT32 cmd_line_ptr; 51 | UINT32 ramdisk_max; 52 | UINT32 kernel_alignment; 53 | UINT8 relocatable_kernel; 54 | UINT8 min_alignment; 55 | UINT16 xloadflags; 56 | UINT32 cmdline_size; 57 | UINT32 hardware_subarch; 58 | UINT64 hardware_subarch_data; 59 | UINT32 payload_offset; 60 | UINT32 payload_length; 61 | UINT64 setup_data; 62 | UINT64 pref_address; 63 | UINT32 init_size; 64 | UINT32 handover_offset; 65 | } __attribute__((packed)); 66 | 67 | #ifdef __x86_64__ 68 | typedef VOID(*handover_f)(VOID *image, EFI_SYSTEM_TABLE *table, struct SetupHeader *setup); 69 | static inline VOID linux_efi_handover(EFI_HANDLE image, struct SetupHeader *setup) { 70 | handover_f handover; 71 | 72 | asm volatile ("cli"); 73 | handover = (handover_f)((UINTN)setup->code32_start + 512 + setup->handover_offset); 74 | handover(image, ST, setup); 75 | } 76 | #else 77 | typedef VOID(*handover_f)(VOID *image, EFI_SYSTEM_TABLE *table, struct SetupHeader *setup) __attribute__((regparm(0))); 78 | static inline VOID linux_efi_handover(EFI_HANDLE image, struct SetupHeader *setup) { 79 | handover_f handover; 80 | 81 | handover = (handover_f)((UINTN)setup->code32_start + setup->handover_offset); 82 | handover(image, ST, setup); 83 | } 84 | #endif 85 | 86 | EFI_STATUS linux_exec(EFI_HANDLE *image, 87 | CHAR8 *cmdline, UINTN cmdline_len, 88 | UINTN linux_addr, 89 | UINTN initrd_addr, UINTN initrd_size) { 90 | struct SetupHeader *image_setup; 91 | struct SetupHeader *boot_setup; 92 | EFI_PHYSICAL_ADDRESS addr; 93 | EFI_STATUS err; 94 | 95 | image_setup = (struct SetupHeader *)(linux_addr); 96 | if (image_setup->signature != 0xAA55 || image_setup->header != SETUP_MAGIC) 97 | return EFI_LOAD_ERROR; 98 | 99 | if (image_setup->version < 0x20b || !image_setup->relocatable_kernel) 100 | return EFI_LOAD_ERROR; 101 | 102 | addr = 0x3fffffff; 103 | err = uefi_call_wrapper(BS->AllocatePages, 4, AllocateMaxAddress, EfiLoaderData, 104 | EFI_SIZE_TO_PAGES(0x4000), &addr); 105 | if (EFI_ERROR(err)) 106 | return err; 107 | boot_setup = (struct SetupHeader *)(UINTN)addr; 108 | ZeroMem(boot_setup, 0x4000); 109 | CopyMem(boot_setup, image_setup, sizeof(struct SetupHeader)); 110 | boot_setup->loader_id = 0xff; 111 | 112 | boot_setup->code32_start = (UINT32)linux_addr + (image_setup->setup_secs+1) * 512; 113 | 114 | if (cmdline) { 115 | addr = 0xA0000; 116 | err = uefi_call_wrapper(BS->AllocatePages, 4, AllocateMaxAddress, EfiLoaderData, 117 | EFI_SIZE_TO_PAGES(cmdline_len + 1), &addr); 118 | if (EFI_ERROR(err)) 119 | return err; 120 | CopyMem((VOID *)(UINTN)addr, cmdline, cmdline_len); 121 | ((CHAR8 *)addr)[cmdline_len] = 0; 122 | boot_setup->cmd_line_ptr = (UINT32)addr; 123 | } 124 | 125 | boot_setup->ramdisk_start = (UINT32)initrd_addr; 126 | boot_setup->ramdisk_len = (UINT32)initrd_size; 127 | 128 | linux_efi_handover(image, boot_setup); 129 | return EFI_LOAD_ERROR; 130 | } 131 | -------------------------------------------------------------------------------- /src/sd-boot/linux.h: -------------------------------------------------------------------------------- 1 | /*-*- Mode: C; c-basic-offset: 8; indent-tabs-mode: nil -*-*/ 2 | 3 | /* 4 | * This program is free software; you can redistribute it and/or modify it 5 | * under the terms of the GNU Lesser General Public License as published by 6 | * the Free Software Foundation; either version 2.1 of the License, or 7 | * (at your option) any later version. 8 | * 9 | * This program is distributed in the hope that it will be useful, but 10 | * WITHOUT ANY WARRANTY; without even the implied warranty of 11 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU 12 | * Lesser General Public License for more details. 13 | * 14 | * Copyright (C) 2015 Kay Sievers 15 | */ 16 | 17 | #ifndef __SDBOOT_kernel_H 18 | #define __SDBOOT_kernel_H 19 | 20 | EFI_STATUS linux_exec(EFI_HANDLE *image, 21 | CHAR8 *cmdline, UINTN cmdline_size, 22 | UINTN linux_addr, 23 | UINTN initrd_addr, UINTN initrd_size); 24 | #endif 25 | -------------------------------------------------------------------------------- /src/sd-boot/measure.c: -------------------------------------------------------------------------------- 1 | /* 2 | * This program is free software; you can redistribute it and/or modify it 3 | * under the terms of the GNU Lesser General Public License as published by 4 | * the Free Software Foundation; either version 2.1 of the License, or 5 | * (at your option) any later version. 6 | * 7 | * This program is distributed in the hope that it will be useful, but 8 | * WITHOUT ANY WARRANTY; without even the implied warranty of 9 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU 10 | * Lesser General Public License for more details. 11 | * 12 | */ 13 | 14 | #ifdef SD_BOOT_LOG_TPM 15 | 16 | #include 17 | #include 18 | #include "measure.h" 19 | 20 | #define EFI_TCG_PROTOCOL_GUID { 0xf541796d, 0xa62e, 0x4954, {0xa7, 0x75, 0x95, 0x84, 0xf6, 0x1b, 0x9c, 0xdd} } 21 | 22 | typedef struct _TCG_VERSION { 23 | UINT8 Major; 24 | UINT8 Minor; 25 | UINT8 RevMajor; 26 | UINT8 RevMinor; 27 | } TCG_VERSION; 28 | 29 | typedef struct _TCG_BOOT_SERVICE_CAPABILITY { 30 | UINT8 Size; 31 | struct _TCG_VERSION StructureVersion; 32 | struct _TCG_VERSION ProtocolSpecVersion; 33 | UINT8 HashAlgorithmBitmap; 34 | BOOLEAN TPMPresentFlag; 35 | BOOLEAN TPMDeactivatedFlag; 36 | } TCG_BOOT_SERVICE_CAPABILITY; 37 | 38 | typedef UINT32 TCG_ALGORITHM_ID; 39 | #define TCG_ALG_SHA 0x00000004 // The SHA1 algorithm 40 | 41 | #define SHA1_DIGEST_SIZE 20 42 | 43 | typedef struct _TCG_DIGEST { 44 | UINT8 Digest[SHA1_DIGEST_SIZE]; 45 | } TCG_DIGEST; 46 | 47 | #define EV_IPL 13 48 | 49 | typedef struct _TCG_PCR_EVENT { 50 | UINT32 PCRIndex; 51 | UINT32 EventType; 52 | struct _TCG_DIGEST digest; 53 | UINT32 EventSize; 54 | UINT8 Event[1]; 55 | } TCG_PCR_EVENT; 56 | 57 | INTERFACE_DECL(_EFI_TCG); 58 | 59 | typedef EFI_STATUS(EFIAPI * EFI_TCG_STATUS_CHECK) (IN struct _EFI_TCG * This, 60 | OUT struct _TCG_BOOT_SERVICE_CAPABILITY * ProtocolCapability, 61 | OUT UINT32 * TCGFeatureFlags, 62 | OUT EFI_PHYSICAL_ADDRESS * EventLogLocation, 63 | OUT EFI_PHYSICAL_ADDRESS * EventLogLastEntry); 64 | 65 | typedef EFI_STATUS(EFIAPI * EFI_TCG_HASH_ALL) (IN struct _EFI_TCG * This, 66 | IN UINT8 * HashData, 67 | IN UINT64 HashDataLen, 68 | IN TCG_ALGORITHM_ID AlgorithmId, 69 | IN OUT UINT64 * HashedDataLen, IN OUT UINT8 ** HashedDataResult); 70 | 71 | typedef EFI_STATUS(EFIAPI * EFI_TCG_LOG_EVENT) (IN struct _EFI_TCG * This, 72 | IN struct _TCG_PCR_EVENT * TCGLogData, 73 | IN OUT UINT32 * EventNumber, IN UINT32 Flags); 74 | 75 | typedef EFI_STATUS(EFIAPI * EFI_TCG_PASS_THROUGH_TO_TPM) (IN struct _EFI_TCG * This, 76 | IN UINT32 TpmInputParameterBlockSize, 77 | IN UINT8 * TpmInputParameterBlock, 78 | IN UINT32 TpmOutputParameterBlockSize, 79 | IN UINT8 * TpmOutputParameterBlock); 80 | 81 | typedef EFI_STATUS(EFIAPI * EFI_TCG_HASH_LOG_EXTEND_EVENT) (IN struct _EFI_TCG * This, 82 | IN EFI_PHYSICAL_ADDRESS HashData, 83 | IN UINT64 HashDataLen, 84 | IN TCG_ALGORITHM_ID AlgorithmId, 85 | IN struct _TCG_PCR_EVENT * TCGLogData, 86 | IN OUT UINT32 * EventNumber, 87 | OUT EFI_PHYSICAL_ADDRESS * EventLogLastEntry); 88 | 89 | typedef struct _EFI_TCG { 90 | EFI_TCG_STATUS_CHECK StatusCheck; 91 | EFI_TCG_HASH_ALL HashAll; 92 | EFI_TCG_LOG_EVENT LogEvent; 93 | EFI_TCG_PASS_THROUGH_TO_TPM PassThroughToTPM; 94 | EFI_TCG_HASH_LOG_EXTEND_EVENT HashLogExtendEvent; 95 | } EFI_TCG; 96 | 97 | #define EFI_TCG2_PROTOCOL_GUID {0x607f766c, 0x7455, 0x42be, { 0x93, 0x0b, 0xe4, 0xd7, 0x6d, 0xb2, 0x72, 0x0f }} 98 | 99 | typedef struct tdEFI_TCG2_PROTOCOL EFI_TCG2_PROTOCOL; 100 | 101 | typedef struct tdEFI_TCG2_VERSION { 102 | UINT8 Major; 103 | UINT8 Minor; 104 | } EFI_TCG2_VERSION; 105 | 106 | typedef UINT32 EFI_TCG2_EVENT_LOG_BITMAP; 107 | typedef UINT32 EFI_TCG2_EVENT_LOG_FORMAT; 108 | typedef UINT32 EFI_TCG2_EVENT_ALGORITHM_BITMAP; 109 | 110 | #define EFI_TCG2_EVENT_LOG_FORMAT_TCG_1_2 0x00000001 111 | #define EFI_TCG2_EVENT_LOG_FORMAT_TCG_2 0x00000002 112 | 113 | typedef struct tdEFI_TCG2_BOOT_SERVICE_CAPABILITY { 114 | UINT8 Size; 115 | EFI_TCG2_VERSION StructureVersion; 116 | EFI_TCG2_VERSION ProtocolVersion; 117 | EFI_TCG2_EVENT_ALGORITHM_BITMAP HashAlgorithmBitmap; 118 | EFI_TCG2_EVENT_LOG_BITMAP SupportedEventLogs; 119 | BOOLEAN TPMPresentFlag; 120 | UINT16 MaxCommandSize; 121 | UINT16 MaxResponseSize; 122 | UINT32 ManufacturerID; 123 | UINT32 NumberOfPCRBanks; 124 | EFI_TCG2_EVENT_ALGORITHM_BITMAP ActivePcrBanks; 125 | } EFI_TCG2_BOOT_SERVICE_CAPABILITY; 126 | 127 | #define EFI_TCG2_EVENT_HEADER_VERSION 1 128 | 129 | typedef struct { 130 | UINT32 HeaderSize; 131 | UINT16 HeaderVersion; 132 | UINT32 PCRIndex; 133 | UINT32 EventType; 134 | } EFI_TCG2_EVENT_HEADER; 135 | 136 | typedef struct tdEFI_TCG2_EVENT { 137 | UINT32 Size; 138 | EFI_TCG2_EVENT_HEADER Header; 139 | UINT8 Event[1]; 140 | } EFI_TCG2_EVENT; 141 | 142 | typedef EFI_STATUS(EFIAPI * EFI_TCG2_GET_CAPABILITY) (IN EFI_TCG2_PROTOCOL * This, 143 | IN OUT EFI_TCG2_BOOT_SERVICE_CAPABILITY * ProtocolCapability); 144 | 145 | typedef EFI_STATUS(EFIAPI * EFI_TCG2_GET_EVENT_LOG) (IN EFI_TCG2_PROTOCOL * This, 146 | IN EFI_TCG2_EVENT_LOG_FORMAT EventLogFormat, 147 | OUT EFI_PHYSICAL_ADDRESS * EventLogLocation, 148 | OUT EFI_PHYSICAL_ADDRESS * EventLogLastEntry, 149 | OUT BOOLEAN * EventLogTruncated); 150 | 151 | typedef EFI_STATUS(EFIAPI * EFI_TCG2_HASH_LOG_EXTEND_EVENT) (IN EFI_TCG2_PROTOCOL * This, 152 | IN UINT64 Flags, 153 | IN EFI_PHYSICAL_ADDRESS DataToHash, 154 | IN UINT64 DataToHashLen, IN EFI_TCG2_EVENT * EfiTcgEvent); 155 | 156 | typedef EFI_STATUS(EFIAPI * EFI_TCG2_SUBMIT_COMMAND) (IN EFI_TCG2_PROTOCOL * This, 157 | IN UINT32 InputParameterBlockSize, 158 | IN UINT8 * InputParameterBlock, 159 | IN UINT32 OutputParameterBlockSize, IN UINT8 * OutputParameterBlock); 160 | 161 | typedef EFI_STATUS(EFIAPI * EFI_TCG2_GET_ACTIVE_PCR_BANKS) (IN EFI_TCG2_PROTOCOL * This, OUT UINT32 * ActivePcrBanks); 162 | 163 | typedef EFI_STATUS(EFIAPI * EFI_TCG2_SET_ACTIVE_PCR_BANKS) (IN EFI_TCG2_PROTOCOL * This, IN UINT32 ActivePcrBanks); 164 | 165 | typedef EFI_STATUS(EFIAPI * EFI_TCG2_GET_RESULT_OF_SET_ACTIVE_PCR_BANKS) (IN EFI_TCG2_PROTOCOL * This, 166 | OUT UINT32 * OperationPresent, OUT UINT32 * Response); 167 | 168 | typedef struct tdEFI_TCG2_PROTOCOL { 169 | EFI_TCG2_GET_CAPABILITY GetCapability; 170 | EFI_TCG2_GET_EVENT_LOG GetEventLog; 171 | EFI_TCG2_HASH_LOG_EXTEND_EVENT HashLogExtendEvent; 172 | EFI_TCG2_SUBMIT_COMMAND SubmitCommand; 173 | EFI_TCG2_GET_ACTIVE_PCR_BANKS GetActivePcrBanks; 174 | EFI_TCG2_SET_ACTIVE_PCR_BANKS SetActivePcrBanks; 175 | EFI_TCG2_GET_RESULT_OF_SET_ACTIVE_PCR_BANKS GetResultOfSetActivePcrBanks; 176 | } EFI_TCG2; 177 | 178 | 179 | static EFI_STATUS tpm1_measure_to_pcr_and_event_log(const EFI_TCG *tcg, UINT32 pcrindex, const EFI_PHYSICAL_ADDRESS buffer, 180 | UINTN buffer_size, const CHAR16 *description) { 181 | EFI_STATUS status; 182 | TCG_PCR_EVENT *tcg_event; 183 | UINT32 event_number; 184 | EFI_PHYSICAL_ADDRESS event_log_last; 185 | UINTN desc_len; 186 | 187 | desc_len = (StrLen(description) + 1) * sizeof(CHAR16); 188 | 189 | tcg_event = AllocateZeroPool(desc_len + sizeof(TCG_PCR_EVENT)); 190 | 191 | if (tcg_event == NULL) 192 | return EFI_OUT_OF_RESOURCES; 193 | 194 | tcg_event->EventSize = desc_len; 195 | CopyMem((VOID *) & tcg_event->Event[0], (VOID *) description, desc_len); 196 | 197 | tcg_event->PCRIndex = pcrindex; 198 | tcg_event->EventType = EV_IPL; 199 | 200 | event_number = 1; 201 | status = uefi_call_wrapper(tcg->HashLogExtendEvent, 7, 202 | tcg, buffer, buffer_size, TCG_ALG_SHA, tcg_event, &event_number, &event_log_last); 203 | 204 | if (EFI_ERROR(status)) 205 | return status; 206 | 207 | uefi_call_wrapper(BS->FreePool, 1, tcg_event); 208 | 209 | return EFI_SUCCESS; 210 | } 211 | 212 | /* 213 | * According to TCG EFI Protocol Specification for TPM 2.0 family, 214 | * all events generated after the invocation of EFI_TCG2_GET_EVENT_LOG 215 | * shall be stored in an instance of an EFI_CONFIGURATION_TABLE aka 216 | * EFI TCG 2.0 final events table. Hence, it is necessary to trigger the 217 | * internal switch through calling get_event_log() in order to allow 218 | * to retrieve the logs from OS runtime. 219 | */ 220 | static EFI_STATUS trigger_tcg2_final_events_table(const EFI_TCG2 *tcg) 221 | { 222 | return uefi_call_wrapper(tcg->GetEventLog, 5, tcg, 223 | EFI_TCG2_EVENT_LOG_FORMAT_TCG_2, NULL, 224 | NULL, NULL); 225 | } 226 | 227 | static EFI_STATUS tpm2_measure_to_pcr_and_event_log(const EFI_TCG2 *tcg, UINT32 pcrindex, const EFI_PHYSICAL_ADDRESS buffer, 228 | UINT64 buffer_size, const CHAR16 *description) { 229 | EFI_STATUS status; 230 | EFI_TCG2_EVENT *tcg_event; 231 | UINTN desc_len; 232 | static BOOLEAN triggered = FALSE; 233 | 234 | if (triggered == FALSE) { 235 | status = trigger_tcg2_final_events_table(tcg); 236 | if (EFI_ERROR(status)) 237 | return status; 238 | 239 | triggered = TRUE; 240 | } 241 | 242 | desc_len = StrLen(description) * sizeof(CHAR16); 243 | 244 | tcg_event = AllocateZeroPool(sizeof(*tcg_event) - sizeof(tcg_event->Event) + desc_len + 1); 245 | 246 | if (tcg_event == NULL) 247 | return EFI_OUT_OF_RESOURCES; 248 | 249 | tcg_event->Size = sizeof(EFI_TCG2_EVENT) - sizeof(tcg_event->Event) + desc_len + 1; 250 | tcg_event->Header.HeaderSize = sizeof(EFI_TCG2_EVENT_HEADER); 251 | tcg_event->Header.HeaderVersion = EFI_TCG2_EVENT_HEADER_VERSION; 252 | tcg_event->Header.PCRIndex = pcrindex; 253 | tcg_event->Header.EventType = EV_IPL; 254 | 255 | CopyMem((VOID *) tcg_event->Event, (VOID *) description, desc_len); 256 | 257 | status = uefi_call_wrapper(tcg->HashLogExtendEvent, 5, tcg, 0, buffer, buffer_size, tcg_event); 258 | 259 | uefi_call_wrapper(BS->FreePool, 1, tcg_event); 260 | 261 | if (EFI_ERROR(status)) 262 | return status; 263 | 264 | return EFI_SUCCESS; 265 | } 266 | 267 | static EFI_TCG * tcg1_interface_check(void) { 268 | EFI_GUID tpm_guid = EFI_TCG_PROTOCOL_GUID; 269 | EFI_STATUS status; 270 | EFI_TCG *tcg; 271 | TCG_BOOT_SERVICE_CAPABILITY capability; 272 | UINT32 features; 273 | EFI_PHYSICAL_ADDRESS event_log_location; 274 | EFI_PHYSICAL_ADDRESS event_log_last_entry; 275 | 276 | status = LibLocateProtocol(&tpm_guid, (void **) &tcg); 277 | 278 | if (EFI_ERROR(status)) 279 | return NULL; 280 | 281 | capability.Size = (UINT8) sizeof(capability); 282 | status = uefi_call_wrapper(tcg->StatusCheck, 5, tcg, &capability, &features, &event_log_location, &event_log_last_entry); 283 | 284 | if (EFI_ERROR(status)) 285 | return NULL; 286 | 287 | if (capability.TPMDeactivatedFlag) 288 | return NULL; 289 | 290 | if (!capability.TPMPresentFlag) 291 | return NULL; 292 | 293 | return tcg; 294 | } 295 | 296 | static EFI_TCG2 * tcg2_interface_check(void) { 297 | EFI_GUID tpm2_guid = EFI_TCG2_PROTOCOL_GUID; 298 | EFI_STATUS status; 299 | EFI_TCG2 *tcg; 300 | EFI_TCG2_BOOT_SERVICE_CAPABILITY capability; 301 | 302 | status = LibLocateProtocol(&tpm2_guid, (void **) &tcg); 303 | 304 | if (EFI_ERROR(status)) 305 | return NULL; 306 | 307 | capability.Size = (UINT8) sizeof(capability); 308 | status = uefi_call_wrapper(tcg->GetCapability, 2, tcg, &capability); 309 | 310 | if (EFI_ERROR(status)) 311 | return NULL; 312 | 313 | if (!capability.TPMPresentFlag) 314 | return NULL; 315 | 316 | return tcg; 317 | } 318 | 319 | EFI_STATUS tpm_log_event(UINT32 pcrindex, const EFI_PHYSICAL_ADDRESS buffer, UINTN buffer_size, const CHAR16 *description) { 320 | EFI_TCG *tpm1; 321 | EFI_TCG2 *tpm2; 322 | 323 | tpm2 = tcg2_interface_check(); 324 | if (tpm2) 325 | return tpm2_measure_to_pcr_and_event_log(tpm2, pcrindex, buffer, buffer_size, description); 326 | 327 | tpm1 = tcg1_interface_check(); 328 | if (tpm1) 329 | return tpm1_measure_to_pcr_and_event_log(tpm1, pcrindex, buffer, buffer_size, description); 330 | 331 | /* No active TPM found, so don't return an error */ 332 | return EFI_SUCCESS; 333 | } 334 | 335 | #endif 336 | -------------------------------------------------------------------------------- /src/sd-boot/measure.h: -------------------------------------------------------------------------------- 1 | /* 2 | * This program is free software; you can redistribute it and/or modify it 3 | * under the terms of the GNU Lesser General Public License as published by 4 | * the Free Software Foundation; either version 2.1 of the License, or 5 | * (at your option) any later version. 6 | * 7 | * This program is distributed in the hope that it will be useful, but 8 | * WITHOUT ANY WARRANTY; without even the implied warranty of 9 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU 10 | * Lesser General Public License for more details. 11 | * 12 | */ 13 | #ifndef __SDBOOT_MEASURE_H 14 | #define __SDBOOT_MEASURE_H 15 | 16 | #ifndef SD_TPM_PCR 17 | #define SD_TPM_PCR 8 18 | #endif 19 | 20 | EFI_STATUS tpm_log_event(UINT32 pcrindex, const EFI_PHYSICAL_ADDRESS buffer, UINTN buffer_size, const CHAR16 *description); 21 | #endif 22 | -------------------------------------------------------------------------------- /src/sd-boot/pefile.c: -------------------------------------------------------------------------------- 1 | /*-*- Mode: C; c-basic-offset: 8; indent-tabs-mode: nil -*-*/ 2 | 3 | /* 4 | * This program is free software; you can redistribute it and/or modify it 5 | * under the terms of the GNU Lesser General Public License as published by 6 | * the Free Software Foundation; either version 2.1 of the License, or 7 | * (at your option) any later version. 8 | * 9 | * This program is distributed in the hope that it will be useful, but 10 | * WITHOUT ANY WARRANTY; without even the implied warranty of 11 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU 12 | * Lesser General Public License for more details. 13 | * 14 | * Copyright (C) 2015 Kay Sievers 15 | */ 16 | 17 | #include 18 | #include 19 | 20 | #include "pefile.h" 21 | #include "util.h" 22 | 23 | struct DosFileHeader { 24 | UINT8 Magic[2]; 25 | UINT16 LastSize; 26 | UINT16 nBlocks; 27 | UINT16 nReloc; 28 | UINT16 HdrSize; 29 | UINT16 MinAlloc; 30 | UINT16 MaxAlloc; 31 | UINT16 ss; 32 | UINT16 sp; 33 | UINT16 Checksum; 34 | UINT16 ip; 35 | UINT16 cs; 36 | UINT16 RelocPos; 37 | UINT16 nOverlay; 38 | UINT16 reserved[4]; 39 | UINT16 OEMId; 40 | UINT16 OEMInfo; 41 | UINT16 reserved2[10]; 42 | UINT32 ExeHeader; 43 | } __attribute__((packed)); 44 | 45 | #define PE_HEADER_MACHINE_I386 0x014c 46 | #define PE_HEADER_MACHINE_X64 0x8664 47 | struct PeFileHeader { 48 | UINT16 Machine; 49 | UINT16 NumberOfSections; 50 | UINT32 TimeDateStamp; 51 | UINT32 PointerToSymbolTable; 52 | UINT32 NumberOfSymbols; 53 | UINT16 SizeOfOptionalHeader; 54 | UINT16 Characteristics; 55 | } __attribute__((packed)); 56 | 57 | struct PeSectionHeader { 58 | UINT8 Name[8]; 59 | UINT32 VirtualSize; 60 | UINT32 VirtualAddress; 61 | UINT32 SizeOfRawData; 62 | UINT32 PointerToRawData; 63 | UINT32 PointerToRelocations; 64 | UINT32 PointerToLinenumbers; 65 | UINT16 NumberOfRelocations; 66 | UINT16 NumberOfLinenumbers; 67 | UINT32 Characteristics; 68 | } __attribute__((packed)); 69 | 70 | 71 | EFI_STATUS pefile_locate_sections(EFI_FILE *dir, CHAR16 *path, CHAR8 **sections, UINTN *addrs, UINTN *offsets, UINTN *sizes) { 72 | EFI_FILE_HANDLE handle; 73 | struct DosFileHeader dos; 74 | uint8_t magic[4]; 75 | struct PeFileHeader pe; 76 | UINTN len; 77 | UINTN i; 78 | EFI_STATUS err; 79 | 80 | err = uefi_call_wrapper(dir->Open, 5, dir, &handle, path, EFI_FILE_MODE_READ, 0ULL); 81 | if (EFI_ERROR(err)) 82 | return err; 83 | 84 | /* MS-DOS stub */ 85 | len = sizeof(dos); 86 | err = uefi_call_wrapper(handle->Read, 3, handle, &len, &dos); 87 | if (EFI_ERROR(err)) 88 | goto out; 89 | if (len != sizeof(dos)) { 90 | err = EFI_LOAD_ERROR; 91 | goto out; 92 | } 93 | 94 | if (CompareMem(dos.Magic, "MZ", 2) != 0) { 95 | err = EFI_LOAD_ERROR; 96 | goto out; 97 | } 98 | 99 | err = uefi_call_wrapper(handle->SetPosition, 2, handle, dos.ExeHeader); 100 | if (EFI_ERROR(err)) 101 | goto out; 102 | 103 | /* PE header */ 104 | len = sizeof(magic); 105 | err = uefi_call_wrapper(handle->Read, 3, handle, &len, &magic); 106 | if (EFI_ERROR(err)) 107 | goto out; 108 | if (len != sizeof(magic)) { 109 | err = EFI_LOAD_ERROR; 110 | goto out; 111 | } 112 | 113 | if (CompareMem(magic, "PE\0\0", 2) != 0) { 114 | err = EFI_LOAD_ERROR; 115 | goto out; 116 | } 117 | 118 | len = sizeof(pe); 119 | err = uefi_call_wrapper(handle->Read, 3, handle, &len, &pe); 120 | if (EFI_ERROR(err)) 121 | goto out; 122 | if (len != sizeof(pe)) { 123 | err = EFI_LOAD_ERROR; 124 | goto out; 125 | } 126 | 127 | /* PE32+ Subsystem type */ 128 | if (pe.Machine != PE_HEADER_MACHINE_X64 && 129 | pe.Machine != PE_HEADER_MACHINE_I386) { 130 | err = EFI_LOAD_ERROR; 131 | goto out; 132 | } 133 | 134 | if (pe.NumberOfSections > 96) { 135 | err = EFI_LOAD_ERROR; 136 | goto out; 137 | } 138 | 139 | /* the sections start directly after the headers */ 140 | err = uefi_call_wrapper(handle->SetPosition, 2, handle, dos.ExeHeader + sizeof(magic) + sizeof(pe) + pe.SizeOfOptionalHeader); 141 | if (EFI_ERROR(err)) 142 | goto out; 143 | 144 | for (i = 0; i < pe.NumberOfSections; i++) { 145 | struct PeSectionHeader sect; 146 | UINTN j; 147 | 148 | len = sizeof(sect); 149 | err = uefi_call_wrapper(handle->Read, 3, handle, &len, §); 150 | if (EFI_ERROR(err)) 151 | goto out; 152 | if (len != sizeof(sect)) { 153 | err = EFI_LOAD_ERROR; 154 | goto out; 155 | } 156 | for (j = 0; sections[j]; j++) { 157 | if (CompareMem(sect.Name, sections[j], strlena(sections[j])) != 0) 158 | continue; 159 | 160 | if (addrs) 161 | addrs[j] = (UINTN)sect.VirtualAddress; 162 | if (offsets) 163 | offsets[j] = (UINTN)sect.PointerToRawData; 164 | if (sizes) 165 | sizes[j] = (UINTN)sect.VirtualSize; 166 | } 167 | } 168 | 169 | out: 170 | uefi_call_wrapper(handle->Close, 1, handle); 171 | return err; 172 | } 173 | -------------------------------------------------------------------------------- /src/sd-boot/pefile.h: -------------------------------------------------------------------------------- 1 | /*-*- Mode: C; c-basic-offset: 8; indent-tabs-mode: nil -*-*/ 2 | 3 | /* 4 | * This program is free software; you can redistribute it and/or modify it 5 | * under the terms of the GNU Lesser General Public License as published by 6 | * the Free Software Foundation; either version 2.1 of the License, or 7 | * (at your option) any later version. 8 | * 9 | * This program is distributed in the hope that it will be useful, but 10 | * WITHOUT ANY WARRANTY; without even the implied warranty of 11 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU 12 | * Lesser General Public License for more details. 13 | * 14 | * Copyright (C) 2015 Kay Sievers 15 | */ 16 | 17 | #ifndef __SDBOOT_PEFILE_H 18 | #define __SDBOOT_PEFILE_H 19 | 20 | EFI_STATUS pefile_locate_sections(EFI_FILE *dir, CHAR16 *path, 21 | CHAR8 **sections, UINTN *addrs, UINTN *offsets, UINTN *sizes); 22 | #endif 23 | -------------------------------------------------------------------------------- /src/sd-boot/splash.c: -------------------------------------------------------------------------------- 1 | /*-*- Mode: C; c-basic-offset: 8; indent-tabs-mode: nil -*-*/ 2 | 3 | /* 4 | * This program is free software; you can redistribute it and/or modify it 5 | * under the terms of the GNU Lesser General Public License as published by 6 | * the Free Software Foundation; either version 2.1 of the License, or 7 | * (at your option) any later version. 8 | * 9 | * This program is distributed in the hope that it will be useful, but 10 | * WITHOUT ANY WARRANTY; without even the implied warranty of 11 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU 12 | * Lesser General Public License for more details. 13 | * 14 | * Copyright (C) 2012-2013 Kay Sievers 15 | * Copyright (C) 2012 Harald Hoyer 16 | */ 17 | 18 | #include 19 | #include 20 | 21 | #include "graphics.h" 22 | #include "splash.h" 23 | #include "util.h" 24 | 25 | struct bmp_file { 26 | CHAR8 signature[2]; 27 | UINT32 size; 28 | UINT16 reserved[2]; 29 | UINT32 offset; 30 | } __attribute__((packed)); 31 | 32 | /* we require at least BITMAPINFOHEADER, later versions are 33 | accepted, but their features ignored */ 34 | struct bmp_dib { 35 | UINT32 size; 36 | UINT32 x; 37 | UINT32 y; 38 | UINT16 planes; 39 | UINT16 depth; 40 | UINT32 compression; 41 | UINT32 image_size; 42 | INT32 x_pixel_meter; 43 | INT32 y_pixel_meter; 44 | UINT32 colors_used; 45 | UINT32 colors_important; 46 | } __attribute__((packed)); 47 | 48 | struct bmp_map { 49 | UINT8 blue; 50 | UINT8 green; 51 | UINT8 red; 52 | UINT8 reserved; 53 | } __attribute__((packed)); 54 | 55 | EFI_STATUS bmp_parse_header(UINT8 *bmp, UINTN size, struct bmp_dib **ret_dib, 56 | struct bmp_map **ret_map, UINT8 **pixmap) { 57 | struct bmp_file *file; 58 | struct bmp_dib *dib; 59 | struct bmp_map *map; 60 | UINTN row_size; 61 | 62 | if (size < sizeof(struct bmp_file) + sizeof(struct bmp_dib)) 63 | return EFI_INVALID_PARAMETER; 64 | 65 | /* check file header */ 66 | file = (struct bmp_file *)bmp; 67 | if (file->signature[0] != 'B' || file->signature[1] != 'M') 68 | return EFI_INVALID_PARAMETER; 69 | if (file->size != size) 70 | return EFI_INVALID_PARAMETER; 71 | if (file->size < file->offset) 72 | return EFI_INVALID_PARAMETER; 73 | 74 | /* check device-independent bitmap */ 75 | dib = (struct bmp_dib *)(bmp + sizeof(struct bmp_file)); 76 | if (dib->size < sizeof(struct bmp_dib)) 77 | return EFI_UNSUPPORTED; 78 | 79 | switch (dib->depth) { 80 | case 1: 81 | case 4: 82 | case 8: 83 | case 24: 84 | if (dib->compression != 0) 85 | return EFI_UNSUPPORTED; 86 | 87 | break; 88 | 89 | case 16: 90 | case 32: 91 | if (dib->compression != 0 && dib->compression != 3) 92 | return EFI_UNSUPPORTED; 93 | 94 | break; 95 | 96 | default: 97 | return EFI_UNSUPPORTED; 98 | } 99 | 100 | row_size = ((UINTN) dib->depth * dib->x + 31) / 32 * 4; 101 | if (file->size - file->offset < dib->y * row_size) 102 | return EFI_INVALID_PARAMETER; 103 | if (row_size * dib->y > 64 * 1024 * 1024) 104 | return EFI_INVALID_PARAMETER; 105 | 106 | /* check color table */ 107 | map = (struct bmp_map *)(bmp + sizeof(struct bmp_file) + dib->size); 108 | if (file->offset < sizeof(struct bmp_file) + dib->size) 109 | return EFI_INVALID_PARAMETER; 110 | 111 | if (file->offset > sizeof(struct bmp_file) + dib->size) { 112 | UINT32 map_count; 113 | UINTN map_size; 114 | 115 | if (dib->colors_used) 116 | map_count = dib->colors_used; 117 | else { 118 | switch (dib->depth) { 119 | case 1: 120 | case 4: 121 | case 8: 122 | map_count = 1 << dib->depth; 123 | break; 124 | 125 | default: 126 | map_count = 0; 127 | break; 128 | } 129 | } 130 | 131 | map_size = file->offset - (sizeof(struct bmp_file) + dib->size); 132 | if (map_size != sizeof(struct bmp_map) * map_count) 133 | return EFI_INVALID_PARAMETER; 134 | } 135 | 136 | *ret_map = map; 137 | *ret_dib = dib; 138 | *pixmap = bmp + file->offset; 139 | 140 | return EFI_SUCCESS; 141 | } 142 | 143 | static VOID pixel_blend(UINT32 *dst, const UINT32 source) { 144 | UINT32 alpha, src, src_rb, src_g, dst_rb, dst_g, rb, g; 145 | 146 | alpha = (source & 0xff); 147 | 148 | /* convert src from RGBA to XRGB */ 149 | src = source >> 8; 150 | 151 | /* decompose into RB and G components */ 152 | src_rb = (src & 0xff00ff); 153 | src_g = (src & 0x00ff00); 154 | 155 | dst_rb = (*dst & 0xff00ff); 156 | dst_g = (*dst & 0x00ff00); 157 | 158 | /* blend */ 159 | rb = ((((src_rb - dst_rb) * alpha + 0x800080) >> 8) + dst_rb) & 0xff00ff; 160 | g = ((((src_g - dst_g) * alpha + 0x008000) >> 8) + dst_g) & 0x00ff00; 161 | 162 | *dst = (rb | g); 163 | } 164 | 165 | EFI_STATUS bmp_to_blt(EFI_GRAPHICS_OUTPUT_BLT_PIXEL *buf, 166 | struct bmp_dib *dib, struct bmp_map *map, 167 | UINT8 *pixmap) { 168 | UINT8 *in; 169 | UINTN y; 170 | 171 | /* transform and copy pixels */ 172 | in = pixmap; 173 | for (y = 0; y < dib->y; y++) { 174 | EFI_GRAPHICS_OUTPUT_BLT_PIXEL *out; 175 | UINTN row_size; 176 | UINTN x; 177 | 178 | out = &buf[(dib->y - y - 1) * dib->x]; 179 | for (x = 0; x < dib->x; x++, in++, out++) { 180 | switch (dib->depth) { 181 | case 1: { 182 | UINTN i; 183 | 184 | for (i = 0; i < 8 && x < dib->x; i++) { 185 | out->Red = map[((*in) >> (7 - i)) & 1].red; 186 | out->Green = map[((*in) >> (7 - i)) & 1].green; 187 | out->Blue = map[((*in) >> (7 - i)) & 1].blue; 188 | out++; 189 | x++; 190 | } 191 | out--; 192 | x--; 193 | break; 194 | } 195 | 196 | case 4: { 197 | UINTN i; 198 | 199 | i = (*in) >> 4; 200 | out->Red = map[i].red; 201 | out->Green = map[i].green; 202 | out->Blue = map[i].blue; 203 | if (x < (dib->x - 1)) { 204 | out++; 205 | x++; 206 | i = (*in) & 0x0f; 207 | out->Red = map[i].red; 208 | out->Green = map[i].green; 209 | out->Blue = map[i].blue; 210 | } 211 | break; 212 | } 213 | 214 | case 8: 215 | out->Red = map[*in].red; 216 | out->Green = map[*in].green; 217 | out->Blue = map[*in].blue; 218 | break; 219 | 220 | case 16: { 221 | UINT16 i = *(UINT16 *) in; 222 | 223 | out->Red = (i & 0x7c00) >> 7; 224 | out->Green = (i & 0x3e0) >> 2; 225 | out->Blue = (i & 0x1f) << 3; 226 | in += 1; 227 | break; 228 | } 229 | 230 | case 24: 231 | out->Red = in[2]; 232 | out->Green = in[1]; 233 | out->Blue = in[0]; 234 | in += 2; 235 | break; 236 | 237 | case 32: { 238 | UINT32 i = *(UINT32 *) in; 239 | 240 | pixel_blend((UINT32 *)out, i); 241 | 242 | in += 3; 243 | break; 244 | } 245 | } 246 | } 247 | 248 | /* add row padding; new lines always start at 32 bit boundary */ 249 | row_size = in - pixmap; 250 | in += ((row_size + 3) & ~3) - row_size; 251 | } 252 | 253 | return EFI_SUCCESS; 254 | } 255 | 256 | EFI_STATUS graphics_splash(UINT8 *content, UINTN len, const EFI_GRAPHICS_OUTPUT_BLT_PIXEL *background) { 257 | EFI_GRAPHICS_OUTPUT_BLT_PIXEL pixel = {}; 258 | EFI_GUID GraphicsOutputProtocolGuid = EFI_GRAPHICS_OUTPUT_PROTOCOL_GUID; 259 | EFI_GRAPHICS_OUTPUT_PROTOCOL *GraphicsOutput = NULL; 260 | struct bmp_dib *dib; 261 | struct bmp_map *map; 262 | UINT8 *pixmap; 263 | UINT64 blt_size; 264 | VOID *blt = NULL; 265 | UINTN x_pos = 0; 266 | UINTN y_pos = 0; 267 | EFI_STATUS err; 268 | 269 | if (!background) { 270 | if (StriCmp(L"Apple", ST->FirmwareVendor) == 0) { 271 | pixel.Red = 0xc0; 272 | pixel.Green = 0xc0; 273 | pixel.Blue = 0xc0; 274 | } 275 | background = &pixel; 276 | } 277 | 278 | err = LibLocateProtocol(&GraphicsOutputProtocolGuid, (VOID **)&GraphicsOutput); 279 | if (EFI_ERROR(err)) 280 | return err; 281 | 282 | err = bmp_parse_header(content, len, &dib, &map, &pixmap); 283 | if (EFI_ERROR(err)) 284 | goto err; 285 | 286 | if(dib->x < GraphicsOutput->Mode->Info->HorizontalResolution) 287 | x_pos = (GraphicsOutput->Mode->Info->HorizontalResolution - dib->x) / 2; 288 | if(dib->y < GraphicsOutput->Mode->Info->VerticalResolution) 289 | y_pos = (GraphicsOutput->Mode->Info->VerticalResolution - dib->y) / 2; 290 | 291 | uefi_call_wrapper(GraphicsOutput->Blt, 10, GraphicsOutput, 292 | (EFI_GRAPHICS_OUTPUT_BLT_PIXEL *)background, 293 | EfiBltVideoFill, 0, 0, 0, 0, 294 | GraphicsOutput->Mode->Info->HorizontalResolution, 295 | GraphicsOutput->Mode->Info->VerticalResolution, 0); 296 | 297 | /* EFI buffer */ 298 | blt_size = dib->x * dib->y * sizeof(EFI_GRAPHICS_OUTPUT_BLT_PIXEL); 299 | blt = AllocatePool(blt_size); 300 | if (!blt) 301 | return EFI_OUT_OF_RESOURCES; 302 | 303 | err = uefi_call_wrapper(GraphicsOutput->Blt, 10, GraphicsOutput, 304 | blt, EfiBltVideoToBltBuffer, x_pos, y_pos, 0, 0, 305 | dib->x, dib->y, 0); 306 | if (EFI_ERROR(err)) 307 | goto err; 308 | 309 | err = bmp_to_blt(blt, dib, map, pixmap); 310 | if (EFI_ERROR(err)) 311 | goto err; 312 | 313 | err = graphics_mode(TRUE); 314 | if (EFI_ERROR(err)) 315 | goto err; 316 | 317 | err = uefi_call_wrapper(GraphicsOutput->Blt, 10, GraphicsOutput, 318 | blt, EfiBltBufferToVideo, 0, 0, x_pos, y_pos, 319 | dib->x, dib->y, 0); 320 | err: 321 | FreePool(blt); 322 | return err; 323 | } 324 | -------------------------------------------------------------------------------- /src/sd-boot/splash.h: -------------------------------------------------------------------------------- 1 | /*-*- Mode: C; c-basic-offset: 8; indent-tabs-mode: nil -*-*/ 2 | 3 | /* 4 | * This program is free software; you can redistribute it and/or modify it 5 | * under the terms of the GNU Lesser General Public License as published by 6 | * the Free Software Foundation; either version 2.1 of the License, or 7 | * (at your option) any later version. 8 | * 9 | * This program is distributed in the hope that it will be useful, but 10 | * WITHOUT ANY WARRANTY; without even the implied warranty of 11 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU 12 | * Lesser General Public License for more details. 13 | * 14 | * Copyright (C) 2012-2013 Kay Sievers 15 | * Copyright (C) 2012 Harald Hoyer 16 | */ 17 | 18 | #ifndef __SDBOOT_SPLASH_H 19 | #define __SDBOOT_SPLASH_H 20 | 21 | EFI_STATUS graphics_splash(UINT8 *content, UINTN len, const EFI_GRAPHICS_OUTPUT_BLT_PIXEL *background); 22 | #endif 23 | -------------------------------------------------------------------------------- /src/sd-boot/stub.c: -------------------------------------------------------------------------------- 1 | /*-*- Mode: C; c-basic-offset: 8; indent-tabs-mode: nil -*-*/ 2 | 3 | /* This program is free software; you can redistribute it and/or modify it 4 | * under the terms of the GNU Lesser General Public License as published by 5 | * the Free Software Foundation; either version 2.1 of the License, or 6 | * (at your option) any later version. 7 | * 8 | * This program is distributed in the hope that it will be useful, but 9 | * WITHOUT ANY WARRANTY; without even the implied warranty of 10 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU 11 | * Lesser General Public License for more details. 12 | * 13 | * Copyright (C) 2015 Kay Sievers 14 | */ 15 | 16 | #include 17 | #include 18 | 19 | #include "disk.h" 20 | #include "graphics.h" 21 | #include "linux.h" 22 | #include "pefile.h" 23 | #include "splash.h" 24 | #include "util.h" 25 | 26 | /* magic string to find in the binary image */ 27 | static const char __attribute__((used)) magic[] = "#### LoaderInfo: systemd-stub " VERSION " ####"; 28 | 29 | static const EFI_GUID global_guid = EFI_GLOBAL_VARIABLE; 30 | 31 | EFI_STATUS efi_main(EFI_HANDLE image, EFI_SYSTEM_TABLE *sys_table) { 32 | EFI_LOADED_IMAGE *loaded_image; 33 | EFI_FILE *root_dir; 34 | CHAR16 *loaded_image_path; 35 | CHAR8 *b; 36 | UINTN size; 37 | BOOLEAN secure = FALSE; 38 | CHAR8 *sections[] = { 39 | (UINT8 *)".cmdline", 40 | (UINT8 *)".linux", 41 | (UINT8 *)".initrd", 42 | (UINT8 *)".splash", 43 | NULL 44 | }; 45 | UINTN addrs[ELEMENTSOF(sections)-1] = {}; 46 | UINTN offs[ELEMENTSOF(sections)-1] = {}; 47 | UINTN szs[ELEMENTSOF(sections)-1] = {}; 48 | CHAR8 *cmdline = NULL; 49 | UINTN cmdline_len; 50 | CHAR16 uuid[37]; 51 | EFI_STATUS err; 52 | 53 | InitializeLib(image, sys_table); 54 | 55 | err = uefi_call_wrapper(BS->OpenProtocol, 6, image, &LoadedImageProtocol, (VOID **)&loaded_image, 56 | image, NULL, EFI_OPEN_PROTOCOL_GET_PROTOCOL); 57 | if (EFI_ERROR(err)) { 58 | Print(L"Error getting a LoadedImageProtocol handle: %r ", err); 59 | uefi_call_wrapper(BS->Stall, 1, 3 * 1000 * 1000); 60 | return err; 61 | } 62 | 63 | root_dir = LibOpenRoot(loaded_image->DeviceHandle); 64 | if (!root_dir) { 65 | Print(L"Unable to open root directory: %r ", err); 66 | uefi_call_wrapper(BS->Stall, 1, 3 * 1000 * 1000); 67 | return EFI_LOAD_ERROR; 68 | } 69 | 70 | loaded_image_path = DevicePathToStr(loaded_image->FilePath); 71 | 72 | if (efivar_get_raw(&global_guid, L"SecureBoot", &b, &size) == EFI_SUCCESS) { 73 | if (*b > 0) 74 | secure = TRUE; 75 | FreePool(b); 76 | } 77 | 78 | err = pefile_locate_sections(root_dir, loaded_image_path, sections, addrs, offs, szs); 79 | if (EFI_ERROR(err)) { 80 | Print(L"Unable to locate embedded .linux section: %r ", err); 81 | uefi_call_wrapper(BS->Stall, 1, 3 * 1000 * 1000); 82 | return err; 83 | } 84 | 85 | if (szs[0] > 0) 86 | cmdline = (CHAR8 *)(loaded_image->ImageBase + addrs[0]); 87 | 88 | cmdline_len = szs[0]; 89 | 90 | /* if we are not in secure boot mode, accept a custom command line and replace the built-in one */ 91 | if (!secure && loaded_image->LoadOptionsSize > 0) { 92 | CHAR16 *options; 93 | CHAR8 *line; 94 | UINTN i; 95 | 96 | options = (CHAR16 *)loaded_image->LoadOptions; 97 | cmdline_len = (loaded_image->LoadOptionsSize / sizeof(CHAR16)) * sizeof(CHAR8); 98 | line = AllocatePool(cmdline_len); 99 | for (i = 0; i < cmdline_len; i++) 100 | line[i] = options[i]; 101 | cmdline = line; 102 | } 103 | 104 | /* export the device path this image is started from */ 105 | if (disk_get_part_uuid(loaded_image->DeviceHandle, uuid) == EFI_SUCCESS) 106 | efivar_set(L"LoaderDevicePartUUID", uuid, FALSE); 107 | 108 | if (szs[3] > 0) 109 | graphics_splash((UINT8 *)((UINTN)loaded_image->ImageBase + addrs[3]), szs[3], NULL); 110 | 111 | err = linux_exec(image, cmdline, cmdline_len, 112 | (UINTN)loaded_image->ImageBase + addrs[1], 113 | (UINTN)loaded_image->ImageBase + addrs[2], szs[2]); 114 | 115 | graphics_mode(FALSE); 116 | Print(L"Execution of embedded linux image failed: %r\n", err); 117 | uefi_call_wrapper(BS->Stall, 1, 3 * 1000 * 1000); 118 | return err; 119 | } 120 | -------------------------------------------------------------------------------- /src/sd-boot/util.c: -------------------------------------------------------------------------------- 1 | /*-*- Mode: C; c-basic-offset: 8; indent-tabs-mode: nil -*-*/ 2 | 3 | /* 4 | * This program is free software; you can redistribute it and/or modify it 5 | * under the terms of the GNU Lesser General Public License as published by 6 | * the Free Software Foundation; either version 2.1 of the License, or 7 | * (at your option) any later version. 8 | * 9 | * This program is distributed in the hope that it will be useful, but 10 | * WITHOUT ANY WARRANTY; without even the implied warranty of 11 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU 12 | * Lesser General Public License for more details. 13 | * 14 | * Copyright (C) 2012-2013 Kay Sievers 15 | * Copyright (C) 2012 Harald Hoyer 16 | */ 17 | 18 | #include 19 | #include 20 | 21 | #include "util.h" 22 | 23 | /* 24 | * Allocated random UUID, intended to be shared across tools that implement 25 | * the (ESP)\loader\entries\-.conf convention and the 26 | * associated EFI variables. 27 | */ 28 | static const EFI_GUID loader_guid = { 0x4a67b082, 0x0a4c, 0x41cf, {0xb6, 0xc7, 0x44, 0x0b, 0x29, 0xbb, 0x8c, 0x4f} }; 29 | 30 | #ifdef __x86_64__ 31 | UINT64 ticks_read(VOID) { 32 | UINT64 a, d; 33 | __asm__ volatile ("rdtsc" : "=a" (a), "=d" (d)); 34 | return (d << 32) | a; 35 | } 36 | #elif defined(__i386__) 37 | UINT64 ticks_read(VOID) { 38 | UINT64 val; 39 | __asm__ volatile ("rdtsc" : "=A" (val)); 40 | return val; 41 | } 42 | #else 43 | UINT64 ticks_read(VOID) { 44 | UINT64 val = 1; 45 | return val; 46 | } 47 | #endif 48 | 49 | /* count TSC ticks during a millisecond delay */ 50 | UINT64 ticks_freq(VOID) { 51 | UINT64 ticks_start, ticks_end; 52 | 53 | ticks_start = ticks_read(); 54 | uefi_call_wrapper(BS->Stall, 1, 1000); 55 | ticks_end = ticks_read(); 56 | 57 | return (ticks_end - ticks_start) * 1000; 58 | } 59 | 60 | UINT64 time_usec(VOID) { 61 | UINT64 ticks; 62 | static UINT64 freq; 63 | 64 | ticks = ticks_read(); 65 | if (ticks == 0) 66 | return 0; 67 | 68 | if (freq == 0) { 69 | freq = ticks_freq(); 70 | if (freq == 0) 71 | return 0; 72 | } 73 | 74 | return 1000 * 1000 * ticks / freq; 75 | } 76 | 77 | EFI_STATUS parse_boolean(CHAR8 *v, BOOLEAN *b) { 78 | if (strcmpa(v, (CHAR8 *)"1") == 0 || 79 | strcmpa(v, (CHAR8 *)"yes") == 0 || 80 | strcmpa(v, (CHAR8 *)"y") == 0 || 81 | strcmpa(v, (CHAR8 *)"true") == 0) { 82 | *b = TRUE; 83 | return EFI_SUCCESS; 84 | } 85 | 86 | if (strcmpa(v, (CHAR8 *)"0") == 0 || 87 | strcmpa(v, (CHAR8 *)"no") == 0 || 88 | strcmpa(v, (CHAR8 *)"n") == 0 || 89 | strcmpa(v, (CHAR8 *)"false") == 0) { 90 | *b = FALSE; 91 | return EFI_SUCCESS; 92 | } 93 | 94 | return EFI_INVALID_PARAMETER; 95 | } 96 | 97 | EFI_STATUS efivar_set_raw(const EFI_GUID *vendor, CHAR16 *name, CHAR8 *buf, UINTN size, BOOLEAN persistent) { 98 | UINT32 flags; 99 | 100 | flags = EFI_VARIABLE_BOOTSERVICE_ACCESS|EFI_VARIABLE_RUNTIME_ACCESS; 101 | if (persistent) 102 | flags |= EFI_VARIABLE_NON_VOLATILE; 103 | 104 | return uefi_call_wrapper(RT->SetVariable, 5, name, (EFI_GUID *)vendor, flags, size, buf); 105 | } 106 | 107 | EFI_STATUS efivar_set(CHAR16 *name, CHAR16 *value, BOOLEAN persistent) { 108 | return efivar_set_raw(&loader_guid, name, (CHAR8 *)value, value ? (StrLen(value)+1) * sizeof(CHAR16) : 0, persistent); 109 | } 110 | 111 | EFI_STATUS efivar_set_int(CHAR16 *name, UINTN i, BOOLEAN persistent) { 112 | CHAR16 str[32]; 113 | 114 | SPrint(str, 32, L"%d", i); 115 | return efivar_set(name, str, persistent); 116 | } 117 | 118 | EFI_STATUS efivar_get(CHAR16 *name, CHAR16 **value) { 119 | CHAR8 *buf; 120 | CHAR16 *val; 121 | UINTN size; 122 | EFI_STATUS err; 123 | 124 | err = efivar_get_raw(&loader_guid, name, &buf, &size); 125 | if (EFI_ERROR(err)) 126 | return err; 127 | 128 | val = StrDuplicate((CHAR16 *)buf); 129 | if (!val) { 130 | FreePool(buf); 131 | return EFI_OUT_OF_RESOURCES; 132 | } 133 | 134 | *value = val; 135 | return EFI_SUCCESS; 136 | } 137 | 138 | EFI_STATUS efivar_get_int(CHAR16 *name, UINTN *i) { 139 | CHAR16 *val; 140 | EFI_STATUS err; 141 | 142 | err = efivar_get(name, &val); 143 | if (!EFI_ERROR(err)) { 144 | *i = Atoi(val); 145 | FreePool(val); 146 | } 147 | return err; 148 | } 149 | 150 | EFI_STATUS efivar_get_raw(const EFI_GUID *vendor, CHAR16 *name, CHAR8 **buffer, UINTN *size) { 151 | CHAR8 *buf; 152 | UINTN l; 153 | EFI_STATUS err; 154 | 155 | l = sizeof(CHAR16 *) * EFI_MAXIMUM_VARIABLE_SIZE; 156 | buf = AllocatePool(l); 157 | if (!buf) 158 | return EFI_OUT_OF_RESOURCES; 159 | 160 | err = uefi_call_wrapper(RT->GetVariable, 5, name, (EFI_GUID *)vendor, NULL, &l, buf); 161 | if (!EFI_ERROR(err)) { 162 | *buffer = buf; 163 | if (size) 164 | *size = l; 165 | } else 166 | FreePool(buf); 167 | return err; 168 | 169 | } 170 | 171 | VOID efivar_set_time_usec(CHAR16 *name, UINT64 usec) { 172 | CHAR16 str[32]; 173 | 174 | if (usec == 0) 175 | usec = time_usec(); 176 | if (usec == 0) 177 | return; 178 | 179 | SPrint(str, 32, L"%ld", usec); 180 | efivar_set(name, str, FALSE); 181 | } 182 | 183 | static INTN utf8_to_16(CHAR8 *stra, CHAR16 *c) { 184 | CHAR16 unichar; 185 | UINTN len; 186 | UINTN i; 187 | 188 | if (stra[0] < 0x80) 189 | len = 1; 190 | else if ((stra[0] & 0xe0) == 0xc0) 191 | len = 2; 192 | else if ((stra[0] & 0xf0) == 0xe0) 193 | len = 3; 194 | else if ((stra[0] & 0xf8) == 0xf0) 195 | len = 4; 196 | else if ((stra[0] & 0xfc) == 0xf8) 197 | len = 5; 198 | else if ((stra[0] & 0xfe) == 0xfc) 199 | len = 6; 200 | else 201 | return -1; 202 | 203 | switch (len) { 204 | case 1: 205 | unichar = stra[0]; 206 | break; 207 | case 2: 208 | unichar = stra[0] & 0x1f; 209 | break; 210 | case 3: 211 | unichar = stra[0] & 0x0f; 212 | break; 213 | case 4: 214 | unichar = stra[0] & 0x07; 215 | break; 216 | case 5: 217 | unichar = stra[0] & 0x03; 218 | break; 219 | case 6: 220 | unichar = stra[0] & 0x01; 221 | break; 222 | } 223 | 224 | for (i = 1; i < len; i++) { 225 | if ((stra[i] & 0xc0) != 0x80) 226 | return -1; 227 | unichar <<= 6; 228 | unichar |= stra[i] & 0x3f; 229 | } 230 | 231 | *c = unichar; 232 | return len; 233 | } 234 | 235 | CHAR16 *stra_to_str(CHAR8 *stra) { 236 | UINTN strlen; 237 | UINTN len; 238 | UINTN i; 239 | CHAR16 *str; 240 | 241 | len = strlena(stra); 242 | str = AllocatePool((len + 1) * sizeof(CHAR16)); 243 | 244 | strlen = 0; 245 | i = 0; 246 | while (i < len) { 247 | INTN utf8len; 248 | 249 | utf8len = utf8_to_16(stra + i, str + strlen); 250 | if (utf8len <= 0) { 251 | /* invalid utf8 sequence, skip the garbage */ 252 | i++; 253 | continue; 254 | } 255 | 256 | strlen++; 257 | i += utf8len; 258 | } 259 | str[strlen] = '\0'; 260 | return str; 261 | } 262 | 263 | CHAR16 *stra_to_path(CHAR8 *stra) { 264 | CHAR16 *str; 265 | UINTN strlen; 266 | UINTN len; 267 | UINTN i; 268 | 269 | len = strlena(stra); 270 | str = AllocatePool((len + 2) * sizeof(CHAR16)); 271 | 272 | str[0] = '\\'; 273 | strlen = 1; 274 | i = 0; 275 | while (i < len) { 276 | INTN utf8len; 277 | 278 | utf8len = utf8_to_16(stra + i, str + strlen); 279 | if (utf8len <= 0) { 280 | /* invalid utf8 sequence, skip the garbage */ 281 | i++; 282 | continue; 283 | } 284 | 285 | if (str[strlen] == '/') 286 | str[strlen] = '\\'; 287 | if (str[strlen] == '\\' && str[strlen-1] == '\\') { 288 | /* skip double slashes */ 289 | i += utf8len; 290 | continue; 291 | } 292 | 293 | strlen++; 294 | i += utf8len; 295 | } 296 | str[strlen] = '\0'; 297 | return str; 298 | } 299 | 300 | CHAR8 *strchra(CHAR8 *s, CHAR8 c) { 301 | do { 302 | if (*s == c) 303 | return s; 304 | } while (*s++); 305 | return NULL; 306 | } 307 | 308 | INTN file_read(EFI_FILE_HANDLE dir, CHAR16 *name, UINTN off, UINTN size, CHAR8 **content) { 309 | EFI_FILE_HANDLE handle; 310 | CHAR8 *buf; 311 | UINTN buflen; 312 | EFI_STATUS err; 313 | UINTN len; 314 | 315 | err = uefi_call_wrapper(dir->Open, 5, dir, &handle, name, EFI_FILE_MODE_READ, 0ULL); 316 | if (EFI_ERROR(err)) 317 | return err; 318 | 319 | if (size == 0) { 320 | EFI_FILE_INFO *info; 321 | 322 | info = LibFileInfo(handle); 323 | buflen = info->FileSize+1; 324 | FreePool(info); 325 | } else 326 | buflen = size; 327 | 328 | if (off > 0) { 329 | err = uefi_call_wrapper(handle->SetPosition, 2, handle, off); 330 | if (EFI_ERROR(err)) 331 | return err; 332 | } 333 | 334 | buf = AllocatePool(buflen); 335 | err = uefi_call_wrapper(handle->Read, 3, handle, &buflen, buf); 336 | if (!EFI_ERROR(err)) { 337 | buf[buflen] = '\0'; 338 | *content = buf; 339 | len = buflen; 340 | } else { 341 | len = err; 342 | FreePool(buf); 343 | } 344 | 345 | uefi_call_wrapper(handle->Close, 1, handle); 346 | return len; 347 | } 348 | -------------------------------------------------------------------------------- /src/sd-boot/util.h: -------------------------------------------------------------------------------- 1 | /*-*- Mode: C; c-basic-offset: 8; indent-tabs-mode: nil -*-*/ 2 | 3 | /* 4 | * This program is free software; you can redistribute it and/or modify it 5 | * under the terms of the GNU Lesser General Public License as published by 6 | * the Free Software Foundation; either version 2.1 of the License, or 7 | * (at your option) any later version. 8 | * 9 | * This program is distributed in the hope that it will be useful, but 10 | * WITHOUT ANY WARRANTY; without even the implied warranty of 11 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU 12 | * Lesser General Public License for more details. 13 | * 14 | * Copyright (C) 2012-2013 Kay Sievers 15 | * Copyright (C) 2012 Harald Hoyer 16 | */ 17 | 18 | #ifndef __SDBOOT_UTIL_H 19 | #define __SDBOOT_UTIL_H 20 | 21 | #include 22 | #include 23 | 24 | #define ELEMENTSOF(x) (sizeof(x)/sizeof((x)[0])) 25 | 26 | static inline const CHAR16 *yes_no(BOOLEAN b) { 27 | return b ? L"yes" : L"no"; 28 | } 29 | 30 | EFI_STATUS parse_boolean(CHAR8 *v, BOOLEAN *b); 31 | 32 | UINT64 ticks_read(void); 33 | UINT64 ticks_freq(void); 34 | UINT64 time_usec(void); 35 | 36 | EFI_STATUS efivar_set(CHAR16 *name, CHAR16 *value, BOOLEAN persistent); 37 | EFI_STATUS efivar_set_raw(const EFI_GUID *vendor, CHAR16 *name, CHAR8 *buf, UINTN size, BOOLEAN persistent); 38 | EFI_STATUS efivar_set_int(CHAR16 *name, UINTN i, BOOLEAN persistent); 39 | VOID efivar_set_time_usec(CHAR16 *name, UINT64 usec); 40 | 41 | EFI_STATUS efivar_get(CHAR16 *name, CHAR16 **value); 42 | EFI_STATUS efivar_get_raw(const EFI_GUID *vendor, CHAR16 *name, CHAR8 **buffer, UINTN *size); 43 | EFI_STATUS efivar_get_int(CHAR16 *name, UINTN *i); 44 | 45 | CHAR8 *strchra(CHAR8 *s, CHAR8 c); 46 | CHAR16 *stra_to_path(CHAR8 *stra); 47 | CHAR16 *stra_to_str(CHAR8 *stra); 48 | 49 | INTN file_read(EFI_FILE_HANDLE dir, CHAR16 *name, UINTN off, UINTN size, CHAR8 **content); 50 | #endif 51 | -------------------------------------------------------------------------------- /test/splash.bmp: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/msekletar/systemd-boot/15d305f4dcc6f01fcd5f76afc2dc7ae48119cfee/test/splash.bmp -------------------------------------------------------------------------------- /test/test-efi-create-disk.sh: -------------------------------------------------------------------------------- 1 | #!/bin/bash -e 2 | 3 | # create GPT table with EFI System Partition 4 | rm -f test-efi-disk.img 5 | dd if=/dev/null of=test-efi-disk.img bs=1M seek=512 count=1 6 | parted --script test-efi-disk.img "mklabel gpt" "mkpart ESP fat32 1MiB 511MiB" "set 1 boot on" 7 | 8 | # create FAT32 file system 9 | LOOP=$(losetup --show -f -P test-efi-disk.img) 10 | mkfs.vfat -F32 ${LOOP}p1 11 | mkdir -p mnt 12 | mount ${LOOP}p1 mnt 13 | 14 | mkdir -p mnt/EFI/{BOOT,systemd} 15 | cp systemd-bootx64.efi mnt/EFI/BOOT/BOOTX64.efi 16 | 17 | [ -e /boot/shellx64.efi ] && cp /boot/shellx64.efi mnt/ 18 | 19 | mkdir mnt/EFI/Linux 20 | echo -n "foo=yes bar=no root=/dev/fakeroot debug rd.break=initqueue" > mnt/cmdline.txt 21 | objcopy \ 22 | --add-section .osrel=/etc/os-release --change-section-vma .osrel=0x20000 \ 23 | --add-section .cmdline=mnt/cmdline.txt --change-section-vma .cmdline=0x30000 \ 24 | --add-section .splash=test/splash.bmp --change-section-vma .splash=0x40000 \ 25 | --add-section .linux=/boot/$(cat /etc/machine-id)/$(uname -r)/linux --change-section-vma .linux=0x2000000 \ 26 | --add-section .initrd=/boot/$(cat /etc/machine-id)/$(uname -r)/initrd --change-section-vma .initrd=0x3000000 \ 27 | linuxx64.efi.stub mnt/EFI/Linux/linux-test.efi 28 | 29 | # install entries 30 | mkdir -p mnt/loader/entries 31 | echo -e "timeout 3\n" > mnt/loader/loader.conf 32 | echo -e "title Test\nefi /test\n" > mnt/loader/entries/test.conf 33 | echo -e "title Test2\nlinux /test2\noptions option=yes word number=1000 more\n" > mnt/loader/entries/test2.conf 34 | echo -e "title Test3\nlinux /test3\n" > mnt/loader/entries/test3.conf 35 | echo -e "title Test4\nlinux /test4\n" > mnt/loader/entries/test4.conf 36 | echo -e "title Test5\nefi /test5\n" > mnt/loader/entries/test5.conf 37 | echo -e "title Test6\nlinux /test6\n" > mnt/loader/entries/test6.conf 38 | 39 | sync 40 | umount mnt 41 | rmdir mnt 42 | losetup -d $LOOP 43 | --------------------------------------------------------------------------------