interplane.Get Omega
Read in your language

aienux

Aienux, the AienOS kernel: boot, scheduling, IPC and capability boundaries. Pre-alpha; host, emulator and physical evidence are labelled separately.

Brought by drakestapleton

branch main Branches Tags History

aienux / vendor / uefi-raw / api_guidelines.md

155 lines · 7203 bytes Raw Blame History Rendered
1# API Guidelines
2
3The `uefi-raw` crate should closely match the definitions in the [UEFI
4Specification], with only some light changes to make it more friendly for use in
5Rust (e.g. casing follows Rust's conventions and modules are used to provide
6some hierarchy). Where the spec is inconsistent or inaccurate, the EDK2 headers
7and sources are consulted.
8
9This document describes the API rules in detail. Some of these rules can be
10checked with `cargo xtask check-raw`, and that check is run automatically in CI
11as well. Other rules require human verification.
12
13If you are contributing to this crate and run into any problems, such as a case
14that isn't covered by the rules, or a case where following the rules seems like
15it will lead to a bad API, don't hesitate to let us know (e.g. by filing an
16[issue]).
17
18## Naming
19
20Type names should match the corresponding spec names, but drop the `EFI_` prefix
21and change to `UpperCamelCase` to match Rust's convention. For example,
22`EFI_LOAD_FILE_PROTOCOL` becomes `LoadFileProtocol`.
23
24Struct field names and function parameter names should match the corresponding
25spec names, but change the case to `snake_case` to match Rust's convention.
26
27When defining a type that isn't part of the spec (for example, a `bitflags!`
28type that represents a collection of constants in the spec), prefix the name
29with a closely-associated type that is defined in the spec. For example, mode
30constants for a `FooBarProtocol` could be collected into a `FooBarMode` type.
31
32It's OK to introduce minor naming changes from the specification where it
33improves clarity.
34
35## Layout
36
37All types must be `repr(C)`, `repr(C, packed)`, `repr(C, align(N))`, or
38`repr(transparent)`.
39
40Types created with the `bitflags!` macro must set `repr(transparent)`.
41
42### Dynamically Sized Types
43
44Some types in the spec end with a variable-length array. It's possible to
45represent these as [Dynamically Sized Types], but that should be left to
46higher-level APIs. In this crate, add a zero-length array at the end of the
47struct to represent the field. For example, if a struct in the spec ends with
48`CHAR16 Name[];`, represent that in Rust with `name: [Char16; 0]`.
49
50This pattern of using a `&Header` to work with dynamically-sized data is
51rejected by the Stacked Borrows model, but allowed by Tree Borrows. See [UCG
52issue 256] for more info.
53
54## Visibility
55
56Everything must have `pub` visibility.
57
58## Constants
59
60Use associated constants where possible instead of top-level constants.
61
62Protocols must have an associated `GUID` constant, for example:
63
64```rust
65impl RngProtocol {
66 pub const GUID: Guid = guid!("3152bca5-eade-433d-862e-c01cdc291f44");
67}
68```
69
70## Pointers
71
72Use pointers (`*const`/`*mut`) instead of references (`&`/`&mut`).
73
74Function pointers must be `unsafe` and have an explicit ABI (almost always
75`efiapi`). If a function pointer field can be null it must be wrapped in
76`Option`. Most function pointer fields do not need to allow null pointers
77though, unless the spec says otherwise.
78
79### Mutability
80
81Pointer mutability (`*mut` vs `*const`) is not a UB concern the way reference
82mutability is. In general, it is not UB to `cast_mut` a const pointer and write
83through it. So picking `*mut` vs `*const` is more about semantics.
84
85Two questions decide the type: who writes through the pointer, and who owns the
86pointee. `*mut` means "written by the firmware" or "owned by the caller, who
87must free it", whereas `*const` means read-only for both sides.
88
89Pointer fields in structs follow the same rule. Pointers to firmware-owned
90structures that consumers must only read, such as the `Mode` pointer of a
91protocol, are generally still `*mut`. Callers that just read such structure can
92use `cast_const()`.
93
94In function parameters, pick between `*const` and `*mut` based on how the
95parameter is described in the spec. An `OUT` or `IN OUT` pointer must be
96`*mut`. An `IN` pointer is `*const` if the parameter is described as being
97source data, but `*mut` if the spec text says that the pointee is written, for
98example `Buffer` of `EFI_LOAD_FILE_PROTOCOL.LoadFile()`.
99
100### Mapping of C Qualifiers
101
102The spec annotates parameters with `IN`, `OUT`, `IN OUT`, `CONST` and
103`OPTIONAL`. In the following tables, `T` is a data type and `F` is a function
104pointer type such as `EFI_EVENT_NOTIFY`.
105
106| C | Rust |
107|----------------------------------|------------------------------------------|
108| `IN T Name` | `T` |
109| `IN T *Name`, `IN CONST T *Name` | `*const T` |
110| `IN T *Name OPTIONAL` | `*const T` |
111| `OUT T *Name`, `IN OUT T *Name` | `*mut T` |
112| `OUT T *Name OPTIONAL` | `*mut T` |
113| `IN F Name` | `unsafe extern "efiapi" fn(...)` |
114| `IN F Name OPTIONAL` | `Option<unsafe extern "efiapi" fn(...)>` |
115
116`OPTIONAL` only changes the Rust type for function pointers: a raw pointer can
117already be null, whereas a bare `fn` pointer cannot, so it needs `Option`.
118
119Buffers (`VOID *Buffer` plus a length) follow the same rule: `*const c_void` if
120the firmware reads them, `*mut c_void` if it writes them. Pointers to pointers
121depend on who owns each level:
122
123| C | Rust | Explanation |
124|--------------------------|------------------------|---------------------------------------|
125| `OUT T **Name` | `*mut *mut T` | callee allocates, caller frees |
126| `OUT T **Name` | `*mut *const T` | points into firmware-owned memory |
127| `OUT T **Name` | `*const *mut T` | caller's array, firmware fills it |
128| `IN OUT VOID *Name[N]` | `*const *mut c_void` | caller's buffers, firmware fills them |
129| `IN CONST CHAR16 **Name` | `*const *const Char16` | read-only strings |
130
131This covers arrays too; the count parameter says how many elements the caller
132owns, as in `LocateHandleBuffer`. `ProtocolsPerHandle` combines two rows, a
133callee-allocated array of pointers into firmware-owned memory, hence
134`*mut *mut *const Guid`.
135
136The same rule applies to return values: a pool allocation the caller must free
137is returned as `*mut T`, but firmware-owned memory as `*const T`.
138
139The `This` parameter of a protocol function is `*const Self` if the function
140only reads state, such as `QueryMode()` of `EFI_SIMPLE_TEXT_OUTPUT_PROTOCOL`.
141If the function logically modifies state, such as `Reset()` or `SetMode()`, it
142is `*mut Self`, even if the protocol structure itself is not written.
143
144## Allowed top-level items
145
146The allowed top-level items are `const`, `impl`, `macro`, `struct`, `type`, and
147`union`.
148
149Rust `enum`s are not allowed; use the `bitflags!` or `newtype_enum!` macros
150instead.
151
152[UEFI Specification]: https://uefi.org/specifications
153[issue]: https://github.com/rust-osdev/uefi-rs/issues/new
154[Dynamically Sized Types]: https://doc.rust-lang.org/reference/dynamically-sized-types.html
155[UCG issue 256]: https://github.com/rust-lang/unsafe-code-guidelines/issues/256