UEFI shell - 内置命令

一. UEFI shell 基础内置命令

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

1. Shell命令的通用选项

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

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

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设备
pingping
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:

edk2/ShellPkg/Library/UefiShellMyWriteCommandsLib/UefiShellMyWriteCommandsLib.inf
##  @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:

edk2/ShellPkg/Library/UefiShellMyWriteCommandsLib/UefiShellMyWriteCommandsLib.c
/** @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:

edk2/ShellPkg/Library/UefiShellMyWriteCommandsLib/UefiShellMyWriteCommandsLib.h
/** @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:

edk2/ShellPkg/Library/UefiShellMyWriteCommandsLib/UefiShellMyWriteCommandsLib.uni
// /**
//
// 
//
//
// 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:

edk2/ShellPkg/Library/UefiShellMyWriteCommandsLib/MyWriteCmd.c
/** @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:
edk2/OvmfPkg/OvmfPkgX64.dsc
    <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 ....

结果

在这里插入图片描述

  • 5
    点赞
  • 33
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值