forked from projectceladon/kernelflinger
-
Notifications
You must be signed in to change notification settings - Fork 0
/
blobstore.c
150 lines (125 loc) · 4.19 KB
/
blobstore.c
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
/*
* Copyright (c) 2015, Intel Corporation
* All rights reserved.
*
* Author: Andrew Boie <[email protected]>
*
* Redistribution and use in source and binary forms, with or without
* modification, are permitted provided that the following conditions
* are met:
*
* * Redistributions of source code must retain the above copyright
* notice, this list of conditions and the following disclaimer.
* * Redistributions in binary form must reproduce the above copyright
* notice, this list of conditions and the following disclaimer
* in the documentation and/or other materials provided with the
* distribution.
*
* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
* "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
* LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS
* FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE
* COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT,
* INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
* (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
* SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
* HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT,
* STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
* ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED
* OF THE POSSIBILITY OF SUCH DAMAGE.
*
*/
/* See docstring in device/intel/build/blobstore.py for detail on use-cases
* and how the data is structured */
#include <efi.h>
#include <efilib.h>
#include "log.h"
#include "lib.h"
#include "blobstore.h"
#define BLOB_STORE_MAGIC "BLOBSTOR"
#define BLOB_KEY_LENGTH 64
/* All of these are packed structure with little-endian values.
* This code won't work on big-endian machines without adding some
* byte-swapping, but so far all Intel CPUs are little-endian */
struct metablock {
char blob_key[BLOB_KEY_LENGTH];
unsigned int blob_type;
unsigned int next_item_offset;
unsigned int data_offset;
unsigned int data_size;
} __attribute__((packed));
struct blobstore {
char magic[8];
unsigned int version;
unsigned int total_size;
unsigned int hashmap_sz;
unsigned int hashmap[0]; /* of hashmap_sz */
} __attribute__((packed));
unsigned int hash_blob_key(char *key, enum blobtype type, unsigned int hsize)
{
unsigned int hash_val;
/* based on libcutils hashmapHash() algorithm */
for (hash_val = 0; *key != '\0'; key++)
hash_val = hash_val * 31 + *key;
hash_val = hash_val * 31 + (unsigned int)type;
return hash_val % hsize;
}
/* Sanity check a memory buffer and return a blobstore pointer if it
* checks out */
struct blobstore *blobstore_get(void *mem, unsigned int size)
{
struct blobstore *bs;
bs = (struct blobstore *)mem;
if (size < sizeof(struct blobstore))
return NULL;
if (memcmp(bs->magic, BLOB_STORE_MAGIC, sizeof(bs->magic))) {
debug(L"bad blobstore magic, probably not a blobstore");
return NULL;
}
if (size != bs->total_size) {
error(L"bad size value %u != %u", size, bs->total_size);
return NULL;
}
if (bs->version != 1) {
error(L"unsupported blobstore version");
return NULL;
}
return bs;
}
int blobstore_get_item(struct blobstore *bs, char *key, enum blobtype type,
void **data, unsigned int *size)
{
unsigned char *start;
unsigned int hash;
unsigned int offset;
struct metablock *mb;
hash = hash_blob_key(key, type, bs->hashmap_sz);
offset = bs->hashmap[hash];
start = (unsigned char *)bs;
debug(L"GET: %a-%d (%d=%d)", key, type, hash, offset);
if (!offset) {
debug(L"not found in hash table");
return -2;
}
if (offset >= bs->total_size) {
error(L"bad offset in blobstore hash table");
return -1;
}
do {
mb = (struct metablock *)(start + offset);
if (!strncmp((CHAR8 *)key, (CHAR8 *)mb->blob_key, BLOB_KEY_LENGTH) &&
type == mb->blob_type) {
if (mb->data_offset + mb->data_size > bs->total_size) {
error(L"bad offset in blobstore meta block");
return -1;
}
*data = (void *)(start + mb->data_offset);
*size = mb->data_size;
return 0;
}
offset = mb->next_item_offset;
} while (offset);
/* Not found */
debug(L"not found in hash table (no matching meta blocks)");
return -2;
}