Leaked source code of windows server 2003
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.

245 lines
4.9 KiB

  1. /*++
  2. Copyright (c) 1999 Intel Corporation
  3. Module Name:
  4. libDiskImage.c
  5. Abstract:
  6. Describes the routines for handling and editing a disk buffer in memory
  7. --*/
  8. #ifndef _LIB_DISK_IMAGE
  9. #define _LIB_DISK_IMAGE
  10. #include "libMisc.h"
  11. extern EE_BUFFER_IMAGE BufferImage;
  12. STATIC EFI_STATUS DiskImageInit (VOID);
  13. STATIC EFI_STATUS DiskImageCleanup (VOID);
  14. STATIC EFI_STATUS DiskImageOpen (VOID);
  15. STATIC EFI_STATUS DiskImageClose (VOID);
  16. STATIC EFI_STATUS DiskImageRead (VOID);
  17. STATIC EFI_STATUS DiskImageWrite (VOID);
  18. STATIC EFI_STATUS DiskImageSetSize (UINTN);
  19. STATIC EFI_STATUS DiskImageSetOffset (UINT64);
  20. STATIC EFI_STATUS DiskImageSetDevice (CHAR16*);
  21. EE_DISK_IMAGE DiskImage = {
  22. NULL,
  23. NULL,
  24. 0,
  25. 0,
  26. DiskImageInit,
  27. DiskImageSetDevice,
  28. DiskImageSetOffset,
  29. DiskImageSetSize
  30. };
  31. STATIC
  32. EFI_STATUS
  33. DiskImageInit (
  34. VOID
  35. )
  36. {
  37. EFI_HANDLE *HandleBuffer = NULL;
  38. BufferImage.ImageCleanup();
  39. BufferImage.ImageCleanup = DiskImageCleanup;
  40. BufferImage.Open = DiskImageOpen;
  41. BufferImage.Close = DiskImageClose;
  42. BufferImage.Read = DiskImageRead;
  43. BufferImage.Write = DiskImageWrite;
  44. BufferImage.BufferType = DISK_BUFFER;
  45. return EFI_SUCCESS;
  46. }
  47. STATIC
  48. EFI_STATUS
  49. DiskImageSetDevice (
  50. IN CHAR16* Device
  51. )
  52. {
  53. DiskImage.DevicePath = (EFI_DEVICE_PATH *)ShellGetMap (Device);
  54. if (DiskImage.DevicePath == NULL) {
  55. return EFI_INVALID_PARAMETER;
  56. }
  57. return EFI_SUCCESS;
  58. }
  59. STATIC
  60. EFI_STATUS
  61. DiskImageSetOffset (
  62. IN UINT64 Offset
  63. )
  64. {
  65. DiskImage.Offset = Offset;
  66. return EFI_SUCCESS;
  67. }
  68. STATIC
  69. EFI_STATUS
  70. DiskImageSetSize (
  71. IN UINTN Size
  72. )
  73. {
  74. DiskImage.Size = Size;
  75. return EFI_SUCCESS;
  76. }
  77. STATIC
  78. EFI_STATUS
  79. DiskImageCleanup (
  80. VOID
  81. )
  82. {
  83. DiskImage.Offset = 0;
  84. DiskImage.Size = 0;
  85. DiskImage.DevicePath = NULL;
  86. BufferImage.ImageCleanup = Nothing;
  87. BufferImage.Open = Nothing;
  88. BufferImage.Close = Nothing;
  89. BufferImage.Read = Nothing;
  90. BufferImage.Write = Nothing;
  91. return EFI_SUCCESS;
  92. }
  93. STATIC
  94. EFI_STATUS
  95. DiskImageOpen (
  96. VOID
  97. )
  98. {
  99. EFI_STATUS Status;
  100. if (DiskImage.DevicePath == NULL) {
  101. return EFI_INVALID_PARAMETER;
  102. }
  103. Status = LibDevicePathToInterface (&BlockIoProtocol, DiskImage.DevicePath, &DiskImage.BlkIo);
  104. if (EFI_ERROR(Status)) {
  105. EditorError(Status,L"Device Not a BlockIo Device");
  106. return Status;
  107. }
  108. return EFI_SUCCESS;
  109. }
  110. STATIC
  111. EFI_STATUS
  112. DiskImageRead (
  113. VOID
  114. )
  115. {
  116. VOID *Buffer;
  117. CHAR16 *Str;
  118. EFI_BLOCK_IO *BlockIo = DiskImage.BlkIo;
  119. UINTN Bytes;
  120. EFI_STATUS Status;
  121. if (DiskImage.Offset > MultU64x32 (BlockIo->Media->LastBlock, BlockIo->Media->BlockSize)) {
  122. DiskImage.Offset = 0;
  123. }
  124. Bytes = BlockIo->Media->BlockSize*DiskImage.Size;
  125. Buffer = AllocatePool (Bytes);
  126. if (Buffer == NULL) {
  127. EditorError(EFI_OUT_OF_RESOURCES,L"DiskImageRead: Could not allocate memory for buffer");
  128. return EFI_OUT_OF_RESOURCES;
  129. }
  130. Status = BlockIo->ReadBlocks (
  131. BlockIo,
  132. BlockIo->Media->MediaId,
  133. DiskImage.Offset,
  134. Bytes,
  135. Buffer
  136. );
  137. if (EFI_ERROR(Status)) {
  138. EditorError(Status,L"DiskImageRead: Error in reading");
  139. } else {
  140. BufferToList(BufferImage.ListHead,Bytes,Buffer);
  141. }
  142. FreePool(Buffer);
  143. Str = PoolPrint(L"%d Bytes Read",Bytes);
  144. MainEditor.StatusBar->SetStatusString(Str);
  145. FreePool (Str);
  146. return Status;
  147. }
  148. STATIC
  149. EFI_STATUS
  150. DiskImageClose (
  151. VOID
  152. )
  153. {
  154. LineDeleteAll(BufferImage.ListHead);
  155. BufferImage.NumBytes = 0;
  156. MainEditor.FileBuffer->Offset = 0x00;
  157. MainEditor.StatusBar->SetOffset(0x00);
  158. MainEditor.FileBuffer->LowVisibleOffset = 0x00;
  159. MainEditor.FileBuffer->SetPosition(DISP_START_ROW,HEX_POSITION);
  160. MainEditor.FileModified = FALSE;
  161. return EFI_SUCCESS;
  162. }
  163. STATIC
  164. EFI_STATUS
  165. DiskImageWrite (
  166. VOID
  167. )
  168. {
  169. EFI_STATUS Status;
  170. VOID *Buffer;
  171. UINTN Bytes;
  172. DiskImage.Size = MainEditor.BufferImage->NumBytes;
  173. Bytes = DiskImage.BlkIo->Media->BlockSize*DiskImage.Size;
  174. Buffer = AllocatePool (Bytes);
  175. if (Buffer == NULL) {
  176. EditorError(EFI_OUT_OF_RESOURCES,L"DiskImageWrite: Could not allocate memory for buffer");
  177. return EFI_OUT_OF_RESOURCES;
  178. }
  179. /*
  180. * Convert from list to buffer
  181. */
  182. Status = DiskImage.BlkIo->WriteBlocks(DiskImage.BlkIo, DiskImage.BlkIo->Media->MediaId, DiskImage.Offset, Bytes, Buffer);
  183. if (EFI_ERROR(Status)) {
  184. EditorError(Status,L"DiskImageWrite: Error in writing");
  185. }
  186. FreePool(Buffer);
  187. return Status;
  188. }
  189. #endif /* _LIB_DISK_IMAGE */