summaryrefslogtreecommitdiff
path: root/StdLib/LibC/Uefi/Console.c
blob: 0c3a21cf45291b65651c7a79340b706b328747ed (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
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
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
/** @file
  File abstractions of the console.

  Manipulates EFI_FILE_PROTOCOL abstractions for stdin, stdout, stderr.

  Copyright (c) 2010, Intel Corporation. All rights reserved.<BR>
  This program and the accompanying materials are licensed and made available under
  the terms and conditions of the BSD License that accompanies this distribution.
  The full text of the license may be found at
  http://opensource.org/licenses/bsd-license.php.

  THE PROGRAM IS DISTRIBUTED UNDER THE BSD LICENSE ON AN "AS IS" BASIS,
  WITHOUT WARRANTIES OR REPRESENTATIONS OF ANY KIND, EITHER EXPRESS OR IMPLIED.

**/
#include  <Uefi.h>
#include  <Library/BaseLib.h>
#include  <Library/BaseMemoryLib.h>
#include  <Library/MemoryAllocationLib.h>
#include  <Library/UefiBootServicesTableLib.h>

#include  <LibConfig.h>
#include  <sys/EfiCdefs.h>

#include  <stdio.h>
#include  <sys/fcntl.h>
#include  <MainData.h>
#include  <Efi/Console.h>

static const CHAR16 * stdioNames[NUM_SPECIAL]   = {
  L"stdin:", L"stdout:", L"stderr:"
};
static const int stdioFlags[NUM_SPECIAL] = {
  O_RDONLY,             // stdin
  O_WRONLY,             // stdout
  O_WRONLY              // stderr
};

static const int  stdioMode[NUM_SPECIAL] = {
  0444,   // stdin
  0222,   // stdout
  0222    // stderr
};

static
EFI_STATUS
EFIAPI
ConClose(
  IN EFI_FILE_PROTOCOL         *This
  )
{
  ConInstance    *Stream;

  Stream = BASE_CR(This, ConInstance, Abstraction);
  // Quick check to see if Stream looks reasonable
  if(Stream->Cookie != 0x62416F49) {    // Cookie == 'IoAb'
    return RETURN_INVALID_PARAMETER;    // Looks like a bad This pointer
  }
  // Nothing to Flush.
  // Mark the Stream as closed.
  Stream->Dev = NULL;

  return RETURN_SUCCESS;
}

static
EFI_STATUS
EFIAPI
ConDelete(
  IN EFI_FILE_PROTOCOL         *This
  )
{
  return RETURN_UNSUPPORTED;
}

static
EFI_STATUS
EFIAPI
ConRead(
  IN EFI_FILE_PROTOCOL         *This,
  IN OUT UINTN                 *BufferSize,
  OUT VOID                     *Buffer
  )
{
  EFI_SIMPLE_TEXT_INPUT_PROTOCOL   *Proto;
  ConInstance    *Stream;
  CHAR16         *OutPtr;
  EFI_INPUT_KEY   Key;
  UINTN           NumChar;
  UINTN           Edex;
  EFI_STATUS      Status;
  UINTN           i;

  Stream = BASE_CR(This, ConInstance, Abstraction);
  // Quick check to see if Stream looks reasonable
  if(Stream->Cookie != 0x62416F49) {    // Cookie == 'IoAb'
    return RETURN_INVALID_PARAMETER;    // Looks like a bad This pointer
  }
  if(Stream->Dev == NULL) {
    // Can't read from an unopened Stream
    return RETURN_DEVICE_ERROR;
  }
  if(Stream != &gMD->StdIo[0]) {
    // Read only valid for stdin
    return RETURN_UNSUPPORTED;
  }
  // It looks like things are OK for trying to read
  // We will accumulate *BufferSize characters or until we encounter
  // an "activation" character.  Currently any control character.
  Proto = (EFI_SIMPLE_TEXT_INPUT_PROTOCOL *)Stream->Dev;
  OutPtr = Buffer;
  NumChar = (*BufferSize - 1) / sizeof(CHAR16);
  i = 0;
  do {
    Status = gBS->WaitForEvent( 1, &Proto->WaitForKey, &Edex);
    if(Status != EFI_SUCCESS) {
      break;
    }
    Status = Proto->ReadKeyStroke(Proto, &Key);
    if(Status != EFI_SUCCESS) {
      break;
    }
    if(Key.ScanCode == SCAN_NULL) {
      *OutPtr++ = Key.UnicodeChar;
      ++i;
    }
    if(Key.UnicodeChar < 0x0020) {    // If a control character, or a scan code
      break;
    }
  } while(i < NumChar);
  *BufferSize = i * sizeof(CHAR16);
  return Status;
}

/* Write a NULL terminated WCS to the EFI console.

  @param[in,out]  BufferSize  Number of bytes in Buffer.  Set to zero if
                              the string couldn't be displayed.
  @parem[in]      Buffer      The WCS string to be displayed

*/
static
EFI_STATUS
EFIAPI
ConWrite(
  IN EFI_FILE_PROTOCOL         *This,
  IN OUT UINTN                 *BufferSize,
  IN VOID                      *Buffer
  )
{
  EFI_STATUS      Status;
  EFI_SIMPLE_TEXT_OUTPUT_PROTOCOL   *Proto;
  ConInstance    *Stream;
  CHAR16         *MyBuf;
  UINTN           NumChar;

  Stream = BASE_CR(This, ConInstance, Abstraction);
  // Quick check to see if Stream looks reasonable
  if(Stream->Cookie != 0x62416F49) {    // Cookie == 'IoAb'
    return RETURN_INVALID_PARAMETER;    // Looks like a bad This pointer
  }
  if(Stream->Dev == NULL) {
    // Can't write to an unopened Stream
    return RETURN_DEVICE_ERROR;
  }
  if(Stream == &gMD->StdIo[0]) {
    // Write is not valid for stdin
    return RETURN_UNSUPPORTED;
  }
  // Everything is OK to do the write.
  Proto = (EFI_SIMPLE_TEXT_OUTPUT_PROTOCOL *)Stream->Dev;
  MyBuf = (CHAR16 *)Buffer;
  NumChar = *BufferSize;

  // Send MyBuf to the console
  Status = Proto->OutputString( Proto, MyBuf);
  // Depending on status, update BufferSize and return
  if(Status != EFI_SUCCESS) {
    *BufferSize = 0;    // We don't really know how many characters made it out
  }
  else {
    *BufferSize = NumChar;
    Stream->NumWritten += NumChar;
  }
  return Status;
}

static
EFI_STATUS
EFIAPI
ConGetPosition(
  IN EFI_FILE_PROTOCOL         *This,
  OUT UINT64                   *Position
  )
{
  ConInstance                       *Stream;
  EFI_SIMPLE_TEXT_OUTPUT_PROTOCOL   *Proto;
  XYoffset                           CursorPos;

  Stream = BASE_CR(This, ConInstance, Abstraction);
  // Quick check to see if Stream looks reasonable
  if(Stream->Cookie != 0x62416F49) {    // Cookie == 'IoAb'
    return RETURN_INVALID_PARAMETER;    // Looks like a bad This pointer
  }
  if(Stream == &gMD->StdIo[0]) {
    // This is stdin
    *Position = Stream->NumRead;
  }
  else {
    Proto = (EFI_SIMPLE_TEXT_OUTPUT_PROTOCOL *)Stream->Dev;
    CursorPos.XYpos.Column = (UINT32)Proto->Mode->CursorColumn;
    CursorPos.XYpos.Row    = (UINT32)Proto->Mode->CursorRow;
    *Position = CursorPos.Offset;
  }
  return RETURN_SUCCESS;
}

static
EFI_STATUS
EFIAPI
ConSetPosition(
  IN EFI_FILE_PROTOCOL         *This,
  IN UINT64                     Position
  )
{
  ConInstance                       *Stream;
  EFI_SIMPLE_TEXT_OUTPUT_PROTOCOL   *Proto;
  XYoffset                           CursorPos;

  Stream = BASE_CR(This, ConInstance, Abstraction);
  // Quick check to see if Stream looks reasonable
  if(Stream->Cookie != 0x62416F49) {    // Cookie == 'IoAb'
    return RETURN_INVALID_PARAMETER;    // Looks like a bad This pointer
  }
  if(Stream->Dev == NULL) {
    // Can't write to an unopened Stream
    return RETURN_DEVICE_ERROR;
  }
  if(Stream == &gMD->StdIo[0]) {
    // Seek is not valid for stdin
    return RETURN_UNSUPPORTED;
  }
  // Everything is OK to do the final verification and "seek".
  Proto = (EFI_SIMPLE_TEXT_OUTPUT_PROTOCOL *)Stream->Dev;
  CursorPos.Offset = Position;

  return Proto->SetCursorPosition(Proto,
                                  (INTN)CursorPos.XYpos.Column,
                                  (INTN)CursorPos.XYpos.Row);
}

static
EFI_STATUS
EFIAPI
ConGetInfo(
  IN EFI_FILE_PROTOCOL         *This,
  IN EFI_GUID                  *InformationType,
  IN OUT UINTN                 *BufferSize,
  OUT VOID                     *Buffer
  )
{
  EFI_FILE_INFO       *InfoBuf;
  ConInstance         *Stream;

  Stream = BASE_CR(This, ConInstance, Abstraction);
  // Quick check to see if Stream looks reasonable
  if((Stream->Cookie != 0x62416F49) ||    // Cookie == 'IoAb'
     (Buffer == NULL))
  {
    return RETURN_INVALID_PARAMETER;
  }
  if(*BufferSize < sizeof(EFI_FILE_INFO)) {
    *BufferSize = sizeof(EFI_FILE_INFO);
    return RETURN_BUFFER_TOO_SMALL;
  }
  // All of our parameters are correct, so fill in the information.
  (void) ZeroMem(Buffer, sizeof(EFI_FILE_INFO));
  InfoBuf = (EFI_FILE_INFO *)Buffer;
  InfoBuf->Size = sizeof(EFI_FILE_INFO);
  InfoBuf->FileSize = 1;
  InfoBuf->PhysicalSize = 1;
  *BufferSize = sizeof(EFI_FILE_INFO);

  return RETURN_SUCCESS;
}

static
EFI_STATUS
EFIAPI
ConSetInfo(
  IN EFI_FILE_PROTOCOL         *This,
  IN EFI_GUID                  *InformationType,
  IN UINTN                      BufferSize,
  IN VOID                      *Buffer
  )
{
  return RETURN_UNSUPPORTED;
}

static
EFI_STATUS
EFIAPI
ConFlush(
  IN EFI_FILE_PROTOCOL         *This
  )
{
  return RETURN_SUCCESS;
}

EFI_STATUS
EFIAPI
ConOpen(
  IN  EFI_FILE_PROTOCOL        *This,
  OUT EFI_FILE_PROTOCOL       **NewHandle,
  IN  CHAR16                   *FileName,
  IN  UINT64                    OpenMode,   // Ignored
  IN  UINT64                    Attributes  // Ignored
  )
{
  ConInstance                      *Stream;
  EFI_SIMPLE_TEXT_OUTPUT_PROTOCOL  *Proto;
  UINTN                             Columns;
  UINTN                             Rows;
  int                               i;

  if((NewHandle == NULL)  ||
     (FileName  == NULL))
  {
    return RETURN_INVALID_PARAMETER;
  }
  // Process FileName
  for(i = 0; i < NUM_SPECIAL; ++i) {
    if(StrCmp( stdioNames[i], FileName) == 0) {
      break;
    }
  }
  if(i >= NUM_SPECIAL) {
    return RETURN_NO_MAPPING;
  }
  // Get pointer to instance.
  Stream = &gMD->StdIo[i];
  if(Stream->Dev == NULL) {
    // If this stream has been closed, then
    // Initialize instance.
    Stream->Cookie      = 0x62416F49;
    Stream->NumRead     = 0;
    Stream->NumWritten  = 0;
    switch(i) {
      case 0:
        Stream->Dev = gST->ConIn;
        break;
      case 1:
        Stream->Dev = gST->ConOut;
        break;
      case 2:
        if(gST->StdErr == NULL) {
          Stream->Dev = gST->ConOut;
        }
        else {
          Stream->Dev = gST->StdErr;
        }
        break;
      default:
        return RETURN_VOLUME_CORRUPTED;     // This is a "should never happen" case.
    }
    Stream->Abstraction.Revision      = 0x00010000;
    Stream->Abstraction.Open          = &ConOpen;
    Stream->Abstraction.Close         = &ConClose;
    Stream->Abstraction.Delete        = &ConDelete;
    Stream->Abstraction.Read          = &ConRead;
    Stream->Abstraction.Write         = &ConWrite;
    Stream->Abstraction.GetPosition   = &ConGetPosition;
    Stream->Abstraction.SetPosition   = &ConSetPosition;
    Stream->Abstraction.GetInfo       = &ConGetInfo;
    Stream->Abstraction.SetInfo       = &ConSetInfo;
    Stream->Abstraction.Flush         = &ConFlush;
    // Get additional information if this is an Output stream
    if(i != 0) {
      Proto = Stream->Dev;
      Stream->ConOutMode = Proto->Mode->Mode;
      if( Proto->QueryMode(Proto, Stream->ConOutMode, &Columns, &Rows) != RETURN_SUCCESS) {
        Stream->Dev = NULL;   // Mark this stream as closed
        return RETURN_INVALID_PARAMETER;
      }
      Stream->MaxConXY.XYpos.Column = (UINT32)Columns;
      Stream->MaxConXY.XYpos.Row    = (UINT32)Rows;
    }
  }
  // Save NewHandle and return.
  *NewHandle = &Stream->Abstraction;

  return RETURN_SUCCESS;
}