1.\" 2.\" Copyright (c) 2024 Netflix, Inc. 3.\" 4.\" SPDX-License-Identifier: BSD-2-Clause 5.\" 6.Dd February 6, 2024 7.Dt LOADER.LUA 8 8.Os 9.Sh NAME 10.Nm loader.lua 11.Nd Fx Lua loader module 12.Sh DESCRIPTION 13The built-in Lua bindings for the 14.Fx 15boot loaders using the Lua interpreter 16are available via the 17.Ic loader 18table. 19.Pp 20The 21.Ic loader 22table is always available in Lua scripts. 23There is no need to require it like other loader-specific modules. 24.Ss Exported Variables 25The following variables are provided by the Lua interpreter in the 26.Nm loader 27table: 28.Bl -tag -width machine_arch 29.It Ic machine 30The target's 31.Va hw.machine 32.Xr sysctl 8 33value. 34.It Ic machine_arch 35The target's 36.Va hw.machine_arch 37.Xr sysctl 8 38value. 39Some boot loaders are 32-bit applications that then load a 64-bit 40kernel. 41In these cases, 42.Ic machine_arch 43represents the 32-bit architecture, not the 64-bit architecture. 44.It Ic lua_path 45The current lua loading path. 46.It Ic version 47The version of the boot program. 48.El 49.Ss Exported Functions 50The following functions are exported in the 51.Nm loader 52table. 53.Bl -tag -width term_putimage 54.It Fn delay usec 55Delay for 56.Va usec 57microseconds. 58.It Fn command_error 59Returns the error string from the last command to fail. 60.It Fn command argc argv 61Like 62.Fn perform 63but the arguments are already parsed onto the stack. 64.It Fn interpret str 65Execute the loader builtin command 66.Va str 67as if it were typed by the user. 68This will first try to execute 69.Va str 70as Lua. 71If that fails, it will attempt to execute it as a cli command, 72including those defined by the 73.Xr cli.lua 8 74mechanism. 75If that fails, it will attempt to execute it as a builtin command 76and return the same values as 77.Fn perform . 78.It Fn parse str 79Parses the command 80.Va str 81into its words and return those words on the stack. 82.It Fn getenv name 83Obtains the value of the environment variable 84.Va name . 85.It Fn has_command cmd 86returns 87.Va true 88if 89.Va commmand 90is present in the interpreter as a builtin. 91Otherwise it returns 92.Va nil 93and an error string. 94It does not check the 95.Dq cli 96table to see if a user defined command has been created. 97.It Fn has_feature feature 98returns 99.Va true 100if the 101.Va feature 102is enabled. 103Otherwise it returns 104.Va nil 105and an error string. 106.It Fn perform str 107Execute the loader builtin command 108.Va str . 109Returns the result of the command, one of the following values: 110.Bl -tag -width loader -offset indent 111.It loader.CMD_OK 112The command completed successfully. 113.It loader.CMD_WARN 114The command was successful, but the user stopped its output 115prematurely. 116.It loader.CMD_ERROR 117The command did not complete successfully. 118Use 119.Va command_error 120to retrieve the error. 121.It loader.CMD_CRIT 122The command returned a critical error that was already printed. 123.It loader.CMD_FATAL 124The command determined continuation was not possible 125and the loader panicked. 126In practice, though, 127.Fn panic 128does not return. 129.El 130.It Fn printc str 131Outputs the string using the loader's 132.Fn putchar 133function. 134This function is also available globally as 135.Fn printc . 136.It Fn setenv name value 137Insert or reset the environment variable 138.Va name 139into the loader's environment list. 140If no environment variable with this name exists, one is created. 141If one exists, its value is replaced. 142.It Fn time 143Returns the loader's notion of time, in seconds since 1970. 144The value of loader's notion varies somewhat between different loading 145environments. 146.It Fn unsetenv name 147Removes the environment variable 148.Va name 149from the loader's environment list. 150.It Fn fb_bezier x0 y0 x1 y1 x2 y2 width 151Draw a bezier curve through the points 152.Pq Va x0 , Va y0 , 153.Pq Va x1 , Va y1 , 154and 155.Pq Va x2 , Va y2 156of width 157.Va width . 158The units are in pixels and have an origin of 159.Pq 0 , 0 . 160.It Fn fb_drawrect x0 y0 x1 y1 fill 161Fill in a rectangle with the pixel 162.Va fill 163with the corners 164.Pq Va x0 , Va y0 165and 166.Pq Va x1 , Va y1 . 167The units are in pixels and have an origin of 168.Pq 0 , 0 . 169.It Fn fb_line x0 y0 x1 y1 width 170Draw a line from 171.Pq Va x0 , Va y0 172to 173.Pq Va x1 , Va y1 174with a width of 175.Va width . 176The units are in pixels and have an origin of 177.Pq 0 , 0 . 178.It Fn fb_putimage name x0 y0 x1 y1 f 179Load the PNG file 180.Va name 181and place it in the rectangle 182with the corners 183.Pq Va x0 , Va y0 184and 185.Pq Va x1 , Va y1 186and fill with pixel 187.Va f . 188The units are in pixels and have an origin of 189.Pq 0 , 0 . 190.It Fn fb_set_pixel x y 191Sets the pixel at 192.Pq Va x , Va y . 193The units are in pixels and have an origin of 194.Pq 0 , 0 . 195.It Fn term_drawrect x0 y0 x1 y1 196Draw the outline of a rectangle with the text coordinate corners of 197.Pq Va x0 , Va y0 198and 199.Pq Va x1 , Va y1 . 200The units are in character cells and have an origin of 201.Pq 1 , 1 . 202.It Fn term_putimage name x0 y0 x1 y1 f 203Load the PNG file 204.Va name 205and place it in the rectangle 206with the text coordinate corners 207.Pq Va x0 , Va y0 208and 209.Pq Va x1 , Va y1 210and fill with pixel 211.Va f . 212The units are in character cells and have an origin of 213.Pq 1 , 1 . 214.El 215.Pp 216The functions starting with 217.Fn fb_ 218and 219.Fn term_ 220are optional. 221They should only be used if they are non-nil and if 222.Fn core.isFramebufferConsole 223is true. 224.Ss Default File 225In addition, the Lua interpreters start with the file 226.Pa /boot/lua/loader.lua 227when they start to boot the system. 228The default one will fixup the screen, load the configuration files, check for a 229password, and then load the menu or load the kernel file and then return. 230If autoboot is enabled, the loaded files will boot. 231.Sh SEE ALSO 232.Xr loader.conf 5 , 233.Xr core.lua 8 , 234.Xr loader 8 , 235.Xr sysctl 8 236.Sh AUTHORS 237The 238.Nm 239man page was written by 240.An Warner Losh Aq Mt imp@FreeBSD.org . 241.Sh BUGS 242.Fn command 243and 244.Fn perform 245should return a tuple when there's 246.Va CMD_ERROR 247or worse. 248