Linux kernel mirror (for testing) git.kernel.org/pub/scm/linux/kernel/git/torvalds/linux.git
kernel os linux

efi/fdt: Apply more cleanups

Apply a number of cleanups:

- Introduce fdt_setprop_*var() helper macros to simplify and shorten repetitive
sequences - this also makes it less likely that the wrong variable size is
passed in. This change makes a lot of the property-setting calls single-line
and easier to read.

- Harmonize comment style: capitalization, punctuation, whitespaces, etc.

- Fix some whitespace noise in the libstub Makefile which I happened to notice.

- Use the standard tabular initialization style:

- map.map = &runtime_map;
- map.map_size = &map_size;
- map.desc_size = &desc_size;
- map.desc_ver = &desc_ver;
- map.key_ptr = &mmap_key;
- map.buff_size = &buff_size;

+ map.map = &runtime_map;
+ map.map_size = &map_size;
+ map.desc_size = &desc_size;
+ map.desc_ver = &desc_ver;
+ map.key_ptr = &mmap_key;
+ map.buff_size = &buff_size;

- Use tabular structure definition for better readability.

- Make all pr*() lines single-line, even if they marginally exceed 80 cols - this
makes them visually less intrusive.

- Unbreak line breaks into single lines when the length exceeds 80 cols only
marginally, for better readability.

- Move assignment closer to the actual usage site.

- Plus some other smaller cleanups, spelling fixes, etc.

No change in functionality intended.

[ ardb: move changes to upstream libfdt into local header. ]

Signed-off-by: Ingo Molnar <mingo@kernel.org>
Signed-off-by: Ard Biesheuvel <ard.biesheuvel@linaro.org>
Cc: AKASHI Takahiro <takahiro.akashi@linaro.org>
Cc: Alexander Graf <agraf@suse.de>
Cc: Bjorn Andersson <bjorn.andersson@linaro.org>
Cc: Borislav Petkov <bp@alien8.de>
Cc: Heinrich Schuchardt <xypron.glpk@gmx.de>
Cc: Jeffrey Hugo <jhugo@codeaurora.org>
Cc: Lee Jones <lee.jones@linaro.org>
Cc: Leif Lindholm <leif.lindholm@linaro.org>
Cc: Linus Torvalds <torvalds@linux-foundation.org>
Cc: Matt Fleming <matt@codeblueprint.co.uk>
Cc: Peter Jones <pjones@redhat.com>
Cc: Peter Zijlstra <peterz@infradead.org>
Cc: Sai Praneeth Prakhya <sai.praneeth.prakhya@intel.com>
Cc: Thomas Gleixner <tglx@linutronix.de>
Cc: linux-efi@vger.kernel.org
Link: http://lkml.kernel.org/r/20190202094119.13230-6-ard.biesheuvel@linaro.org
Signed-off-by: Ingo Molnar <mingo@kernel.org>

+64 -58
+2 -2
drivers/firmware/efi/libstub/Makefile
··· 52 52 53 53 lib-$(CONFIG_ARM) += arm32-stub.o 54 54 lib-$(CONFIG_ARM64) += arm64-stub.o 55 - CFLAGS_arm64-stub.o := -DTEXT_OFFSET=$(TEXT_OFFSET) 55 + CFLAGS_arm64-stub.o := -DTEXT_OFFSET=$(TEXT_OFFSET) 56 56 57 57 # 58 58 # arm64 puts the stub in the kernel proper, which will unnecessarily retain all ··· 89 89 cmd_stubcopy = if $(STRIP) --strip-debug $(STUBCOPY_RM-y) -o $@ $<; \ 90 90 then if $(OBJDUMP) -r $@ | grep $(STUBCOPY_RELOC-y); \ 91 91 then (echo >&2 "$@: absolute symbol references not allowed in the EFI stub"; \ 92 - rm -f $@; /bin/false); \ 92 + rm -f $@; /bin/false); \ 93 93 else $(OBJCOPY) $(STUBCOPY_FLAGS-y) $< $@; fi \ 94 94 else /bin/false; fi 95 95
+11
drivers/firmware/efi/libstub/efistub.h
··· 64 64 65 65 efi_status_t efi_random_get_seed(efi_system_table_t *sys_table_arg); 66 66 67 + /* Helper macros for the usual case of using simple C variables: */ 68 + #ifndef fdt_setprop_inplace_var 69 + #define fdt_setprop_inplace_var(fdt, node_offset, name, var) \ 70 + fdt_setprop_inplace((fdt), (node_offset), (name), &(var), sizeof(var)) 71 + #endif 72 + 73 + #ifndef fdt_setprop_var 74 + #define fdt_setprop_var(fdt, node_offset, name, var) \ 75 + fdt_setprop((fdt), (node_offset), (name), &(var), sizeof(var)) 76 + #endif 77 + 67 78 #endif
+51 -56
drivers/firmware/efi/libstub/fdt.c
··· 26 26 offset = fdt_path_offset(fdt, "/"); 27 27 /* Set the #address-cells and #size-cells values for an empty tree */ 28 28 29 - fdt_setprop_u32(fdt, offset, "#address-cells", 30 - EFI_DT_ADDR_CELLS_DEFAULT); 31 - 32 - fdt_setprop_u32(fdt, offset, "#size-cells", EFI_DT_SIZE_CELLS_DEFAULT); 29 + fdt_setprop_u32(fdt, offset, "#address-cells", EFI_DT_ADDR_CELLS_DEFAULT); 30 + fdt_setprop_u32(fdt, offset, "#size-cells", EFI_DT_SIZE_CELLS_DEFAULT); 33 31 } 34 32 35 33 static efi_status_t update_fdt(efi_system_table_t *sys_table, void *orig_fdt, ··· 40 42 u32 fdt_val32; 41 43 u64 fdt_val64; 42 44 43 - /* Do some checks on provided FDT, if it exists*/ 45 + /* Do some checks on provided FDT, if it exists: */ 44 46 if (orig_fdt) { 45 47 if (fdt_check_header(orig_fdt)) { 46 48 pr_efi_err(sys_table, "Device Tree header not valid!\n"); ··· 48 50 } 49 51 /* 50 52 * We don't get the size of the FDT if we get if from a 51 - * configuration table. 53 + * configuration table: 52 54 */ 53 55 if (orig_fdt_size && fdt_totalsize(orig_fdt) > orig_fdt_size) { 54 56 pr_efi_err(sys_table, "Truncated device tree! foo!\n"); ··· 62 64 status = fdt_create_empty_tree(fdt, new_fdt_size); 63 65 if (status == 0) { 64 66 /* 65 - * Any failure from the following function is non 66 - * critical 67 + * Any failure from the following function is 68 + * non-critical: 67 69 */ 68 70 fdt_update_cell_size(sys_table, fdt); 69 71 } ··· 84 86 if (node < 0) { 85 87 node = fdt_add_subnode(fdt, 0, "chosen"); 86 88 if (node < 0) { 87 - status = node; /* node is error code when negative */ 89 + /* 'node' is an error code when negative: */ 90 + status = node; 88 91 goto fdt_set_fail; 89 92 } 90 93 } 91 94 92 - if ((cmdline_ptr != NULL) && (strlen(cmdline_ptr) > 0)) { 95 + if (cmdline_ptr != NULL && strlen(cmdline_ptr) > 0) { 93 96 status = fdt_setprop(fdt, node, "bootargs", cmdline_ptr, 94 97 strlen(cmdline_ptr) + 1); 95 98 if (status) ··· 102 103 u64 initrd_image_end; 103 104 u64 initrd_image_start = cpu_to_fdt64(initrd_addr); 104 105 105 - status = fdt_setprop(fdt, node, "linux,initrd-start", 106 - &initrd_image_start, sizeof(u64)); 106 + status = fdt_setprop_var(fdt, node, "linux,initrd-start", initrd_image_start); 107 107 if (status) 108 108 goto fdt_set_fail; 109 + 109 110 initrd_image_end = cpu_to_fdt64(initrd_addr + initrd_size); 110 - status = fdt_setprop(fdt, node, "linux,initrd-end", 111 - &initrd_image_end, sizeof(u64)); 111 + status = fdt_setprop_var(fdt, node, "linux,initrd-end", initrd_image_end); 112 112 if (status) 113 113 goto fdt_set_fail; 114 114 } ··· 115 117 /* Add FDT entries for EFI runtime services in chosen node. */ 116 118 node = fdt_subnode_offset(fdt, 0, "chosen"); 117 119 fdt_val64 = cpu_to_fdt64((u64)(unsigned long)sys_table); 118 - status = fdt_setprop(fdt, node, "linux,uefi-system-table", 119 - &fdt_val64, sizeof(fdt_val64)); 120 + 121 + status = fdt_setprop_var(fdt, node, "linux,uefi-system-table", fdt_val64); 120 122 if (status) 121 123 goto fdt_set_fail; 122 124 123 125 fdt_val64 = U64_MAX; /* placeholder */ 124 - status = fdt_setprop(fdt, node, "linux,uefi-mmap-start", 125 - &fdt_val64, sizeof(fdt_val64)); 126 + 127 + status = fdt_setprop_var(fdt, node, "linux,uefi-mmap-start", fdt_val64); 126 128 if (status) 127 129 goto fdt_set_fail; 128 130 129 131 fdt_val32 = U32_MAX; /* placeholder */ 130 - status = fdt_setprop(fdt, node, "linux,uefi-mmap-size", 131 - &fdt_val32, sizeof(fdt_val32)); 132 + 133 + status = fdt_setprop_var(fdt, node, "linux,uefi-mmap-size", fdt_val32); 132 134 if (status) 133 135 goto fdt_set_fail; 134 136 135 - status = fdt_setprop(fdt, node, "linux,uefi-mmap-desc-size", 136 - &fdt_val32, sizeof(fdt_val32)); 137 + status = fdt_setprop_var(fdt, node, "linux,uefi-mmap-desc-size", fdt_val32); 137 138 if (status) 138 139 goto fdt_set_fail; 139 140 140 - status = fdt_setprop(fdt, node, "linux,uefi-mmap-desc-ver", 141 - &fdt_val32, sizeof(fdt_val32)); 141 + status = fdt_setprop_var(fdt, node, "linux,uefi-mmap-desc-ver", fdt_val32); 142 142 if (status) 143 143 goto fdt_set_fail; 144 144 ··· 146 150 efi_status = efi_get_random_bytes(sys_table, sizeof(fdt_val64), 147 151 (u8 *)&fdt_val64); 148 152 if (efi_status == EFI_SUCCESS) { 149 - status = fdt_setprop(fdt, node, "kaslr-seed", 150 - &fdt_val64, sizeof(fdt_val64)); 153 + status = fdt_setprop_var(fdt, node, "kaslr-seed", fdt_val64); 151 154 if (status) 152 155 goto fdt_set_fail; 153 156 } else if (efi_status != EFI_NOT_FOUND) { ··· 154 159 } 155 160 } 156 161 157 - /* shrink the FDT back to its minimum size */ 162 + /* Shrink the FDT back to its minimum size: */ 158 163 fdt_pack(fdt); 159 164 160 165 return EFI_SUCCESS; ··· 177 182 return EFI_LOAD_ERROR; 178 183 179 184 fdt_val64 = cpu_to_fdt64((unsigned long)*map->map); 180 - err = fdt_setprop_inplace(fdt, node, "linux,uefi-mmap-start", 181 - &fdt_val64, sizeof(fdt_val64)); 185 + 186 + err = fdt_setprop_inplace_var(fdt, node, "linux,uefi-mmap-start", fdt_val64); 182 187 if (err) 183 188 return EFI_LOAD_ERROR; 184 189 185 190 fdt_val32 = cpu_to_fdt32(*map->map_size); 186 - err = fdt_setprop_inplace(fdt, node, "linux,uefi-mmap-size", 187 - &fdt_val32, sizeof(fdt_val32)); 191 + 192 + err = fdt_setprop_inplace_var(fdt, node, "linux,uefi-mmap-size", fdt_val32); 188 193 if (err) 189 194 return EFI_LOAD_ERROR; 190 195 191 196 fdt_val32 = cpu_to_fdt32(*map->desc_size); 192 - err = fdt_setprop_inplace(fdt, node, "linux,uefi-mmap-desc-size", 193 - &fdt_val32, sizeof(fdt_val32)); 197 + 198 + err = fdt_setprop_inplace_var(fdt, node, "linux,uefi-mmap-desc-size", fdt_val32); 194 199 if (err) 195 200 return EFI_LOAD_ERROR; 196 201 197 202 fdt_val32 = cpu_to_fdt32(*map->desc_ver); 198 - err = fdt_setprop_inplace(fdt, node, "linux,uefi-mmap-desc-ver", 199 - &fdt_val32, sizeof(fdt_val32)); 203 + 204 + err = fdt_setprop_inplace_var(fdt, node, "linux,uefi-mmap-desc-ver", fdt_val32); 200 205 if (err) 201 206 return EFI_LOAD_ERROR; 202 207 ··· 204 209 } 205 210 206 211 #ifndef EFI_FDT_ALIGN 207 - #define EFI_FDT_ALIGN EFI_PAGE_SIZE 212 + # define EFI_FDT_ALIGN EFI_PAGE_SIZE 208 213 #endif 209 214 210 215 struct exit_boot_struct { 211 - efi_memory_desc_t *runtime_map; 212 - int *runtime_entry_count; 213 - void *new_fdt_addr; 216 + efi_memory_desc_t *runtime_map; 217 + int *runtime_entry_count; 218 + void *new_fdt_addr; 214 219 }; 215 220 216 221 static efi_status_t exit_boot_func(efi_system_table_t *sys_table_arg, ··· 230 235 } 231 236 232 237 #ifndef MAX_FDT_SIZE 233 - #define MAX_FDT_SIZE SZ_2M 238 + # define MAX_FDT_SIZE SZ_2M 234 239 #endif 235 240 236 241 /* ··· 261 266 unsigned long mmap_key; 262 267 efi_memory_desc_t *memory_map, *runtime_map; 263 268 efi_status_t status; 264 - int runtime_entry_count = 0; 269 + int runtime_entry_count; 265 270 struct efi_boot_memmap map; 266 271 struct exit_boot_struct priv; 267 272 268 - map.map = &runtime_map; 269 - map.map_size = &map_size; 270 - map.desc_size = &desc_size; 271 - map.desc_ver = &desc_ver; 272 - map.key_ptr = &mmap_key; 273 - map.buff_size = &buff_size; 273 + map.map = &runtime_map; 274 + map.map_size = &map_size; 275 + map.desc_size = &desc_size; 276 + map.desc_ver = &desc_ver; 277 + map.key_ptr = &mmap_key; 278 + map.buff_size = &buff_size; 274 279 275 280 /* 276 281 * Get a copy of the current memory map that we will use to prepare ··· 284 289 return status; 285 290 } 286 291 287 - pr_efi(sys_table, 288 - "Exiting boot services and installing virtual address map...\n"); 292 + pr_efi(sys_table, "Exiting boot services and installing virtual address map...\n"); 289 293 290 294 map.map = &memory_map; 291 295 status = efi_high_alloc(sys_table, MAX_FDT_SIZE, EFI_FDT_ALIGN, 292 296 new_fdt_addr, max_addr); 293 297 if (status != EFI_SUCCESS) { 294 - pr_efi_err(sys_table, 295 - "Unable to allocate memory for new device tree.\n"); 298 + pr_efi_err(sys_table, "Unable to allocate memory for new device tree.\n"); 296 299 goto fail; 297 300 } 298 301 ··· 311 318 goto fail_free_new_fdt; 312 319 } 313 320 314 - priv.runtime_map = runtime_map; 315 - priv.runtime_entry_count = &runtime_entry_count; 316 - priv.new_fdt_addr = (void *)*new_fdt_addr; 317 - status = efi_exit_boot_services(sys_table, handle, &map, &priv, 318 - exit_boot_func); 321 + runtime_entry_count = 0; 322 + priv.runtime_map = runtime_map; 323 + priv.runtime_entry_count = &runtime_entry_count; 324 + priv.new_fdt_addr = (void *)*new_fdt_addr; 325 + 326 + status = efi_exit_boot_services(sys_table, handle, &map, &priv, exit_boot_func); 319 327 320 328 if (status == EFI_SUCCESS) { 321 329 efi_set_virtual_address_map_t *svam; ··· 357 363 358 364 fail: 359 365 sys_table->boottime->free_pool(runtime_map); 366 + 360 367 return EFI_LOAD_ERROR; 361 368 } 362 369