hc
2024-03-25 edb30157bad0c0001c32b854271ace01d3b9a16a
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
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
/** @file
 *
 *  Copyright (c) 2006 - 2016, Intel Corporation. All rights reserved.
 *  Copyright (c) 2018, Andrei Warkentin <andrey.warkentin@gmail.com>
 *
 *  SPDX-License-Identifier: BSD-2-Clause-Patent
 *
 **/
 
/*
 * Loosely based on CrScreenShotDxe (https://github.com/LongSoft/CrScreenshotDxe).
 *
 * Copyright (c) 2016, Nikolaj Schlej, All rights reserved.
 *
 * Redistribution and use in source and binary forms, with or without
 * modification, are permitted provided that the following conditions are met:
 * - Redistributions of source code must retain the above copyright notice,
 *   this list of conditions and the following disclaimer.
 * - Redistributions in binary form must reproduce the above copyright notice,
 *   this list of conditions and the following disclaimer in the documentation
 * and/or other materials provided with the distribution.
 *
 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
 * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO,
 * THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
 * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR
 * CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
 * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
 * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR
 * PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF
 * LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
 * NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE,
 * EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
 */
 
#include "DisplayDxe.h"
#include <Protocol/SimpleFileSystem.h>
#include <Library/PrintLib.h>
#include <Library/BmpSupportLib.h>
#include <Library/UefiRuntimeServicesTableLib.h>
 
/*
 * ShowStatus defs.
 */
#define STATUS_SQUARE_SIDE 5
#define STATUS_YELLOW 0xff, 0xff, 0x00
#define STATUS_GREEN  0x00, 0xff, 0x00
#define STATUS_BLUE   0x00, 0x00, 0xff
#define STATUS_RED    0xff, 0x00, 0x00
 
EFI_STATUS
ShowStatus (
  IN EFI_GRAPHICS_OUTPUT_PROTOCOL *GraphicsOutput,
  IN UINT8 Red,
  IN UINT8 Green,
  IN UINT8 Blue
  )
{
  UINTN Index;
  EFI_GRAPHICS_OUTPUT_BLT_PIXEL Square[STATUS_SQUARE_SIDE * STATUS_SQUARE_SIDE];
  EFI_GRAPHICS_OUTPUT_BLT_PIXEL Backup[STATUS_SQUARE_SIDE * STATUS_SQUARE_SIDE];
 
  for (Index = 0; Index < STATUS_SQUARE_SIDE * STATUS_SQUARE_SIDE; Index++) {
    Square[Index].Blue = Blue;
    Square[Index].Green = Green;
    Square[Index].Red = Red;
    Square[Index].Reserved = 0x00;
  }
 
  // Backup current image.
  GraphicsOutput->Blt (GraphicsOutput, Backup,
                    EfiBltVideoToBltBuffer, 0, 0, 0, 0,
                    STATUS_SQUARE_SIDE, STATUS_SQUARE_SIDE, 0);
 
  // Draw the status square.
  GraphicsOutput->Blt (GraphicsOutput, Square,
                    EfiBltBufferToVideo, 0, 0, 0, 0,
                    STATUS_SQUARE_SIDE, STATUS_SQUARE_SIDE, 0);
 
  // Wait 500ms.
  gBS->Stall (500 * 1000);
 
  // Restore the backup.
  GraphicsOutput->Blt (GraphicsOutput, Backup,
                    EfiBltBufferToVideo, 0, 0, 0, 0,
                    STATUS_SQUARE_SIDE, STATUS_SQUARE_SIDE, 0);
  return EFI_SUCCESS;
}
 
STATIC
EFI_STATUS
FindWritableFs (
  OUT EFI_FILE_PROTOCOL **WritableFs
  )
{
  EFI_FILE_PROTOCOL *Fs = NULL;
  EFI_HANDLE *HandleBuffer = NULL;
  UINTN      HandleCount;
  UINTN      Index;
 
  EFI_STATUS Status = gBS->LocateHandleBuffer (ByProtocol,
                             &gEfiSimpleFileSystemProtocolGuid,
                             NULL, &HandleCount, &HandleBuffer);
  if (EFI_ERROR (Status)) {
    return Status;
  }
 
  for (Index = 0; Index < HandleCount; Index++) {
    EFI_SIMPLE_FILE_SYSTEM_PROTOCOL *SimpleFs = NULL;
    EFI_FILE_PROTOCOL *File = NULL;
 
    Status = gBS->HandleProtocol (HandleBuffer[Index],
                    &gEfiSimpleFileSystemProtocolGuid,
                    (VOID**)&SimpleFs);
    if (EFI_ERROR (Status)) {
      ASSERT_EFI_ERROR (Status);
      /*
       * Not supposed to happen.
       */
      continue;
    }
 
    Status = SimpleFs->OpenVolume (SimpleFs, &Fs);
    if (EFI_ERROR (Status)) {
      DEBUG ((DEBUG_ERROR, "%a OpenVolume[%u] returned %r\n", __FUNCTION__, Index, Status));
      continue;
    }
 
    Status = Fs->Open (Fs, &File, L"--------.---",
                   EFI_FILE_MODE_CREATE | EFI_FILE_MODE_READ |
                   EFI_FILE_MODE_WRITE, 0);
    if (EFI_ERROR (Status)) {
      DEBUG ((DEBUG_ERROR, "%a Open[%u] returned %r\n", __FUNCTION__, Index, Status));
      continue;
    }
 
    /*
     * Okay, we have a writable filesystem!
     */
    Fs->Delete (File);
    *WritableFs = Fs;
    Status = EFI_SUCCESS;
    break;
  }
 
  if (HandleBuffer) {
    FreePool (HandleBuffer);
  }
 
  return Status;
}
 
STATIC
VOID
TakeScreenshot (
  VOID
  )
{
  VOID *BmpImage = NULL;
  EFI_FILE_PROTOCOL *Fs = NULL;
  EFI_FILE_PROTOCOL *File = NULL;
  EFI_GRAPHICS_OUTPUT_PROTOCOL *GraphicsOutput = &gDisplayProto;
  EFI_GRAPHICS_OUTPUT_BLT_PIXEL *Image = NULL;
  EFI_STATUS Status;
  CHAR16 FileName[8 + 1 + 3 + 1];
  UINT32 ScreenWidth;
  UINT32 ScreenHeight;
  UINTN ImageSize;
  UINTN BmpSize;
  UINTN Index;
  EFI_TIME Time;
 
  Status = FindWritableFs (&Fs);
  if (EFI_ERROR (Status)) {
    ShowStatus (GraphicsOutput, STATUS_YELLOW);
  }
 
  ScreenWidth = GraphicsOutput->Mode->Info->HorizontalResolution;
  ScreenHeight = GraphicsOutput->Mode->Info->VerticalResolution;
  ImageSize = ScreenWidth * ScreenHeight;
 
  Status = gRT->GetTime (&Time, NULL);
  if (!EFI_ERROR (Status)) {
    UnicodeSPrint (FileName, sizeof (FileName), L"%02d%02d%02d%02d.bmp",
      Time.Day, Time.Hour, Time.Minute, Time.Second);
  } else {
    UnicodeSPrint (FileName, sizeof (FileName), L"scrnshot.bmp");
  }
 
  Image = AllocatePool (ImageSize * sizeof (EFI_GRAPHICS_OUTPUT_BLT_PIXEL));
  if (Image == NULL) {
    Status = EFI_OUT_OF_RESOURCES;
    ShowStatus (GraphicsOutput, STATUS_RED);
    goto Done;
  }
 
  Status = GraphicsOutput->Blt (GraphicsOutput, Image,
                             EfiBltVideoToBltBuffer, 0, 0, 0, 0,
                             ScreenWidth, ScreenHeight, 0);
  if (EFI_ERROR (Status)) {
    ShowStatus (GraphicsOutput, STATUS_RED);
    goto Done;
  }
 
  for (Index = 0; Index < ImageSize; Index++) {
    if (Image[Index].Red != 0x00 ||
        Image[Index].Green != 0x00 ||
        Image[Index].Blue != 0x00) {
      break;
    }
  }
 
  if (Index == ImageSize) {
    ShowStatus (GraphicsOutput, STATUS_BLUE);
    goto Done;
  }
 
  Status = TranslateGopBltToBmp (Image, ScreenHeight, ScreenWidth,
             &BmpImage, (UINT32*)&BmpSize);
  if (EFI_ERROR (Status)) {
    ShowStatus (GraphicsOutput, STATUS_RED);
    goto Done;
  }
 
  Status = Fs->Open (Fs, &File, FileName, EFI_FILE_MODE_CREATE |
                 EFI_FILE_MODE_READ | EFI_FILE_MODE_WRITE, 0);
  if (EFI_ERROR (Status)) {
    ShowStatus (GraphicsOutput, STATUS_RED);
    goto Done;
  }
 
  Status = File->Write (File, &BmpSize, BmpImage);
  File->Close (File);
  if (EFI_ERROR (Status)) {
    ShowStatus (GraphicsOutput, STATUS_RED);
    goto Done;
  }
 
  ShowStatus (GraphicsOutput, STATUS_GREEN);
 
Done:
  if (BmpImage != NULL) {
    FreePool (BmpImage);
  }
 
  if (Image != NULL) {
    FreePool (Image);
  }
}
 
STATIC
EFI_STATUS
EFIAPI
ScreenshotKeyHandler (
  IN EFI_KEY_DATA *KeyData
  )
{
  TakeScreenshot ();
  return EFI_SUCCESS;
}
 
STATIC
EFI_STATUS
ProcessScreenshotHandler (
  IN EFI_SIMPLE_TEXT_INPUT_EX_PROTOCOL *SimpleTextInEx
  )
{
  EFI_STATUS Status;
  EFI_HANDLE Handle;
  EFI_KEY_DATA ScreenshotKey;
 
  /*
   * LCtrl+LAlt+F12
   */
  ScreenshotKey.Key.ScanCode = SCAN_F12;
  ScreenshotKey.Key.UnicodeChar = 0;
  ScreenshotKey.KeyState.KeyShiftState = EFI_SHIFT_STATE_VALID |
    EFI_LEFT_CONTROL_PRESSED | EFI_LEFT_ALT_PRESSED;
  ScreenshotKey.KeyState.KeyToggleState = 0;
 
  Status = SimpleTextInEx->RegisterKeyNotify (
                             SimpleTextInEx,
                             &ScreenshotKey,
                             ScreenshotKeyHandler,
                             &Handle
                           );
  if (EFI_ERROR (Status)) {
    DEBUG ((DEBUG_ERROR, "%a: couldn't register key notification: %r\n", __FUNCTION__, Status));
    return Status;
  }
 
  return EFI_SUCCESS;
}
 
STATIC
VOID
ProcessScreenshotHandlers (
  VOID
  )
{
  UINTN Index;
  EFI_STATUS Status;
  UINTN HandleCount;
  EFI_HANDLE *HandleBuffer;
  EFI_SIMPLE_TEXT_INPUT_EX_PROTOCOL *SimpleTextInEx;
 
  Status = gBS->LocateHandleBuffer (ByProtocol, &gEfiSimpleTextInputExProtocolGuid,
                  NULL, &HandleCount, &HandleBuffer);
  if (EFI_ERROR (Status)) {
    return;
  }
 
  for (Index = 0; Index < HandleCount; Index++) {
    Status = gBS->HandleProtocol (HandleBuffer[Index],
                    &gEfiSimpleTextInputExProtocolGuid,
                    (VOID**)&SimpleTextInEx);
    if (EFI_ERROR (Status)) {
      ASSERT_EFI_ERROR (Status);
      /*
       * Not supposed to happen.
       */
      continue;
    }
 
    Status = ProcessScreenshotHandler (SimpleTextInEx);
    if (EFI_ERROR (Status)) {
      continue;
    }
  }
}
 
STATIC
VOID
EFIAPI
OnTextInExInstall (
  IN EFI_EVENT Event,
  IN VOID *Context
  )
{
  ProcessScreenshotHandlers ();
}
 
VOID
RegisterScreenshotHandlers (
  VOID
)
{
  EFI_STATUS Status;
  EFI_EVENT TextInExInstallEvent;
  VOID *TextInExInstallRegistration;
 
  ProcessScreenshotHandlers ();
 
  Status = gBS->CreateEvent (EVT_NOTIFY_SIGNAL, TPL_CALLBACK,
                  OnTextInExInstall, NULL,
                  &TextInExInstallEvent);
  if (EFI_ERROR (Status)) {
    DEBUG ((DEBUG_ERROR, "%a: couldn't create protocol install event: %r\n", __FUNCTION__, Status));
    return;
  }
 
  Status = gBS->RegisterProtocolNotify (&gEfiSimpleTextInputExProtocolGuid,
                  TextInExInstallEvent,
                  &TextInExInstallRegistration);
  if (EFI_ERROR (Status)) {
    DEBUG ((DEBUG_ERROR, "%a: couldn't register protocol install notify: %r\n", __FUNCTION__, Status));
    return;
  }
}