Skip to content

Commit 3543217

Browse files
travisgzhangxp1998
authored andcommitted
[lib][uefi] fix a few warnings and a little code tidying
GCC 14 is quite picky about warnings, probably more so than clang. -Fix a bunch of printf warnings. Pointers should be printed with %p. -Move some stuff into an anonymous namespace. -Worked around GCC really not liking reinterpret_casting from one function pointer type to another. Fiddled with it a bit and eventually settled on casting the function pointer to const void * and passing it through.
1 parent 7fff351 commit 3543217

File tree

5 files changed

+63
-54
lines changed

5 files changed

+63
-54
lines changed

lib/uefi/boot_service_provider.cpp

Lines changed: 32 additions & 32 deletions
Original file line numberDiff line numberDiff line change
@@ -421,10 +421,10 @@ EfiStatus read_blocks(EfiBlockIoProtocol *self, uint32_t media_id, uint64_t lba,
421421
return END_OF_MEDIA;
422422
}
423423

424-
const auto bytes_read =
424+
const size_t bytes_read =
425425
call_with_stack(interface->io_stack, bio_read_block, dev, buffer, lba,
426426
buffer_size / dev->block_size);
427-
if (bytes_read != static_cast<ssize_t>(buffer_size)) {
427+
if (bytes_read != buffer_size) {
428428
printf("Failed to read %ld bytes from %s\n", buffer_size, dev->name);
429429
return DEVICE_ERROR;
430430
}
@@ -449,7 +449,7 @@ EfiStatus open_block_device(EfiHandle handle, void **intf) {
449449
vmm_alloc(vmm_get_kernel_aspace(), "uefi_io_stack", kIoStackSize, &io_stack,
450450
PAGE_SIZE_SHIFT, 0, 0);
451451
}
452-
printf("%s(%s)\n", __FUNCTION__, handle);
452+
printf("%s(%p)\n", __FUNCTION__, handle);
453453
const auto interface = reinterpret_cast<EfiBlockIoInterface *>(
454454
mspace_malloc(get_mspace(), sizeof(EfiBlockIoInterface)));
455455
memset(interface, 0, sizeof(EfiBlockIoInterface));
@@ -504,7 +504,7 @@ EFI_STATUS efi_dt_fixup(struct EfiDtFixupProtocol *self, void *fdt,
504504
EfiStatus fixup_kernel_commandline(struct GblEfiOsConfigurationProtocol *self,
505505
const char *command_line, char *fixup,
506506
size_t *fixup_buffer_size) {
507-
printf("%s(0x%lx, \"%s\")\n", __FUNCTION__, self, command_line);
507+
printf("%s(%p, \"%s\")\n", __FUNCTION__, self, command_line);
508508
*fixup_buffer_size = 0;
509509
return SUCCESS;
510510
}
@@ -513,7 +513,7 @@ EfiStatus fixup_kernel_commandline(struct GblEfiOsConfigurationProtocol *self,
513513
EfiStatus fixup_bootconfig(struct GblEfiOsConfigurationProtocol *self,
514514
const char *bootconfig, size_t size, char *fixup,
515515
size_t *fixup_buffer_size) {
516-
printf("%s(0x%lx, %s, %lu, %lu)\n", __FUNCTION__, self, bootconfig, size,
516+
printf("%s(%p, %s, %lu, %lu)\n", __FUNCTION__, self, bootconfig, size,
517517
*fixup_buffer_size);
518518
constexpr auto &&to_add = "\nandroidboot.fstab_suffix=cf.f2fs."
519519
"hctr2\nandroidboot.boot_devices=4010000000.pcie";
@@ -532,7 +532,7 @@ EfiStatus fixup_bootconfig(struct GblEfiOsConfigurationProtocol *self,
532532
EfiStatus select_device_trees(struct GblEfiOsConfigurationProtocol *self,
533533
GblEfiVerifiedDeviceTree *device_trees,
534534
size_t num_device_trees) {
535-
printf("%s(0x%lx, %lx, %lu)\n", __FUNCTION__, self, device_trees,
535+
printf("%s(%p, %p %lu)\n", __FUNCTION__, self, device_trees,
536536
num_device_trees);
537537
return UNSUPPORTED;
538538
}
@@ -547,29 +547,29 @@ EfiStatus open_protocol(EfiHandle handle, const EfiGuid *protocol, void **intf,
547547
interface->parent_handle = handle;
548548
interface->image_base = handle;
549549
*intf = interface;
550-
printf("%s(LOADED_IMAGE_PROTOCOL_GUID, handle=0x%lx, agent_handle=0x%lx, "
551-
"controller_handle=0x%lx, attr=0x%x)\n",
550+
printf("%s(LOADED_IMAGE_PROTOCOL_GUID, handle=%p, agent_handle=%p, "
551+
"controller_handle=%p, attr=0x%x)\n",
552552
__FUNCTION__, handle, agent_handle, controller_handle, attr);
553553
return SUCCESS;
554554
} else if (guid_eq(protocol, EFI_DEVICE_PATH_PROTOCOL_GUID)) {
555555
printf(
556-
"%s(EFI_DEVICE_PATH_PROTOCOL_GUID, handle=0x%lx, agent_handle=0x%lx, "
557-
"controller_handle=0x%lx, attr=0x%x)\n",
556+
"%s(EFI_DEVICE_PATH_PROTOCOL_GUID, handle=%p, agent_handle=%p, "
557+
"controller_handle=%p, attr=0x%x)\n",
558558
__FUNCTION__, handle, agent_handle, controller_handle, attr);
559559
return UNSUPPORTED;
560560
} else if (guid_eq(protocol, EFI_BLOCK_IO_PROTOCOL_GUID)) {
561-
printf("%s(EFI_BLOCK_IO_PROTOCOL_GUID, handle=0x%lx, agent_handle=0x%lx, "
562-
"controller_handle=0x%lx, attr=0x%x)\n",
561+
printf("%s(EFI_BLOCK_IO_PROTOCOL_GUID, handle=%p, agent_handle=%p, "
562+
"controller_handle=%p, attr=0x%x)\n",
563563
__FUNCTION__, handle, agent_handle, controller_handle, attr);
564564
return open_block_device(handle, intf);
565565
} else if (guid_eq(protocol, EFI_BLOCK_IO2_PROTOCOL_GUID)) {
566-
printf("%s(EFI_BLOCK_IO2_PROTOCOL_GUID, handle=0x%lx, agent_handle=0x%lx, "
567-
"controller_handle=0x%lx, attr=0x%x)\n",
566+
printf("%s(EFI_BLOCK_IO2_PROTOCOL_GUID, handle=%p, agent_handle=%p, "
567+
"controller_handle=%p, attr=0x%x)\n",
568568
__FUNCTION__, handle, agent_handle, controller_handle, attr);
569569
return UNSUPPORTED;
570570
} else if (guid_eq(protocol, EFI_DT_FIXUP_PROTOCOL_GUID)) {
571-
printf("%s(EFI_DT_FIXUP_PROTOCOL_GUID, handle=0x%lx, agent_handle=0x%lx, "
572-
"controller_handle=0x%lx, attr=0x%x)\n",
571+
printf("%s(EFI_DT_FIXUP_PROTOCOL_GUID, handle=%p, agent_handle=%p, "
572+
"controller_handle=%p, attr=0x%x)\n",
573573
__FUNCTION__, handle, agent_handle, controller_handle, attr);
574574
if (intf != nullptr) {
575575
EfiDtFixupProtocol *fixup = nullptr;
@@ -584,9 +584,9 @@ EfiStatus open_protocol(EfiHandle handle, const EfiGuid *protocol, void **intf,
584584
}
585585
return SUCCESS;
586586
} else if (guid_eq(protocol, EFI_GBL_OS_CONFIGURATION_PROTOCOL_GUID)) {
587-
printf("%s(EFI_GBL_OS_CONFIGURATION_PROTOCOL_GUID, handle=0x%lx, "
588-
"agent_handle=0x%lx, "
589-
"controller_handle=0x%lx, attr=0x%x)\n",
587+
printf("%s(EFI_GBL_OS_CONFIGURATION_PROTOCOL_GUID, handle=%p, "
588+
"agent_handle=%p, "
589+
"controller_handle=%p, attr=0x%x)\n",
590590
__FUNCTION__, handle, agent_handle, controller_handle, attr);
591591
GblEfiOsConfigurationProtocol *config = nullptr;
592592
allocate_pool(BOOT_SERVICES_DATA, sizeof(*config),
@@ -610,24 +610,24 @@ EfiStatus open_protocol(EfiHandle handle, const EfiGuid *protocol, void **intf,
610610
EfiStatus close_protocol(EfiHandle handle, const EfiGuid *protocol,
611611
EfiHandle agent_handle, EfiHandle controller_handle) {
612612
if (guid_eq(protocol, LOADED_IMAGE_PROTOCOL_GUID)) {
613-
printf("%s(LOADED_IMAGE_PROTOCOL_GUID, handle=0x%lx, agent_handle=0x%lx, "
614-
"controller_handle=0x%lx)\n",
613+
printf("%s(LOADED_IMAGE_PROTOCOL_GUID, handle=%p, agent_handle=%p, "
614+
"controller_handle=%p)\n",
615615
__FUNCTION__, handle, agent_handle, controller_handle);
616616
return SUCCESS;
617617
} else if (guid_eq(protocol, EFI_DEVICE_PATH_PROTOCOL_GUID)) {
618618
printf(
619-
"%s(EFI_DEVICE_PATH_PROTOCOL_GUID, handle=0x%lx, agent_handle=0x%lx, "
620-
"controller_handle=0x%lx)\n",
619+
"%s(EFI_DEVICE_PATH_PROTOCOL_GUID, handle=%p, agent_handle=%p, "
620+
"controller_handle=%p)\n",
621621
__FUNCTION__, handle, agent_handle, controller_handle);
622622
return SUCCESS;
623623
} else if (guid_eq(protocol, EFI_BLOCK_IO_PROTOCOL_GUID)) {
624-
printf("%s(EFI_BLOCK_IO_PROTOCOL_GUID, handle=0x%lx, agent_handle=0x%lx, "
625-
"controller_handle=0x%lx)\n",
624+
printf("%s(EFI_BLOCK_IO_PROTOCOL_GUID, handle=%p, agent_handle=%p, "
625+
"controller_handle=%p)\n",
626626
__FUNCTION__, handle, agent_handle, controller_handle);
627627
return SUCCESS;
628628
} else if (guid_eq(protocol, EFI_DT_FIXUP_PROTOCOL_GUID)) {
629-
printf("%s(EFI_DT_FIXUP_PROTOCOL_GUID, handle=0x%lx, agent_handle=0x%lx, "
630-
"controller_handle=0x%lx)\n",
629+
printf("%s(EFI_DT_FIXUP_PROTOCOL_GUID, handle=%p, agent_handle=%p, "
630+
"controller_handle=%p)\n",
631631
__FUNCTION__, handle, agent_handle, controller_handle);
632632
return SUCCESS;
633633
}
@@ -661,16 +661,16 @@ EfiStatus locate_handle_buffer(EfiLocateHandleSearchType search_type,
661661
if (search_type == BY_PROTOCOL) {
662662
return list_block_devices(num_handles, buf);
663663
}
664-
printf("%s(0x%x, EFI_BLOCK_IO_PROTOCOL_GUID, search_key=0x%lx)\n",
664+
printf("%s(0x%x, EFI_BLOCK_IO_PROTOCOL_GUID, search_key=%p)\n",
665665
__FUNCTION__, search_type, search_key);
666666
return UNSUPPORTED;
667667
} else if (guid_eq(protocol, EFI_TEXT_INPUT_PROTOCOL_GUID)) {
668-
printf("%s(0x%x, EFI_TEXT_INPUT_PROTOCOL_GUID, search_key=0x%lx)\n",
668+
printf("%s(0x%x, EFI_TEXT_INPUT_PROTOCOL_GUID, search_key=%p)\n",
669669
__FUNCTION__, search_type, search_key);
670670
return NOT_FOUND;
671671
} else if (guid_eq(protocol, EFI_GBL_OS_CONFIGURATION_PROTOCOL_GUID)) {
672672
printf(
673-
"%s(0x%x, EFI_GBL_OS_CONFIGURATION_PROTOCOL_GUID, search_key=0x%lx)\n",
673+
"%s(0x%x, EFI_GBL_OS_CONFIGURATION_PROTOCOL_GUID, search_key=%p)\n",
674674
__FUNCTION__, search_type, search_key);
675675
if (num_handles != nullptr) {
676676
*num_handles = 1;
@@ -681,7 +681,7 @@ EfiStatus locate_handle_buffer(EfiLocateHandleSearchType search_type,
681681
}
682682
return SUCCESS;
683683
} else if (guid_eq(protocol, EFI_DT_FIXUP_PROTOCOL_GUID)) {
684-
printf("%s(0x%x, EFI_DT_FIXUP_PROTOCOL_GUID, search_key=0x%lx)\n",
684+
printf("%s(0x%x, EFI_DT_FIXUP_PROTOCOL_GUID, search_key=%p)\n",
685685
__FUNCTION__, search_type, search_key);
686686
if (num_handles != nullptr) {
687687
*num_handles = 1;
@@ -692,7 +692,7 @@ EfiStatus locate_handle_buffer(EfiLocateHandleSearchType search_type,
692692
}
693693
return SUCCESS;
694694
}
695-
printf("%s(0x%x, (0x%x 0x%x 0x%x 0x%llx), search_key=0x%lx)\n", __FUNCTION__,
695+
printf("%s(0x%x, (0x%x 0x%x 0x%x 0x%llx), search_key=%p)\n", __FUNCTION__,
696696
search_type, protocol->data1, protocol->data2, protocol->data3,
697697
*(uint64_t *)&protocol->data4, search_key);
698698
return UNSUPPORTED;

lib/uefi/runtime_service_provider.cpp

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,8 @@
2121
#include <stdio.h>
2222
#include <string.h>
2323

24+
namespace {
25+
2426
constexpr auto &&kSecureBoot = L"SecureBoot";
2527

2628
EFI_STATUS GetVariable(char16_t *VariableName, EfiGuid *VendorGuid,
@@ -62,6 +64,8 @@ EFI_STATUS SetVariable(char16_t *VariableName, EfiGuid *VendorGuid,
6264
return UNSUPPORTED;
6365
}
6466

67+
} // namespace
68+
6569
void setup_runtime_service_table(EfiRuntimeService *service) {
6670
service->GetVariable = GetVariable;
6771
service->SetVariable = SetVariable;

lib/uefi/switch_stack.S

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -14,12 +14,11 @@
1414
* limitations under the License.
1515
*
1616
*/
17-
18-
#include <lk/asm.h>
1917

18+
#include <lk/asm.h>
2019

21-
// int call_with_stack(void *stack, int (*fp)(), int arg1, int arg2, int arg3, int arg4);
22-
FUNCTION(call_with_stack)
20+
// int call_with_stack_asm(void *stack, int (*fp)(), int arg1, int arg2, int arg3, int arg4);
21+
FUNCTION(call_with_stack_asm)
2322
stp fp, lr, [sp, #-16]!
2423
mov fp, sp
2524

@@ -38,3 +37,4 @@ mov sp,x7
3837

3938
ldp fp, lr, [sp], 16
4039
ret lr
40+
END_FUNCTION(call_with_stack_asm)

lib/uefi/switch_stack.h

Lines changed: 15 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -15,28 +15,30 @@
1515
*
1616
*/
1717

18+
#include <lk/compiler.h>
1819
#include <stddef.h>
1920

20-
#ifdef __cplusplus
21-
extern "C" {
22-
23-
#endif
21+
__BEGIN_CDECLS
2422

25-
size_t call_with_stack(void *stack, int (*fp)(void *, void *), void *param1,
26-
void *param2, void *param3 = nullptr,
27-
void *param4 = nullptr);
23+
size_t call_with_stack_asm(void *stack, const void *function, void *param1,
24+
void *param2, void *param3, void *param4);
2825

29-
#ifdef __cplusplus
30-
}
26+
__END_CDECLS
3127

32-
#endif
3328
#ifdef __cplusplus
3429
template <typename Function, typename P1, typename P2, typename P3, typename P4>
3530
size_t call_with_stack(void *stack, Function &&fp, P1 &&param1, P2 &&param2,
36-
P3 param3, P4 &&param4) {
37-
return call_with_stack(
38-
stack, reinterpret_cast<int (*)(void *, void *)>(fp),
31+
P3 &&param3, P4 &&param4) {
32+
return call_with_stack_asm(
33+
stack, reinterpret_cast<const void *>(fp),
3934
reinterpret_cast<void *>(param1), reinterpret_cast<void *>(param2),
4035
reinterpret_cast<void *>(param3), reinterpret_cast<void *>(param4));
4136
}
37+
38+
template <typename Function, typename P1, typename P2>
39+
size_t call_with_stack(void *stack, Function &&fp, P1 &&param1, P2 &&param2) {
40+
return call_with_stack_asm(
41+
stack, reinterpret_cast<const void *>(fp),
42+
reinterpret_cast<void *>(param1), reinterpret_cast<void *>(param2), 0, 0);
43+
}
4244
#endif

lib/uefi/uefi.cpp

Lines changed: 8 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,6 @@
1818
#include "boot_service.h"
1919
#include "boot_service_provider.h"
2020
#include "defer.h"
21-
#include "kernel/thread.h"
2221
#include "pe.h"
2322

2423
#include <lib/bio.h>
@@ -40,6 +39,8 @@
4039
#include "system_table.h"
4140
#include "text_protocol.h"
4241

42+
namespace {
43+
4344
constexpr auto EFI_SYSTEM_TABLE_SIGNATURE =
4445
static_cast<u64>(0x5453595320494249ULL);
4546

@@ -57,9 +58,9 @@ template <typename T> void fill(T *data, size_t skip, uint8_t begin = 0) {
5758
}
5859
}
5960

60-
static constexpr size_t BIT26 = 1 << 26;
61-
static constexpr size_t BIT11 = 1 << 11;
62-
static constexpr size_t BIT10 = 1 << 10;
61+
constexpr size_t BIT26 = 1 << 26;
62+
constexpr size_t BIT11 = 1 << 11;
63+
constexpr size_t BIT10 = 1 << 10;
6364

6465
/**
6566
Pass in a pointer to an ARM MOVT or MOVW immediate instruciton and
@@ -299,7 +300,7 @@ int load_sections_and_execute(bdev_t *dev,
299300
constexpr size_t kStackSize = 8 * 1024ul * 1024;
300301
auto stack = reinterpret_cast<char *>(alloc_page(kStackSize, 23));
301302
memset(stack, 0, kStackSize);
302-
printf("Calling kernel with stack [0x%lx, 0x%lx]\n", stack,
303+
printf("Calling kernel with stack [%p, %p]\n", stack,
303304
stack + kStackSize - 1);
304305
return call_with_stack(stack + kStackSize, entry, image_base, &table);
305306
}
@@ -373,3 +374,5 @@ int cmd_uefi_load(int argc, const console_cmd_args *argv) {
373374
STATIC_COMMAND_START
374375
STATIC_COMMAND("uefi_load", "load UEFI application and run it", &cmd_uefi_load)
375376
STATIC_COMMAND_END(uefi);
377+
378+
} // namespace

0 commit comments

Comments
 (0)