UEFI 基础教程 (四十八.2) — UEFI code style
在向社区贡献代码过程中,Code style 十分重要,如果code style有问题,大概率不能通过code review.本文以 edk2
中一段程序为例会大致说明,C 语言在UEFI中大致遵循的规则,
UINT64 gAllZero = 0;/**The Entry Point for PCI Bus module. The user code starts with this function.Installs driver module protocols and. Creates virtual device handles for ConIn,ConOut, and StdErr. Installs Simple Text In protocol, Simple Text In Ex protocol,Simple Pointer protocol, Absolute Pointer protocol on those virtual handlers.Installs Graphics Output protocol and/or UGA Draw protocol if needed.@param[in] ImageHandle The firmware allocated handle for the EFI image.@param[in] SystemTable A pointer to the EFI System Table.@retval EFI_SUCCESS The entry point is executed successfully.@retval other Some error occurred when executing this entry point.**/
EFI_STATUS
EFIAPI
PciBusEntryPoint (IN EFI_HANDLE ImageHandle,IN EFI_SYSTEM_TABLE *SystemTable)
{EFI_STATUS Status;EFI_HANDLE Handle;//// Initializes PCI devices pool//InitializePciDevicePool ();//// Install driver model protocol(s).//Status = EfiLibInstallDriverBindingComponentName2 (ImageHandle,SystemTable,&gPciBusDriverBinding,ImageHandle,&gPciBusComponentName,&gPciBusComponentName2);ASSERT_EFI_ERROR (Status);if (FeaturePcdGet (PcdPciBusHotplugDeviceSupport)) {//// If Hot Plug is supported, install EFI PCI Hot Plug Request protocol.//Handle = NULL;Status = gBS->InstallProtocolInterface (&Handle,&gEfiPciHotPlugRequestProtocolGuid,EFI_NATIVE_INTERFACE,&mPciHotPlugRequest);}return Status;
}
UEFI C code style rule:
1. 变量
局部变量: 驼峰式命名,如Status,首字母大写
全局变量: 驼峰式命名,如gAllZero,类似于局部变量,不过首字母要加上g或者m
变量数据类型: 使用UEFI 定义的数据类型,`UINT8,UINT16,UINT32 `等, 如果是自定义类型,如`EFI_CSDN`,单词之间用下划线隔开(不要使用标准C原装数据类型,`char,int,short`)
2. 函数
函数名: 驼峰式命名(无下划线),多个单词拼装一起如PciBusEntryPoint ,函数名后的左 `( `和函数名之间需要有一个空格,右`)`需要单独一行加两个空格缩进
返回值: 使用`UEFI` 定义的数据类型,`UINT8,UINT16,EFI_STATUS`等
`EFI_API` 前缀:如果是`drive`r的`entrypoin`t或者`lib`的函数需要加`EFI_API `
函数参数:对函数定义而言,每个餐宿写一行,并退2个空格,如果知道参数是输入还是输出需要加上`IN`或者`OUT`
函数调用: 函数调用函数名与`(`之前有一个空格
3. 注释
代码行注释: 需三个 //
如下,
//// xxxxx//
函数定义注释: 需三个部分,
1)函数整体描述
2)函数详细描述
3)参数+返回值
/**The Entry Point for PCI Bus module. The user code starts with this function.Installs driver module protocols and. Creates virtual device handles for ConIn,ConOut, and StdErr. Installs Simple Text In protocol, Simple Text In Ex protocol,Simple Pointer protocol, Absolute Pointer protocol on those virtual handlers.Installs Graphics Output protocol and/or UGA Draw protocol if needed.@param[in] ImageHandle The firmware allocated handle for the EFI image.@param[in] SystemTable A pointer to the EFI System Table.@retval EFI_SUCCESS The entry point is executed successfully.@retval other Some error occurred when executing this entry point.**/
4. 空格处理
尾部空格需要全部清除
=
之间要加空格
函数名之后要加空格
5. 其他
状态码判断,使用UEFI定义的状态码如下,如果自定义需要参考UFEI定义格式,不要使用真实数字
为确保代码健壮,调用UFEI函数之后都要使用ASSERT_EFI_ERROR (Status);
或者EFI_ERROR(Status);
判断
EFI_SUCCESS
EFI_LOAD_ERROR
EFI_INVALID_PARAMETE
EFI_UNSUPPORTED
EFI_BAD_BUFFER_SIZE
EFI_BUFFER_TOO_SMALL
EFI_NOT_READY