GUID的定义、声明、必要性、生成GUID的方法

目录

一、关于GUID的定义与声明

1. GUID 的定义和使用:

 2. 在 .inf 文件中声明 GUID:

二、uefi中一个全局变量为何既要用变量名又要用GUid进行标识

1. 变量名(VariableName)的作用

2. GUID(VendorGuid)的作用

3. 为什么需要同时使用变量名和 GUID

4. 示例

5. 总结

三、GUID的定义结构与生成方法

1. GUID 的定义和结构

2. GUID 的生成方式

3. GUID 的使用

如何定义和声明 GUID

4.生成 GUID 的工具

(1) 使用 Windows 系统工具,这种方法适用于 Windows 10 和 Windows 11。

(2) 在线生成工具

(3)使用编程语言生成


一、关于GUID的定义与声明

1. GUID 的定义和使用

GUID 的定义通常在 .dec 文件中完成,这是包的声明文件。在 .dec 文件中定义的 GUID 可以被该包中的所有模块使用。

  •  如果其他包需要使用该 GUID,只需在对应的 .inf 文件中引用该 .dec 文件即可。

 2. 在 .inf 文件中声明 GUID

  • .inf 文件用于定义模块的依赖关系和资源。在 .inf 文件中声明 GUID 的目的是让编译器知道该模块需要使用某个 GUID。

  • 如果多个模块需要使用同一个 GUID,它们可以在各自的 .inf 文件中声明相同的 GUID。这不会导致冲突,因为 GUID 的定义是全局唯一的。

  • @LsRefCodePkg/Loongson.dec
    [Guids]
     gPrimaryDisplayFormSetGuid                        = { 0x5d5178f3, 0x966c, 0x4bc5, { 0xae, 0xde, 0xf1, 0x11, 0x6e, 0x0a, 0x12, 0x34 } }
    
    
    
    @./LsRefCodePkg/Drivers/PrimaryDisplayDxe/PrimaryDisplayDxe.inf
    [Guids]
     gPrimaryDisplayFormSetGuid
    
    
    @./LsRefCodePkg/Drivers/PrimaryDisplayInit/PrimaryDisplayInit.inf 
    [Guids]
     gPrimaryDisplayFormSetGuid
    
    
    

二、uefi中一个全局变量为何既要用变量名又要用GUid进行标识

在 UEFI 中,变量的标识需要同时使用变量名(VariableName)和 GUID(VendorGuid),原因如下:

1. 变量名(VariableName)的作用

变量名是一个 Unicode 字符串,用于标识变量的名称。它在同一个 GUID 命名空间内是唯一的。变量名的作用是:

  • 提供直观的标识,方便开发者理解和使用

  • 允许在同一个 GUID 命名空间下定义多个变量。

2. GUID(VendorGuid)的作用

GUID(全局唯一标识符)是一个 128 位的唯一标识符,用于区分不同供应商或模块的变量。它的作用是:

  • 避免命名冲突:不同的供应商或模块可以使用相同的变量名,但通过不同的 GUID 来区分。

  • 提供命名空间隔离:GUID 定义了一个独立的命名空间,确保变量的全局唯一性。

3. 为什么需要同时使用变量名和 GUID

  1. 唯一性

    • 单独使用变量名可能导致命名冲突,因为不同模块可能使用相同的变量名。

    • 单独使用 GUID 可以避免冲突,但无法直观地表示变量的含义。

  2. 灵活性

    • 使用变量名和 GUID 的组合,可以在同一个 GUID 命名空间下定义多个变量,同时避免与其他模块的变量冲突

  3. 标准化

    • UEFI 规范要求每个变量通过名称和 GUID 唯一标识,这确保了跨平台和跨供应商的兼容性。

4. 示例

假设两个不同的模块需要定义一个名为 Lang 的变量:

  • 模块 A 使用 GUID gEfiGlobalVariableGuid 定义 Lang

  • 模块 B 使用 GUID gMyCustomModuleGuid 定义 Lang

通过变量名和 GUID 的组合,这两个变量可以共存,不会相互冲突。

用生活中例子理解: 湖南有个叫张伟的,山东也有个叫张伟的,我们叫张伟可能两个人同时应答,但是加上身份证号,就知道叫的人唯一。

关于同一个GUID命名空间下可定义多个变量,我的理解,一个身份证号可以给不同名字的人使用,比如同一个身份证号(GUID)下可以有张伟、张丽、张梅,这些同一GUID下的变量名字必须不同。

5. 总结

在 UEFI 中,变量名和 GUID 的组合用于确保变量的唯一性、避免命名冲突,并提供灵活的命名空间管理。这种机制是 UEFI 规范的核心部分,确保了变量的全局唯一性和跨模块的兼容性。

三、GUID的定义结构与生成方法

在 UEFI 和其他系统中,GUID(全局唯一标识符)是一个 128位的数字标识符,通常以 32个十六进制数字 表示,并以 8-4-4-4-12 的格式分组,例如:xxxxxxxx-xxxx-xxxx-xxxx-xxxxxxxxxxxx。这种格式确保了 GUID 的唯一性,同时便于人类阅读和调试。

1. GUID 的定义和结构

GUID 的定义遵循 RFC 4122 标准,其结构由以下几部分组成:

  1. TimeLow(4字节):时间戳的低字段。

  2. TimeMid(2字节):时间戳的中间字段。

  3. TimeHighAndVersion(2字节):时间戳的高字段和版本号。

  4. ClockSeqHighAndReserved(1字节):时钟序列的高字段和变体信息。

  5. ClockSeqLow(1字节):时钟序列的低字段。

  6. Node(6字节):空间唯一的节点标识符,通常基于网卡的 MAC 地址或随机生成。

2. GUID 的生成方式

GUID 的生成算法通常结合以下因素,以确保其全局唯一性:

  1. 时间戳:基于生成时的时间信息。

  2. 硬件地址:如网卡的 MAC 地址。

  3. 随机数:增加唯一性。

  4. 版本号和变体:标识生成算法的版本和类型。

3. GUID 的使用

在 UEFI 中,GUID 用于标识各种对象和服务,如协议、变量等。它们通过 .dec 文件定义,并在 .inf 文件中引用。

如何定义和声明 GUID

在代码中,GUID 通常以以下方式定义和声明:

EFI_GUID gMyGuid = {0x12345678, 0x1234, 0x1234, {0x12, 0x34, 0x56, 0x78, 0x90, 0xAB, 0xCD, 0xEF}};

.dec 文件中:

[Guids]
gMyGuid = {0x12345678, 0x1234, 0x1234, {0x12, 0x34, 0x56, 0x78, 0x90, 0xAB, 0xCD, 0xEF}}

.inf 文件中:

[Guids]
gMyGuid

4.生成 GUID 的工具

生成自己的 GUID(全局唯一标识符)可以通过多种方式实现,以下是一些常见的方法和工具:

(1) 使用 Windows 系统工具,这种方法适用于 Windows 10 和 Windows 11。

在 Windows 系统中,可以通过 PowerShell 快速生成 GUID:

  • 按下 Win + R 键,打开“运行”对话框,输入 powershell 并回车。

  • 在 PowerShell 窗口中,执行以下命令生成不带括号的 GUID:

    powershell
    [guid]::NewGuid().ToString()
  • 如果需要生成带括号的 GUID,可以使用:

  • '{'+[guid]::NewGuid().ToString()+'}'

(2) 在线生成工具

如果你需要快速生成 GUID,可以使用在线工具,这些工具通常提供简单易用的界面,支持批量生成:

(4)  使用 UEFI 开发工具

在 UEFI 开发环境中,GUID 通常通过 .dec 文件定义。你可以使用工具如  GenGuid 来生成 GUID,并将其添加到 .dec 文件中。

### Spring Framework ApplicationEventPublisher Example and Usage In the context of the Spring framework, `ApplicationEventPublisher` is an interface that allows beans to publish events to the application context. This mechanism facilitates a loosely coupled architecture where components can notify each other about significant occurrences without being directly dependent on one another. The core classes involved in this event-driven model include: - **ApplicationEvent**: A class extending from which all custom events should derive. - **ApplicationListener<E extends ApplicationEvent>**: An interface implemented by any bean wishing to listen for specific types of events. - **ApplicationEventMulticaster**: The component responsible for broadcasting events to registered listeners within the ApplicationContext[^1]. To demonstrate how these pieces work together using `ApplicationEventPublisher`, consider the following code snippets illustrating both publishing and listening capabilities. #### Publishing Events with ApplicationEventPublisher A service or repository layer might want to inform others when certain actions occur. For instance, after saving data into storage, it could broadcast such activity as shown below: ```java @Service public class MyService { private final ApplicationEventPublisher publisher; @Autowired public MyService(ApplicationEventPublisher publisher) { this.publisher = publisher; } void performAction() { // Action logic here... CustomEvent event = new CustomEvent(this); publisher.publishEvent(event); // Publishes the event through the context } } ``` Here, upon executing some action inside `performAction()`, a new `CustomEvent` gets created and published via injection of `ApplicationEventPublisher`. #### Listening for Specific Events Using ApplicationListener On the receiving end, interested parties implement `ApplicationListener<SpecificEventType>` to react accordingly whenever their targeted type occurs: ```java @Component public class EventConsumer implements ApplicationListener<MyCustomEvent> { @Override public void onApplicationEvent(MyCustomEvent event) { System.out.println("Received my custom event : " + event.getMessage()); } } ``` This listener will automatically receive notifications every time a matching event (`MyCustomEvent`) happens anywhere across different parts of your application[^2]. Additionally, annotations like `@EventListener` provide even more concise syntax while offering flexibility regarding method signatures and parameters used during handling processes. By leveraging these constructs effectively, developers gain powerful tools enabling robust communication patterns throughout complex systems built atop Spring's foundation.
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值