]> git.proxmox.com Git - efi-boot-shim.git/blob - model.c
New upstream version 15.7
[efi-boot-shim.git] / model.c
1 // SPDX-License-Identifier: BSD-2-Clause-Patent
2 /*
3 * model.c - modeling file for coverity
4 * Copyright Peter Jones <pjones@redhat.com>
5 */
6
7 #ifndef __COVERITY__
8 /* This is so vim's Syntastic checker won't yell about all these. */
9 extern void __coverity_string_size_sanitize__(int);
10 extern void __coverity_negative_sink__(int);
11 extern void *__coverity_alloc_nosize__(void);
12 extern void __coverity_writeall0__(void *);
13 extern void *__coverity_alloc__(int);
14 extern void __coverity_sleep__();
15 extern void __coverity_tainted_data_sanitize__(void *);
16 extern void __coverity_free__(void *);
17 #endif
18
19 void *
20 OBJ_dup(void *o)
21 {
22 return __coverity_alloc_nosize__();
23 }
24
25 int
26 UTF8_getc(const unsigned char *str, int len, unsigned long *val)
27 {
28 /* You can't quite express the right thing here, so instead we're
29 * telling covscan that if len is a certain value, the string has
30 * been checked for having a NUL at the right place. Ideally what
31 * we'd tell it is it's never allowed to give us a string shorter
32 * than a certain length if certain bits (i.e. the UTF-8 surrogate
33 * length bits) are set. */
34 if (len <= 0) {
35 __coverity_string_size_sanitize__(0);
36 return 0;
37 } else if (len <= 6) {
38 __coverity_string_size_sanitize__(0);
39 return len;
40 }
41 return -2;
42 }
43
44 typedef unsigned long long u64;
45 typedef struct {
46 unsigned long long hi;
47 unsigned long long lo;
48 } u128;
49
50 void
51 gcm_gmult_4bit(u64 Xi[2], u128 Htable[16])
52 {
53 __coverity_tainted_data_sanitize__(Htable);
54 }
55
56 void
57 msleep(int n)
58 {
59 __coverity_sleep__();
60 }
61
62 /* From MdePkg/Include/Base.h or so */
63 typedef unsigned long long UINT64;
64 typedef unsigned long UINTN;
65 typedef long INTN;
66 typedef UINT64 EFI_PHYSICAL_ADDRESS;
67 typedef UINTN RETURN_STATUS;
68 typedef RETURN_STATUS EFI_STATUS;
69
70 #define MAX_BIT (1ULL << (sizeof (INTN) * 8 - 1))
71 #define MAX_INTN ((INTN)~MAX_BIT)
72
73 #define ENCODE_ERROR(StatusCode) ((RETURN_STATUS)(MAX_BIT | (StatusCode)))
74 #define ENCODE_WARNING(StatusCode) ((RETURN_STATUS)(StatusCode))
75 #define RETURN_ERROR(StatusCode) (((INTN)(RETURN_STATUS)(StatusCode)) < 0)
76 #define RETURN_SUCCESS 0
77 #define RETURN_INVALID_PARAMETER ENCODE_ERROR (2)
78 #define RETURN_OUT_OF_RESOURCES ENCODE_ERROR (9)
79
80 /* From MdePkg/Include/Uefi/UefiBaseType.h */
81 #define EFI_SUCCESS RETURN_SUCCESS
82 #define EFI_INVALID_PARAMETER RETURN_INVALID_PARAMETER
83 #define EFI_OUT_OF_RESOURCES RETURN_OUT_OF_RESOURCES
84
85 #define EFI_PAGE_MASK 0xFFF
86 #define EFI_PAGE_SHIFT 12
87 #define EFI_SIZE_TO_PAGES(a) (((a) >> EFI_PAGE_SHIFT) + (((a) & EFI_PAGE_MASK) ? 1 : 0))
88 #define EFI_PAGES_TO_SIZE(a) ((a) << EFI_PAGE_SHIFT)
89
90 /* From MdePkg/Include/Uefi/UefiMultiPhase.h */
91 typedef enum {
92 EfiReservedMemoryType,
93 EfiLoaderCode,
94 EfiLoaderData,
95 EfiBootServicesCode,
96 EfiBootServicesData,
97 EfiRuntimeServicesCode,
98 EfiRuntimeServicesData,
99 EfiConventionalMemory,
100 EfiUnusableMemory,
101 EfiACPIReclaimMemory,
102 EfiACPIMemoryNVS,
103 EfiMemoryMappedIO,
104 EfiMemoryMappedIOPortSpace,
105 EfiPalCode,
106 EfiPersistentMemory,
107 EfiMaxMemoryType
108 } EFI_MEMORY_TYPE;
109
110 /* From MdePkg/Include/Uefi/UefiSpec.h */
111 typedef enum {
112 AllocateAnyPages,
113 AllocateMaxAddress,
114 AllocateAddress,
115 MaxAllocateType
116 } EFI_ALLOCATE_TYPE;
117
118 EFI_STATUS
119 AllocatePages(EFI_ALLOCATE_TYPE Type,
120 EFI_MEMORY_TYPE MemoryType,
121 unsigned long Pages,
122 EFI_PHYSICAL_ADDRESS *Memory)
123 {
124 int has_memory;
125 unsigned long bytes = EFI_PAGES_TO_SIZE(Pages);
126
127 if (Pages >= (unsigned long)((-1L) >> EFI_PAGE_SHIFT))
128 return EFI_INVALID_PARAMETER;
129
130 __coverity_negative_sink__(bytes);
131 if (has_memory) {
132 *Memory = (EFI_PHYSICAL_ADDRESS)__coverity_alloc__(bytes);
133 return EFI_SUCCESS;
134 }
135 return EFI_OUT_OF_RESOURCES;
136 }
137
138 void *
139 AllocateZeroPool(int sz)
140 {
141 void *ptr;
142
143 __coverity_negative_sink__(sz);
144 ptr = __coverity_alloc__(sz);
145 __coverity_writeall0__(ptr);
146 return ptr;
147 }
148
149 void
150 FreePool(void *ptr)
151 {
152 __coverity_free__(ptr);
153 }
154
155 // vim:fenc=utf-8:tw=75