UEFI shell - 内置命令

一. UEFI shell 基础内置命令

其命令实现在ShellPkg/Library下:
在这里插入图片描述
主要有调试(Debug1),驱动(Driver1),网路(NetWork1),网路(NetWork2),安装(Install1),Level1,Level2,Level3。

1. Shell命令的通用选项

命令行参数 作用
-b,-break 输出信息分屏显示
-q,-quit 不输出任何信息
-t,-terse 用简洁格式输出信息
-sfo 用标准格式输出
-? 输出帮助信息

想要查询特定的命令,使用

1
2
3
help cmd
或者
cmd  -?

2. 基础命令汇总

基础命令已经集成在shell内部,可以使用用help查看
在这里插入图片描述

命令 功能
alias 显示,创建,删除别名
attrib 显示,更改文件或目录属性
bcfg 管理启动项
cd 更改当前目录
cls 清空标准输出
comp 比较两个文件
connect 将driver绑定到指定的设备并启动driver
cp 将文件或文件夹复制到另一个位置
date 显示或设置日期
dblk 显示块设备里的块
devices 列出所有设备
devtree 显示设备树
dh 显示设备句柄
disconnect 从指定设备卸载驱动
dmem 显示系统或设备内存的内容
dmpstore 管理UEFI NVRAM变量
drivers 显示设备驱动
drvcfg 配置驱动
drvdiag 调动Driver Disgnostis Protocol
echo 回显
edit 编辑ASCII或UCS-2文件
eficompress 压缩文件
efidecompress 解压文件
exit 退出Shell或脚本
help 显示帮助
hexedit 二进制编辑器,可编辑文件,块设备或内存
ifconfig 配置IP地址
load 加载UEFI驱动
loadpcirom 加载PCI ROM
ls 列出目录内容或文件信息
map 显示Mapping
memmap 显示目录映射
mkdir 创建目录
mm 列出或修改MEM/MMIO/IO/PCI/PCIE地址空间
mode 列出或修改输出设备的模式
mv 移动文件或目录
openinfo 显示Protocols打开信息
pause 暂停执行脚本,等待用户输入
pci 显示PCI设备
ping ping
reconnect 重新连接驱动与设备
reset 重启系统
rm 删除文件或目录
setmode 设置串口属性
set 显示或修改Shell中的环境变量
setsize 调整文件大小
setvar 设置UEFI变量
smbiosview 显示SMBIOS信息
stall 在指定的时间内暂停执行
time 显示/设置时区
timezone 显示/设置时区
touch 更新文件时间设置
type 显示文件类型
unload 卸载驱动
vol 显示/设置卷标

二. 写一个UEFI shell的内置命令

参考edk2/ShellPkg/Library/UefiShellNetwork1CommandsLib

源文件

1.inf文件

PATH:

1
edk2/ShellPkg/Library/UefiShellMyWriteCommandsLib/UefiShellMyWriteCommandsLib.inf

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
##  @file
# Provides shell my write functions
#
#
#
##

[Defines]
  INF_VERSION                    = 0x00010006
  BASE_NAME                      = UefiShellMyWriteCommandsLib
  FILE_GUID                      = 9A933F7E-3861-45ce-87AB-7221219AE255
  MODULE_TYPE                    = UEFI_APPLICATION
  VERSION_STRING                 = 1.0
  LIBRARY_CLASS                  = NULL|UEFI_APPLICATION UEFI_DRIVER
  CONSTRUCTOR                    = ShellMyWriteCommandsLibConstructor
  DESTRUCTOR                     = ShellMyWriteCommandsLibDestructor

[Sources.common]
  MyWriteCmd.c
  UefiShellMyWriteCommandsLib.uni
  UefiShellMyWriteCommandsLib.c
  UefiShellMyWriteCommandsLib.h

[Packages]
  MdePkg/MdePkg.dec
  ShellPkg/ShellPkg.dec
  MdeModulePkg/MdeModulePkg.dec

[LibraryClasses]
  MemoryAllocationLib
  BaseLib
  BaseMemoryLib
  DebugLib
  ShellCommandLib
  ShellLib
  UefiLib
  UefiRuntimeServicesTableLib
  UefiBootServicesTableLib
  PcdLib
  HiiLib
  FileHandleLib

[Pcd]
  gEfiShellPkgTokenSpaceGuid.PcdShellProfileMask ## CONSUMES

[Protocols]
 

[Guids]
  gShellMyWriteHiiGuid                         ## SOMETIMES_CONSUMES ## HII

2. Lib C文件

PATH:

1
edk2/ShellPkg/Library/UefiShellMyWriteCommandsLib/UefiShellMyWriteCommandsLib.c
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
/** @file
  Main file for NULL named library for My Write shell command functions.
**
**
**/

#include "UefiShellMyWriteCommandsLib.h"


#include "UefiShellMyWriteCommandsLib.h"

CONST CHAR16 gShelMyWriteFileName[] = L"ShellMyWriteCommands";
EFI_HII_HANDLE gShellMyWriteHiiHandle = NULL;

/**
  return the file name of the help text file if not using HII.

  @return The string pointer to the file name.
**/
CONST CHAR16*
EFIAPI
ShellCommandGetManFileNameMyWrite (
  VOID
  )
{
  return (gShelMyWriteFileName);
}

/**
  Constructor for the Shell My Write Commands library.

  Install the handlers for My Write UEFI Shell 2.0 profile commands.

  @param ImageHandle            The image handle of the process.
  @param SystemTable            The EFI System Table pointer.

  @retval EFI_SUCCESS           The shell command handlers were installed sucessfully.
  @retval EFI_UNSUPPORTED       The shell level required was not found.
**/
EFI_STATUS
EFIAPI
ShellMyWriteCommandsLibConstructor (
  IN EFI_HANDLE        ImageHandle,
  IN EFI_SYSTEM_TABLE  *SystemTable
  )
{
  gShellMyWriteHiiHandle = NULL;

  //
  // check our bit of the profiles mask
  //
  if ((PcdGet8(PcdShellProfileMask) & BIT3) == 0) {
    return (EFI_SUCCESS);
  }

  gShellMyWriteHiiHandle = HiiAddPackages (
                             &gShellMyWriteHiiGuid,
                             gImageHandle,
                             UefiShellMyWriteCommandsLibStrings,
                             NULL
                             );
  if (gShellMyWriteHiiHandle == NULL) {
    return (EFI_DEVICE_ERROR);
  }
  //
  // install our shell command handlers
  //
  ShellCommandRegisterCommandName(
    L"MyWriteCmd",    
    ShellCommandRunMyWrite,
    ShellCommandGetManFileNameMyWrite,
    0,
    L"MyWriteCmd",
    TRUE,
    gShellMyWriteHiiHandle,
    STRING_TOKEN(STR_MY_WRITE_HELP_PING)
    );

  return (EFI_SUCCESS);
}

/**
  Destructor for the library.  free any resources.

  @param ImageHandle            The image handle of the process.
  @param SystemTable            The EFI System Table pointer.
**/
EFI_STATUS
EFIAPI
ShellMyWriteCommandsLibDestructor (
  IN EFI_HANDLE        ImageHandle,
  IN EFI_SYSTEM_TABLE  *SystemTable
  )
{
  if (gShellMyWriteHiiHandle != NULL) {
    HiiRemovePackages(gShellMyWriteHiiHandle);
  }
  return (EFI_SUCCESS);
}

3. Lib H文件

PATH:

1
edk2/ShellPkg/Library/UefiShellMyWriteCommandsLib/UefiShellMyWriteCommandsLib.h
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
/** @file
  header file for NULL named library for my write shell command functions.

**/

#ifndef _UEFI_SHELL_MYWRITE_COMMANDS_LIB_H_
#define _UEFI_SHELL_MYWRITE_COMMANDS_LIB_H_

#include <Uefi.h>

#include <Guid/ShellLibHiiGuid.h>

#include <Protocol/Cpu.h>
#include <Protocol/ServiceBinding.h>
#include <Protocol/Arp.h>

#include <Library/BaseLib.h>
#include <Library/BaseMemoryLib.h>
#include <Library/DebugLib.h>
#include <Library/MemoryAllocationLib.h>
#include <Library/PcdLib.h>
#include <Library/ShellCommandLib.h>
#include <Library/ShellLib.h>
#include <Library/SortLib.h>
#include <Library/UefiLib.h>
#include <Library/UefiRuntimeServicesTableLib.h>
#include <Library/UefiBootServicesTableLib.h>
#include <Library/HiiLib.h>
#include <Library/DevicePathLib.h>
#include <Library/PrintLib.h>

extern EFI_HII_HANDLE gShellMyWriteHiiHandle;

/**
  Function for 'ping' command.

  @param[in] ImageHandle  Handle to the Image (NULL if Internal).
  @param[in] SystemTable  Pointer to the System Table (NULL if Internal).
**/
SHELL_STATUS
EFIAPI
ShellCommandRunMyWrite (
  IN EFI_HANDLE        ImageHandle,
  IN EFI_SYSTEM_TABLE  *SystemTable
  );

#endif

4.UNI文件

PATH:

1
edk2/ShellPkg/Library/UefiShellMyWriteCommandsLib/UefiShellMyWriteCommandsLib.uni
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
// /**
//
//
//
//
// String definitions for UEFI Shell 2.0 my write commands
//
//
// **/

/=#

#langdef   en-US "english"

#string STR_MY_WRITE_HELP_PING         #language en-US ""
"------------------------------------------\r\n"
"------------------------------------------\r\n"
"test write uefi shell cmd for help !!! ...\r\n"
"------------------------------------------\r\n"
"------------------------------------------\r\n"

5. cmd C文件

PATH:

1
edk2/ShellPkg/Library/UefiShellMyWriteCommandsLib/MyWriteCmd.c
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
/** @file
  The implementation for Shell command my write

  (C) Copyright 2013-2015 Hewlett-Packard Development Company, L.P.<BR>
  Copyright (c) 2006 - 2018, Intel Corporation. All rights reserved.<BR>

  SPDX-License-Identifier: BSD-2-Clause-Patent

**/

#include "UefiShellMyWriteCommandsLib.h"

STATIC CONST SHELL_PARAM_ITEM ParamList[] = {
  {L"-v", TypeValue},
  {L"-h", TypeValue},
  {NULL, TypeMax}
  };

/**
  Function for my write command.

  @param[in]  ImageHandle           Handle to the Image (NULL if Internal).
  @param[in]  SystemTable           Pointer to the System Table (NULL if Internal).

  @retval  SHELL_SUCCESS            The command completed successfully.
  @retval  Others                   The command failed.

**/
SHELL_STATUS
EFIAPI
ShellCommandRunMyWrite (
  IN  EFI_HANDLE                    ImageHandle,
  IN  EFI_SYSTEM_TABLE              *SystemTable
  )
{
  EFI_STATUS                      Status;
  LIST_ENTRY                      *CheckPackage;
  CHAR16                  *ProblemParam;

  Status = ShellInitialize ();
  ASSERT_EFI_ERROR (Status);

  Status = ShellCommandLineParse (ParamList, &CheckPackage, &ProblemParam, TRUE);
  ASSERT_EFI_ERROR (Status);

  if (ShellCommandLineGetFlag (CheckPackage, L"-v")) {
    ShellPrintEx (-1, -1, L"%V v1.0 %N\r\n");
    return SHELL_SUCCESS;
  }
 
  if (ShellCommandLineGetFlag (CheckPackage, L"-h")) {
      //ShellPrintEx (-1, -1, L"%V my write cmd for help... %N\r\n");
      ShellPrintHiiEx (-1, -1, NULL, STRING_TOKEN (STR_MY_WRITE_HELP_PING), gShellMyWriteHiiHandle, L"MyWriteCmd");
      return SHELL_SUCCESS;
  }

  Print (L"this is my write cmd !\n");

  return SHELL_SUCCESS;
}

操作

  1. 把该工程文件添加到OvmfPkg编译
    PATH:
1
edk2/OvmfPkg/OvmfPkgX64.dsc
1
2
3
4
5
6
7
8
9
10
11
12
13
    <LibraryClasses>
      ShellCommandLib|ShellPkg/Library/UefiShellCommandLib/UefiShellCommandLib.inf
      NULL|ShellPkg/Library/UefiShellLevel2CommandsLib/UefiShellLevel2CommandsLib.inf
      NULL|ShellPkg/Library/UefiShellLevel1CommandsLib/UefiShellLevel1CommandsLib.inf
      NULL|ShellPkg/Library/UefiShellLevel3CommandsLib/UefiShellLevel3CommandsLib.inf
      NULL|ShellPkg/Library/UefiShellDriver1CommandsLib/UefiShellDriver1CommandsLib.inf
      NULL|ShellPkg/Library/UefiShellDebug1CommandsLib/UefiShellDebug1CommandsLib.inf
      NULL|ShellPkg/Library/UefiShellInstall1CommandsLib/UefiShellInstall1CommandsLib.inf
      NULL|ShellPkg/Library/UefiShellNetwork1CommandsLib/UefiShellNetwork1CommandsLib.inf

#my write ...
      NULL|ShellPkg/Library/UefiShellMyWriteCommandsLib/UefiShellMyWriteCommandsLib.inf
#my write ....

结果

在这里插入图片描述