Source code of Windows XP (NT5)
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

258 lines
6.2 KiB

  1. /*++
  2. Copyright (c) 1999 Intel Corporation
  3. Module Name:
  4. libFileImage.c
  5. Abstract:
  6. Defines the properties and the operation of the FileImage data type, which
  7. is the image of the entire file that resides in memory.
  8. --*/
  9. #ifndef _LIB_FILE_IMAGE
  10. #define _LIB_FILE_IMAGE
  11. #include "libMisc.h"
  12. #define FILE_ATTRIBUTES EFI_FILE_MODE_READ | \
  13. EFI_FILE_MODE_WRITE | \
  14. EFI_FILE_MODE_CREATE
  15. #define FILE_READ_WRITE EFI_FILE_MODE_READ | \
  16. EFI_FILE_MODE_WRITE
  17. #define FILE_CREATE EFI_FILE_MODE_READ | \
  18. EFI_FILE_MODE_WRITE | \
  19. EFI_FILE_MODE_CREATE
  20. STATIC EFI_STATUS FileImageInit (VOID);
  21. STATIC EFI_STATUS FileImageCleanup(VOID);
  22. STATIC EFI_STATUS FileImageOpen (VOID);
  23. STATIC EFI_STATUS FileImageRead (VOID);
  24. STATIC EFI_STATUS FileImageClose (VOID);
  25. STATIC EFI_STATUS FileImageWrite (VOID);
  26. STATIC EFI_STATUS FileImageSetFilename(CHAR16*);
  27. extern EE_BUFFER_IMAGE BufferImage;
  28. EE_FILE_IMAGE FileImage = {
  29. NULL,
  30. NULL,
  31. NULL,
  32. FileImageInit,
  33. FileImageSetFilename
  34. };
  35. STATIC
  36. EFI_STATUS
  37. FileImageInit (
  38. VOID
  39. )
  40. {
  41. EFI_STATUS Status;
  42. EFI_LOADED_IMAGE *LoadedImage;
  43. EFI_DEVICE_PATH *DevicePath;
  44. EFI_FILE_IO_INTERFACE *Vol;
  45. BufferImage.Close();
  46. BufferImage.ImageCleanup();
  47. Status = BS->HandleProtocol (*MainEditor.ImageHandle,&LoadedImageProtocol,&LoadedImage);
  48. if (EFI_ERROR(Status)) {
  49. Print (L"Could not obtain Loaded Image Protocol\n");
  50. return EFI_LOAD_ERROR;
  51. }
  52. Status = BS->HandleProtocol (LoadedImage->DeviceHandle,&DevicePathProtocol,&DevicePath);
  53. if (EFI_ERROR(Status) || DevicePath == NULL) {
  54. Print (L"Could not obtain Device Path Protocol\n");
  55. return EFI_LOAD_ERROR;
  56. }
  57. Status = BS->HandleProtocol (LoadedImage->DeviceHandle,&FileSystemProtocol,&Vol);
  58. if (EFI_ERROR(Status)) {
  59. Print (L"Could not obtain File System Protocol\n");
  60. return EFI_LOAD_ERROR;
  61. }
  62. Status = Vol->OpenVolume(Vol,&FileImage.CurrentDir);
  63. if ( EFI_ERROR(Status) ) {
  64. Print (L"Could not open volume for the filesystem\n");
  65. return EFI_LOAD_ERROR;
  66. }
  67. FileImage.FileName = PoolPrint(L"NewFile.bin");
  68. MainEditor.BufferImage->Open = FileImageOpen;
  69. MainEditor.BufferImage->Close = FileImageClose;
  70. MainEditor.BufferImage->Read = FileImageRead;
  71. MainEditor.BufferImage->Write = FileImageWrite;
  72. MainEditor.BufferImage->ImageCleanup = FileImageCleanup;
  73. MainEditor.BufferImage->BufferType = FILE_BUFFER;
  74. return EFI_SUCCESS;
  75. }
  76. STATIC
  77. EFI_STATUS
  78. FileImageOpen (
  79. VOID
  80. )
  81. {
  82. EFI_STATUS Status;
  83. Status = FileImage.CurrentDir->Open (FileImage.CurrentDir,&FileImage.FileHandle,FileImage.FileName,FILE_ATTRIBUTES,0);
  84. if (EFI_ERROR(Status)) {
  85. MainEditor.StatusBar->SetStatusString(L"File Could Not be Opened");
  86. return EFI_NOT_FOUND;
  87. }
  88. return EFI_SUCCESS;
  89. }
  90. STATIC
  91. EFI_STATUS
  92. FileImageCleanup (
  93. VOID
  94. )
  95. {
  96. FreePool(FileImage.FileName);
  97. BufferImage.ImageCleanup = Nothing;
  98. BufferImage.Open = Nothing;
  99. BufferImage.Close = Nothing;
  100. BufferImage.Read = Nothing;
  101. BufferImage.Write = Nothing;
  102. return EFI_SUCCESS;
  103. }
  104. STATIC
  105. EFI_STATUS
  106. FileImageRead (
  107. VOID
  108. )
  109. {
  110. UINTN FileSize;
  111. VOID *FileBuffer;
  112. CHAR16 *Status;
  113. FileSize = 0x100000;
  114. FileBuffer = AllocatePool(FileSize);
  115. if ( FileBuffer == NULL ) {
  116. Print(L"%ECould not allocate File Buffer\n%N");
  117. return EFI_OUT_OF_RESOURCES;
  118. }
  119. FileImage.FileHandle->Read(FileImage.FileHandle,&FileSize,FileBuffer);
  120. BufferToList (BufferImage.ListHead,FileSize,FileBuffer);
  121. FreePool (FileBuffer);
  122. FileImage.FileHandle->Close(FileImage.FileHandle);
  123. Status = PoolPrint(L"0x%x Bytes Read",FileSize);
  124. MainEditor.StatusBar->SetStatusString(Status);
  125. FreePool (Status);
  126. return EFI_SUCCESS;
  127. }
  128. STATIC
  129. EFI_STATUS
  130. FileImageWrite (
  131. VOID
  132. )
  133. {
  134. EFI_STATUS Status;
  135. UINTN Length = 0;
  136. CHAR16 *Str;
  137. VOID *Buffer;
  138. Status = FileImage.CurrentDir->Open (FileImage.CurrentDir,&FileImage.FileHandle,FileImage.FileName,FILE_READ_WRITE,0);
  139. if (!EFI_ERROR(Status)) {
  140. Status = FileImage.FileHandle->Delete (FileImage.FileHandle);
  141. if (EFI_ERROR(Status)) {
  142. EditorError(Status,L"Error Deleting File");
  143. return EFI_SUCCESS;
  144. }
  145. }
  146. Status = FileImage.CurrentDir->Open (FileImage.CurrentDir,&FileImage.FileHandle,FileImage.FileName,FILE_CREATE,0);
  147. if (EFI_ERROR(Status)) {
  148. EditorError(Status,L"Error Accessing File");
  149. return EFI_SUCCESS;
  150. }
  151. Status = ListToBuffer(BufferImage.ListHead,&Length,&Buffer);
  152. if (EFI_ERROR(Status)) {
  153. EditorError(Status,L"FileImageWrite: Could not allocate buffer");
  154. return Status;
  155. }
  156. Status = FileImage.FileHandle->Write(FileImage.FileHandle,&Length,Buffer);
  157. if ( EFI_ERROR(Status) ) {
  158. EditorError(Status,L"Error Writing File");
  159. return Status;
  160. }
  161. FreePool(Buffer);
  162. Str = PoolPrint(L"0x%x Bytes Written",Length);
  163. MainEditor.StatusBar->SetStatusString(Str);
  164. FreePool(Str);
  165. Status = FileImage.CurrentDir->Close(FileImage.FileHandle);
  166. if ( EFI_ERROR(Status) ) {
  167. EditorError(Status,L"Error Closing File");
  168. return Status;
  169. }
  170. return EFI_SUCCESS;
  171. }
  172. STATIC
  173. EFI_STATUS
  174. FileImageClose (
  175. VOID
  176. )
  177. {
  178. LineDeleteAll(BufferImage.ListHead);
  179. BufferImage.NumBytes = 0;
  180. MainEditor.FileBuffer->Offset = 0x00;
  181. MainEditor.StatusBar->SetOffset(0x00);
  182. MainEditor.FileBuffer->LowVisibleOffset = 0x00;
  183. MainEditor.FileBuffer->SetPosition(DISP_START_ROW,HEX_POSITION);
  184. MainEditor.FileModified = FALSE;
  185. return EFI_SUCCESS;
  186. }
  187. STATIC
  188. EFI_STATUS
  189. FileImageSetFilename (
  190. IN CHAR16* Filename
  191. )
  192. {
  193. if (Filename == NULL) {
  194. return EFI_LOAD_ERROR;
  195. }
  196. if (FileImage.FileName != NULL) {
  197. FreePool(FileImage.FileName);
  198. }
  199. FileImage.FileName = PoolPrint(Filename);
  200. return EFI_SUCCESS;
  201. }
  202. #endif /* _LIB_FILE_IMAGE */