ReactOS  0.4.15-dev-1374-g8d3e80e
bootcode.c
Go to the documentation of this file.
1 /*
2  * PROJECT: ReactOS Setup Library
3  * LICENSE: GPL-2.0+ (https://spdx.org/licenses/GPL-2.0+)
4  * PURPOSE: BootCode support functions.
5  * COPYRIGHT: Copyright 2020 Hermes Belusca-Maito
6  */
7 
8 /* INCLUDES *****************************************************************/
9 
10 #include "precomp.h"
11 
12 #include "bootcode.h"
13 
14 #define NDEBUG
15 #include <debug.h>
16 
17 
18 /* FUNCTIONS ****************************************************************/
19 
22  IN OUT PBOOTCODE BootCodeInfo,
25 {
27  PVOID BootCode;
30 
31  ASSERT(BootCodeInfo);
32 
33  /* Normalize the bootcode length */
34  if (Length == 0 || Length == (ULONG)-1)
36 
37  /* Allocate a buffer for the bootcode */
39  if (BootCode == NULL)
41 
42  /* Read the bootcode from the file into the buffer */
43  FileOffset.QuadPart = 0ULL;
45  NULL,
46  NULL,
47  NULL,
49  BootCode,
50  Length,
51  &FileOffset,
52  NULL);
53  if (!NT_SUCCESS(Status))
54  {
55  RtlFreeHeap(ProcessHeap, 0, BootCode);
56  return Status;
57  }
58 
59  /* Update the bootcode information */
60  if (BootCodeInfo->BootCode)
61  RtlFreeHeap(ProcessHeap, 0, BootCodeInfo->BootCode);
62  BootCodeInfo->BootCode = BootCode;
63  BootCodeInfo->Length = Length;
64 
65  return STATUS_SUCCESS;
66 }
67 
70  IN OUT PBOOTCODE BootCodeInfo,
73 {
78 
79  ASSERT(BootCodeInfo);
80 
81  /* Open the file */
83  FilePath,
85  NULL,
86  NULL);
91  FILE_SHARE_READ | FILE_SHARE_WRITE, // Is FILE_SHARE_WRITE necessary?
93  if (!NT_SUCCESS(Status))
94  return Status;
95 
96  Status = ReadBootCodeByHandle(BootCodeInfo, FileHandle, Length);
97 
98  /* Close the file and return */
100  return Status;
101 }
102 
103 VOID
105  IN OUT PBOOTCODE BootCodeInfo)
106 {
107  ASSERT(BootCodeInfo);
108 
109  /* Update the bootcode information */
110  if (BootCodeInfo->BootCode)
111  RtlFreeHeap(ProcessHeap, 0, BootCodeInfo->BootCode);
112  BootCodeInfo->BootCode = NULL;
113  BootCodeInfo->Length = 0;
114 }
115 
116 /* EOF */
IN PUNICODE_STRING IN POBJECT_ATTRIBUTES ObjectAttributes
Definition: conport.c:35
#define IN
Definition: typedefs.h:39
#define STATUS_INSUFFICIENT_RESOURCES
Definition: udferr_usr.h:158
_In_ ULONG _In_ ULONG _In_ ULONG Length
Definition: ntddpcm.h:101
#define OBJ_CASE_INSENSITIVE
Definition: winternl.h:228
LONG NTSTATUS
Definition: precomp.h:26
BOOLEAN NTAPI RtlFreeHeap(IN PVOID HeapHandle, IN ULONG Flags, IN PVOID HeapBase)
Definition: heap.c:606
NTSTATUS ReadBootCodeFromFile(IN OUT PBOOTCODE BootCodeInfo, IN PUNICODE_STRING FilePath, IN ULONG Length OPTIONAL)
Definition: bootcode.c:69
#define FILE_SHARE_WRITE
Definition: nt_native.h:681
#define FILE_SHARE_READ
Definition: compat.h:136
PCWSTR FilePath
HANDLE FileHandle
Definition: stats.c:38
#define ULL(a, b)
Definition: format_msg.c:27
PVOID NTAPI RtlAllocateHeap(IN PVOID HeapHandle, IN ULONG Flags, IN SIZE_T Size)
Definition: heap.c:588
Status
Definition: gdiplustypes.h:24
NTSYSAPI NTSTATUS NTAPI NtOpenFile(OUT PHANDLE phFile, IN ACCESS_MASK DesiredAccess, IN POBJECT_ATTRIBUTES ObjectAttributes, OUT PIO_STATUS_BLOCK pIoStatusBlock, IN ULONG ShareMode, IN ULONG OpenMode)
Definition: file.c:3951
#define ASSERT(a)
Definition: mode.c:45
#define NT_SUCCESS(StatCode)
Definition: apphelp.c:32
NTSTATUS NTAPI NtClose(IN HANDLE Handle)
Definition: obhandle.c:3399
#define GENERIC_READ
Definition: compat.h:135
#define SYNCHRONIZE
Definition: nt_native.h:61
HANDLE ProcessHeap
Definition: servman.c:15
NTSTATUS ReadBootCodeByHandle(IN OUT PBOOTCODE BootCodeInfo, IN HANDLE FileHandle, IN ULONG Length OPTIONAL)
Definition: bootcode.c:21
VOID FreeBootCode(IN OUT PBOOTCODE BootCodeInfo)
Definition: bootcode.c:104
_In_ PFCB _In_ LONGLONG FileOffset
Definition: cdprocs.h:159
static OUT PIO_STATUS_BLOCK IoStatusBlock
Definition: pipe.c:75
#define NULL
Definition: types.h:112
#define HEAP_ZERO_MEMORY
Definition: compat.h:134
#define FILE_SYNCHRONOUS_IO_NONALERT
Definition: from_kernel.h:31
#define OUT
Definition: typedefs.h:40
unsigned int ULONG
Definition: retypes.h:1
#define InitializeObjectAttributes(p, n, a, r, s)
Definition: reg.c:106
#define STATUS_SUCCESS
Definition: shellext.h:65
#define SECTORSIZE
Definition: bootcode.h:13
NTSTATUS NTAPI NtReadFile(HANDLE FileHandle, HANDLE Event, PIO_APC_ROUTINE ApcRoutine, PVOID ApcContext, PIO_STATUS_BLOCK IoStatusBlock, PVOID Buffer, ULONG Length, PLARGE_INTEGER ByteOffset, PULONG Key)
PULONG MinorVersion OPTIONAL
Definition: CrossNt.h:68