blob: 9865ea06e34d72a184ae26390bceb2cf9684f9e1 [file] [log] [blame]
Anuj Gargae409552020-06-03 19:21:55 +05301/* Copyright (c) 2016-2020, The Linux Foundation. All rights reserved.
lijuang463e3cb2016-07-27 19:26:38 +08002 *
3 * Redistribution and use in source and binary forms, with or without
4 * modification, are permitted provided that the following conditions are
5 * met:
6 * * Redistributions of source code must retain the above copyright
7 * notice, this list of conditions and the following disclaimer.
8 * * Redistributions in binary form must reproduce the above
9 * copyright notice, this list of conditions and the following
10 * disclaimer in the documentation and/or other materials provided
11 * with the distribution.
12 * * Neither the name of The Linux Foundation nor the names of its
13 * contributors may be used to endorse or promote products derived
14 * from this software without specific prior written permission.
15 *
16 * THIS SOFTWARE IS PROVIDED "AS IS" AND ANY EXPRESS OR IMPLIED
17 * WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF
18 * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NON-INFRINGEMENT
19 * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS
20 * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
21 * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
22 * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR
23 * BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY,
24 * WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE
25 * OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN
26 * IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
27 */
Jeevan Shriram17f173d2017-10-24 22:11:07 -070028#include "AutoGen.h"
lijuang463e3cb2016-07-27 19:26:38 +080029#include <Library/BaseLib.h>
lijuang463e3cb2016-07-27 19:26:38 +080030#include <Library/BaseMemoryLib.h>
31#include <Library/DebugLib.h>
lijuang463e3cb2016-07-27 19:26:38 +080032#include <Library/DeviceInfo.h>
33#include <Library/DrawUI.h>
34#include <Library/FastbootMenu.h>
Jeevan Shriram17f173d2017-10-24 22:11:07 -070035#include <Library/MemoryAllocationLib.h>
lijuang463e3cb2016-07-27 19:26:38 +080036#include <Library/MenuKeysDetection.h>
Jeevan Shriram17f173d2017-10-24 22:11:07 -070037#include <Library/UefiBootServicesTableLib.h>
lijuang463e3cb2016-07-27 19:26:38 +080038#include <Library/UpdateDeviceTree.h>
Anuj Gargae409552020-06-03 19:21:55 +053039#include <Library/BootLinux.h>
lijuang463e3cb2016-07-27 19:26:38 +080040#include <Protocol/EFIVerifiedBoot.h>
Jeevan Shriram17f173d2017-10-24 22:11:07 -070041#include <Uefi.h>
lijuang463e3cb2016-07-27 19:26:38 +080042
43STATIC OPTION_MENU_INFO gMenuInfo;
44
45STATIC MENU_MSG_INFO mFastbootOptionTitle[] = {
Jeevan Shriram17f173d2017-10-24 22:11:07 -070046 {{"START"},
47 BIG_FACTOR,
48 BGR_GREEN,
49 BGR_BLACK,
50 OPTION_ITEM,
51 0,
52 RESTART},
53 {{"Restart bootloader"},
54 BIG_FACTOR,
55 BGR_RED,
56 BGR_BLACK,
57 OPTION_ITEM,
58 0,
59 FASTBOOT},
60 {{"Recovery mode"},
61 BIG_FACTOR,
62 BGR_RED,
63 BGR_BLACK,
64 OPTION_ITEM,
65 0,
66 RECOVER},
67 {{"Power off"},
68 BIG_FACTOR,
69 BGR_WHITE,
70 BGR_BLACK,
71 OPTION_ITEM,
72 0,
73 POWEROFF},
74 {{"Boot to FFBM"},
75 BIG_FACTOR,
76 BGR_YELLOW,
77 BGR_BLACK,
78 OPTION_ITEM,
79 0,
80 FFBM},
81 {{"Boot to QMMI"},
82 BIG_FACTOR,
83 BGR_YELLOW,
84 BGR_BLACK,
85 OPTION_ITEM,
86 0,
87 QMMI},
lijuang463e3cb2016-07-27 19:26:38 +080088};
89
90STATIC MENU_MSG_INFO mFastbootCommonMsgInfo[] = {
Jeevan Shriram17f173d2017-10-24 22:11:07 -070091 {{"\nPress volume key to select, "
92 "and press power key to select\n\n"},
93 COMMON_FACTOR,
94 BGR_WHITE,
95 BGR_BLACK,
96 COMMON,
97 0,
98 NOACTION},
Alexander Martinz1bf67092021-02-17 17:43:53 +010099 {{"Fastboot mode\n\n"},
Jeevan Shriram17f173d2017-10-24 22:11:07 -0700100 COMMON_FACTOR,
Alexander Martinz1bf67092021-02-17 17:43:53 +0100101 BGR_YELLOW,
Jeevan Shriram17f173d2017-10-24 22:11:07 -0700102 BGR_BLACK,
103 COMMON,
104 0,
105 NOACTION},
106 {{"PRODUCT_NAME - "},
107 COMMON_FACTOR,
108 BGR_WHITE,
109 BGR_BLACK,
110 COMMON,
111 0,
112 NOACTION},
Alexander Martinz968ec912020-06-02 14:06:24 +0200113 {{"PRODUCT_MODEL - "},
114 COMMON_FACTOR,
115 BGR_WHITE,
116 BGR_BLACK,
117 COMMON,
118 0,
119 NOACTION},
Jeevan Shriram17f173d2017-10-24 22:11:07 -0700120 {{"VARIANT - "},
121 COMMON_FACTOR,
122 BGR_WHITE,
123 BGR_BLACK,
124 COMMON,
125 0,
126 NOACTION},
127 {{"BOOTLOADER VERSION - "},
128 COMMON_FACTOR,
129 BGR_WHITE,
130 BGR_BLACK,
131 COMMON,
132 0,
133 NOACTION},
134 {{"BASEBAND VERSION - "},
135 COMMON_FACTOR,
136 BGR_WHITE,
137 BGR_BLACK,
138 COMMON,
139 0,
140 NOACTION},
141 {{"SERIAL NUMBER - "},
142 COMMON_FACTOR,
143 BGR_WHITE,
144 BGR_BLACK,
145 COMMON,
146 0,
147 NOACTION},
Alexander Martinz44513b02020-06-02 12:18:31 +0200148 {{"HARDWARE REVISION - "},
149 COMMON_FACTOR,
150 BGR_WHITE,
151 BGR_BLACK,
152 COMMON,
153 0,
154 NOACTION},
Jeevan Shriram17f173d2017-10-24 22:11:07 -0700155 {{"SECURE BOOT - "},
156 COMMON_FACTOR,
157 BGR_WHITE,
158 BGR_BLACK,
159 COMMON,
160 0,
161 NOACTION},
Alexander Martinz1bf67092021-02-17 17:43:53 +0100162 {{"\nDEVICE STATE - unlocked"},
Jeevan Shriram17f173d2017-10-24 22:11:07 -0700163 COMMON_FACTOR,
164 BGR_RED,
165 BGR_BLACK,
166 COMMON,
167 0,
168 NOACTION},
Alexander Martinz1bf67092021-02-17 17:43:53 +0100169 {{"\nDEVICE STATE - locked"},
170 COMMON_FACTOR,
171 BGR_GREEN,
172 BGR_BLACK,
173 COMMON,
174 0,
175 NOACTION},
lijuang463e3cb2016-07-27 19:26:38 +0800176};
177
178/**
179 Update the fastboot option item
180 @param[in] OptionItem The new fastboot option item
181 @param[out] pLocation The pointer of the location
182 @retval EFI_SUCCESS The entry point is executed successfully.
183 @retval other Some error occurs when executing this entry point.
184 **/
Jeevan Shriram17f173d2017-10-24 22:11:07 -0700185EFI_STATUS
186UpdateFastbootOptionItem (UINT32 OptionItem, UINT32 *pLocation)
lijuang463e3cb2016-07-27 19:26:38 +0800187{
Jeevan Shriram17f173d2017-10-24 22:11:07 -0700188 EFI_STATUS Status = EFI_SUCCESS;
189 UINT32 Location = 0;
190 UINT32 Height = 0;
191 MENU_MSG_INFO *FastbootLineInfo = NULL;
lijuang463e3cb2016-07-27 19:26:38 +0800192
Jeevan Shriram17f173d2017-10-24 22:11:07 -0700193 FastbootLineInfo = AllocateZeroPool (sizeof (MENU_MSG_INFO));
194 if (FastbootLineInfo == NULL) {
195 DEBUG ((EFI_D_ERROR, "Failed to allocate zero pool.\n"));
196 return EFI_OUT_OF_RESOURCES;
197 }
lijuang463e3cb2016-07-27 19:26:38 +0800198
Jeevan Shriram17f173d2017-10-24 22:11:07 -0700199 SetMenuMsgInfo (FastbootLineInfo, "__________", COMMON_FACTOR,
200 mFastbootOptionTitle[OptionItem].FgColor,
201 mFastbootOptionTitle[OptionItem].BgColor, LINEATION, Location,
202 NOACTION);
203 Status = DrawMenu (FastbootLineInfo, &Height);
204 if (Status != EFI_SUCCESS)
205 goto Exit;
206 Location += Height;
lijuang463e3cb2016-07-27 19:26:38 +0800207
Jeevan Shriram17f173d2017-10-24 22:11:07 -0700208 mFastbootOptionTitle[OptionItem].Location = Location;
209 Status = DrawMenu (&mFastbootOptionTitle[OptionItem], &Height);
210 if (Status != EFI_SUCCESS)
211 goto Exit;
212 Location += Height;
lijuang463e3cb2016-07-27 19:26:38 +0800213
Jeevan Shriram17f173d2017-10-24 22:11:07 -0700214 FastbootLineInfo->Location = Location;
215 Status = DrawMenu (FastbootLineInfo, &Height);
216 if (Status != EFI_SUCCESS)
217 goto Exit;
218 Location += Height;
lijuang463e3cb2016-07-27 19:26:38 +0800219
lijuangc336a292017-08-22 19:48:49 +0800220Exit:
221 FreePool (FastbootLineInfo);
222 FastbootLineInfo = NULL;
lijuang463e3cb2016-07-27 19:26:38 +0800223
Jeevan Shriram17f173d2017-10-24 22:11:07 -0700224 if (pLocation != NULL)
225 *pLocation = Location;
lijuang463e3cb2016-07-27 19:26:38 +0800226
Jeevan Shriram17f173d2017-10-24 22:11:07 -0700227 return Status;
lijuang463e3cb2016-07-27 19:26:38 +0800228}
229
230/**
231 Draw the fastboot menu
232 @param[out] OptionMenuInfo Fastboot option info
233 @retval EFI_SUCCESS The entry point is executed successfully.
234 @retval other Some error occurs when executing this entry point.
235 **/
Jeevan Shriram17f173d2017-10-24 22:11:07 -0700236STATIC EFI_STATUS
237FastbootMenuShowScreen (OPTION_MENU_INFO *OptionMenuInfo)
lijuang463e3cb2016-07-27 19:26:38 +0800238{
Jeevan Shriram17f173d2017-10-24 22:11:07 -0700239 EFI_STATUS Status = EFI_SUCCESS;
240 UINT32 Location = 0;
241 UINT32 OptionItem = 0;
242 UINT32 Height = 0;
243 UINT32 i = 0;
244 CHAR8 StrTemp[MAX_RSP_SIZE] = "";
245 CHAR8 StrTemp1[MAX_RSP_SIZE] = "";
246 CHAR8 VersionTemp[MAX_VERSION_LEN] = "";
lijuang463e3cb2016-07-27 19:26:38 +0800247
Jeevan Shriram17f173d2017-10-24 22:11:07 -0700248 ZeroMem (&OptionMenuInfo->Info, sizeof (MENU_OPTION_ITEM_INFO));
lijuang463e3cb2016-07-27 19:26:38 +0800249
Jeevan Shriram17f173d2017-10-24 22:11:07 -0700250 /* Update fastboot option title */
251 OptionMenuInfo->Info.MsgInfo = mFastbootOptionTitle;
252 for (i = 0; i < ARRAY_SIZE (mFastbootOptionTitle); i++) {
253 OptionMenuInfo->Info.OptionItems[i] = i;
254 }
255 OptionItem =
256 OptionMenuInfo->Info.OptionItems[OptionMenuInfo->Info.OptionIndex];
257 Status = UpdateFastbootOptionItem (OptionItem, &Location);
258 if (Status != EFI_SUCCESS)
259 return Status;
lijuang463e3cb2016-07-27 19:26:38 +0800260
Jeevan Shriram17f173d2017-10-24 22:11:07 -0700261 /* Update fastboot common message */
262 for (i = 0; i < ARRAY_SIZE (mFastbootCommonMsgInfo); i++) {
263 switch (i) {
264 case 0:
265 case 1:
266 break;
267 case 2:
268 /* Get product name */
269 AsciiStrnCatS (mFastbootCommonMsgInfo[i].Msg,
Jeevan Shriramd5d169f2018-04-20 16:09:10 -0700270 sizeof (mFastbootCommonMsgInfo[i].Msg), PRODUCT_NAME,
271 AsciiStrLen (PRODUCT_NAME));
Jeevan Shriram17f173d2017-10-24 22:11:07 -0700272 break;
273 case 3:
Alexander Martinz968ec912020-06-02 14:06:24 +0200274 /* Get product model */
275 AsciiStrnCatS (mFastbootCommonMsgInfo[i].Msg,
276 sizeof (mFastbootCommonMsgInfo[i].Msg), PRODUCT_MODEL,
277 AsciiStrLen (PRODUCT_MODEL));
278 break;
279 case 4:
Jeevan Shriram17f173d2017-10-24 22:11:07 -0700280 /* Get variant value */
281 BoardHwPlatformName (StrTemp, sizeof (StrTemp));
282 GetRootDeviceType (StrTemp1, sizeof (StrTemp1));
lijuang77421c02016-11-02 16:16:43 +0800283
Jeevan Shriram17f173d2017-10-24 22:11:07 -0700284 AsciiStrnCatS (mFastbootCommonMsgInfo[i].Msg,
285 sizeof (mFastbootCommonMsgInfo[i].Msg), StrTemp,
286 sizeof (StrTemp));
287 AsciiStrnCatS (mFastbootCommonMsgInfo[i].Msg,
288 sizeof (mFastbootCommonMsgInfo[i].Msg), " ",
289 AsciiStrLen (" "));
290 AsciiStrnCatS (mFastbootCommonMsgInfo[i].Msg,
291 sizeof (mFastbootCommonMsgInfo[i].Msg), StrTemp1,
292 sizeof (StrTemp1));
293 break;
Alexander Martinz968ec912020-06-02 14:06:24 +0200294 case 5:
Jeevan Shriram17f173d2017-10-24 22:11:07 -0700295 /* Get bootloader version */
296 GetBootloaderVersion (VersionTemp, sizeof (VersionTemp));
297 AsciiStrnCatS (mFastbootCommonMsgInfo[i].Msg,
298 sizeof (mFastbootCommonMsgInfo[i].Msg), VersionTemp,
299 sizeof (VersionTemp));
300 break;
Alexander Martinz968ec912020-06-02 14:06:24 +0200301 case 6:
Jeevan Shriram17f173d2017-10-24 22:11:07 -0700302 /* Get baseband version */
303 ZeroMem (VersionTemp, sizeof (VersionTemp));
304 GetRadioVersion (VersionTemp, sizeof (VersionTemp));
305 AsciiStrnCatS (mFastbootCommonMsgInfo[i].Msg,
306 sizeof (mFastbootCommonMsgInfo[i].Msg), VersionTemp,
307 sizeof (VersionTemp));
308 break;
Alexander Martinz968ec912020-06-02 14:06:24 +0200309 case 7:
Jeevan Shriram17f173d2017-10-24 22:11:07 -0700310 /* Get serial number */
311 ZeroMem (StrTemp, sizeof (StrTemp));
312 BoardSerialNum (StrTemp, MAX_RSP_SIZE);
313 AsciiStrnCatS (mFastbootCommonMsgInfo[i].Msg,
314 sizeof (mFastbootCommonMsgInfo[i].Msg), StrTemp,
315 sizeof (StrTemp));
316 break;
Alexander Martinz968ec912020-06-02 14:06:24 +0200317 case 8:
Alexander Martinz44513b02020-06-02 12:18:31 +0200318 /* Get hardware revision */
319 ZeroMem (StrTemp, sizeof (StrTemp));
320 BoardHardwareRevision (StrTemp, MAX_RSP_SIZE);
321 AsciiStrnCatS (mFastbootCommonMsgInfo[i].Msg,
322 sizeof (mFastbootCommonMsgInfo[i].Msg), StrTemp,
323 sizeof (StrTemp));
324 break;
Alexander Martinz968ec912020-06-02 14:06:24 +0200325 case 9:
Jeevan Shriram17f173d2017-10-24 22:11:07 -0700326 /* Get secure boot value */
327 AsciiStrnCatS (
328 mFastbootCommonMsgInfo[i].Msg, sizeof (mFastbootCommonMsgInfo[i].Msg),
329 IsSecureBootEnabled () ? "yes" : "no",
330 IsSecureBootEnabled () ? AsciiStrLen ("yes") : AsciiStrLen ("no"));
331 break;
Alexander Martinz968ec912020-06-02 14:06:24 +0200332 case 10:
Alexander Martinz1bf67092021-02-17 17:43:53 +0100333 /* Get device status, only show when unlocked */
334 if (!IsUnlocked ())
335 continue;
336 break;
337 case 11:
338 /* Get device status, only show when locked */
339 if (IsUnlocked ())
340 continue;
Jeevan Shriram17f173d2017-10-24 22:11:07 -0700341 break;
342 }
lijuang463e3cb2016-07-27 19:26:38 +0800343
Jeevan Shriram17f173d2017-10-24 22:11:07 -0700344 mFastbootCommonMsgInfo[i].Location = Location;
345 Status = DrawMenu (&mFastbootCommonMsgInfo[i], &Height);
346 if (Status != EFI_SUCCESS)
347 return Status;
348 Location += Height;
349 }
lijuang463e3cb2016-07-27 19:26:38 +0800350
Jeevan Shriram17f173d2017-10-24 22:11:07 -0700351 OptionMenuInfo->Info.MenuType = DISPLAY_MENU_FASTBOOT;
352 OptionMenuInfo->Info.OptionNum = ARRAY_SIZE (mFastbootOptionTitle);
lijuang463e3cb2016-07-27 19:26:38 +0800353
Jeevan Shriram17f173d2017-10-24 22:11:07 -0700354 return Status;
lijuang463e3cb2016-07-27 19:26:38 +0800355}
356
357/* Draw the fastboot menu and start to detect the key's status */
Jeevan Shriram17f173d2017-10-24 22:11:07 -0700358VOID DisplayFastbootMenu (VOID)
lijuang463e3cb2016-07-27 19:26:38 +0800359{
Jeevan Shriram17f173d2017-10-24 22:11:07 -0700360 EFI_STATUS Status;
361 OPTION_MENU_INFO *OptionMenuInfo;
lijuang463e3cb2016-07-27 19:26:38 +0800362
Anuj Gargae409552020-06-03 19:21:55 +0530363 if (IsEnableDisplayMenuFlagSupported ()) {
364 OptionMenuInfo = &gMenuInfo;
Jeevan Shriram17f173d2017-10-24 22:11:07 -0700365 DrawMenuInit ();
366 OptionMenuInfo->LastMenuType = OptionMenuInfo->Info.MenuType;
lijuang463e3cb2016-07-27 19:26:38 +0800367
Jeevan Shriram17f173d2017-10-24 22:11:07 -0700368 Status = FastbootMenuShowScreen (OptionMenuInfo);
369 if (Status != EFI_SUCCESS) {
370 DEBUG ((EFI_D_ERROR, "Unable to show fastboot menu on screen: %r\n",
371 Status));
372 return;
373 }
lijuang463e3cb2016-07-27 19:26:38 +0800374
Jeevan Shriram17f173d2017-10-24 22:11:07 -0700375 MenuKeysDetectionInit (OptionMenuInfo);
376 DEBUG ((EFI_D_VERBOSE, "Creating fastboot menu keys detect event\n"));
377 } else {
378 DEBUG ((EFI_D_INFO, "Display menu is not enabled!\n"));
379 }
lijuang463e3cb2016-07-27 19:26:38 +0800380}