当前位置: 首页 > news >正文

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  

http://www.mrgr.cn/news/54261.html

相关文章:

  • 《嵌入式最全面试题-Offer直通车》目录
  • General Purpose I/O Ports and Peripheral I/O Lines (Ports)
  • idea 2023 创建 springboot 项目 LTS
  • xlnt加载excel报错:xl/workbook.xml:2:2581: error: attribute ‘localSheetId‘ expected
  • 3.1.1ReactOS系统中搜索给定长度的空间地址区间函数的实现
  • Gitlab 完全卸载–亲测可行
  • org.apache.http.impl.client.CloseableHttpClient的时候如果发生异常
  • 《使用Gin框架构建分布式应用》阅读笔记:p88-p100
  • 群控系统服务端开发模式-功能整理
  • 【移动安全】OWASP MASTG 移动应用程序安全测试指南
  • 大模型~合集14
  • 理解 React 中的 ReactElement、children 和 ReactNode
  • Java 线程池获取池中所有线程列表的方法
  • 优化方法之随机梯度下降SGD优化器收敛性证明
  • 代码随想录day04
  • mysql connect -- C api编译链接问题,接口介绍(初始化和销毁,连接,执行sql语句,获取结果集的元数据和数据,设置编码格式)
  • Python Logging 模块
  • Unexpected error: java.security.InvalidAlgorithmParameterException
  • 关于office中的word文档图片替换问题
  • MySQL程序介绍<二>
  • freeswitch-esl 进行强拆控制
  • 【代码随想录Day46】单调栈Part01
  • 探索计算机技术的无限可能:从基础到前沿的深度之旅
  • PCL 点云配准 非线性加权最小二乘优化的点到面ICP算法(精配准)
  • 使用 NVBit 进行内存访问跟踪指南
  • 希尔(shell)排序