aboutsummaryrefslogtreecommitdiffstats
path: root/Lessons/Lesson_72/HIIFormLabel/HIIFormLabel.c
blob: adf3172e2fd0dfaae63ec6decb4e1625707393fb (plain)
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
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
/*
 * Copyright (c) 2021, Konstantin Aladyshev <aladyshev22@gmail.com>
 *
 * SPDX-License-Identifier: MIT
 */

#include <Library/UefiBootServicesTableLib.h>
#include <Library/UefiLib.h>
#include <Library/HiiLib.h>
#include <Guid/MdeModuleHii.h>
#include "Data.h"

extern UINT8 FormBin[];

EFI_HII_HANDLE  mHiiHandle = NULL;


EFI_STATUS
EFIAPI
HIIFormLabelUnload (
  EFI_HANDLE ImageHandle
  )
{
  if (mHiiHandle != NULL)
    HiiRemovePackages(mHiiHandle);

  return EFI_SUCCESS;
}

EFI_STATUS
EFIAPI
HIIFormLabelEntryPoint (
  IN EFI_HANDLE        ImageHandle,
  IN EFI_SYSTEM_TABLE  *SystemTable
  )
{
  mHiiHandle = HiiAddPackages(
                 &gEfiCallerIdGuid,
                 NULL,
                 HIIFormLabelStrings,
                 FormBin,
                 NULL
                 );
  if (mHiiHandle == NULL) {
    return EFI_OUT_OF_RESOURCES;
  }

  VOID* StartOpCodeHandle = HiiAllocateOpCodeHandle();
  EFI_IFR_GUID_LABEL* StartLabel = (EFI_IFR_GUID_LABEL*) HiiCreateGuidOpCode(StartOpCodeHandle,
                                                                             &gEfiIfrTianoGuid,
                                                                             NULL,
                                                                             sizeof(EFI_IFR_GUID_LABEL)
                                                                             );
  if (StartLabel == NULL) {
    Print(L"Error! Can't create StartLabel opcode, not enough space\n");
    HiiRemovePackages(mHiiHandle);
    return EFI_BUFFER_TOO_SMALL;
  }
  StartLabel->ExtendOpCode = EFI_IFR_EXTEND_OP_LABEL;
  StartLabel->Number = LABEL_START;


  VOID* EndOpCodeHandle = HiiAllocateOpCodeHandle();
  EFI_IFR_GUID_LABEL* EndLabel = (EFI_IFR_GUID_LABEL*) HiiCreateGuidOpCode(EndOpCodeHandle,
                                                                           &gEfiIfrTianoGuid,
                                                                           NULL,
                                                                           sizeof(EFI_IFR_GUID_LABEL)
                                                                           );
  if (EndLabel == NULL) {
    Print(L"Error! Can't create EndLabel opcode, not enough space\n");
    HiiFreeOpCodeHandle(StartOpCodeHandle);
    HiiFreeOpCodeHandle(EndOpCodeHandle);
    HiiRemovePackages(mHiiHandle);
    return EFI_BUFFER_TOO_SMALL;
  }

  EndLabel->ExtendOpCode = EFI_IFR_EXTEND_OP_LABEL;
  EndLabel->Number = LABEL_END;

  EFI_STRING_ID text_prompt = HiiSetString(mHiiHandle,
                                           0,
                                           L"Text prompt",
                                           NULL);
  EFI_STRING_ID text_help = HiiSetString(mHiiHandle,
                                         0,
                                         L"Text help",
                                         NULL);


  UINT8* Result = HiiCreateTextOpCode(StartOpCodeHandle,
                                      text_prompt,
                                      text_help,
                                      0);
  if (Result == NULL) {
    Print(L"Error! Can't create Text opcode, not enough space\n");
    HiiFreeOpCodeHandle(StartOpCodeHandle);
    HiiFreeOpCodeHandle(EndOpCodeHandle);
    HiiRemovePackages(mHiiHandle);
    return EFI_BUFFER_TOO_SMALL;
  }

  text_prompt = HiiSetString(mHiiHandle,
                             0,
                             L"Another text prompt",
                             NULL);
  text_help = HiiSetString(mHiiHandle,
                           0,
                           L"Another text help",
                           NULL);

  Result = HiiCreateTextOpCode(StartOpCodeHandle,
                               text_prompt,
                               text_help,
                               0);
  if (Result == NULL) {
    Print(L"Error! Can't create Text opcode, not enough space\n");
    HiiFreeOpCodeHandle(StartOpCodeHandle);
    HiiFreeOpCodeHandle(EndOpCodeHandle);
    HiiRemovePackages(mHiiHandle);
    return EFI_BUFFER_TOO_SMALL;
  }


 
  EFI_GUID formsetGuid = FORMSET_GUID;
  EFI_STATUS Status = HiiUpdateForm(
                        mHiiHandle,
                        &formsetGuid,
                        0x1,
                        StartOpCodeHandle,
                        EndOpCodeHandle
                      );
  if (EFI_ERROR(Status)) {
    Print(L"Error! HiiUpdateForm returned = %r\n", Status);
  }

  HiiFreeOpCodeHandle(StartOpCodeHandle);
  HiiFreeOpCodeHandle(EndOpCodeHandle);
  return Status;
}