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