2 * Greybus module manifest parsing
4 * Copyright 2014 Google Inc.
6 * Released under the GPLv2 only.
9 #define pr_fmt(fmt) KBUILD_MODNAME ": " fmt
11 #include <linux/err.h>
16 * We scan the manifest once to identify where all the descriptors
17 * are. The result is a list of these manifest_desc structures. We
18 * then pick through them for what we're looking for (starting with
19 * the module descriptor). As each is processed we remove it from
20 * the list. When we're done the list should (probably) be empty.
22 struct manifest_desc {
23 struct list_head links;
27 enum greybus_descriptor_type type;
30 static LIST_HEAD(manifest_descs);
32 static void release_manifest_descriptor(struct manifest_desc *descriptor)
34 list_del(&descriptor->links);
38 static void release_manifest_descriptors(void)
40 struct manifest_desc *descriptor;
41 struct manifest_desc *next;
43 list_for_each_entry_safe(descriptor, next, &manifest_descs, links)
44 release_manifest_descriptor(descriptor);
48 * Validate the given descriptor. Its reported size must fit within
49 * the number of bytes reamining, and it must have a recognized
50 * type. Check that the reported size is at least as big as what
51 * we expect to see. (It could be bigger, perhaps for a new version
54 * Returns the number of bytes consumed by the descriptor, or a
57 static int identify_descriptor(struct greybus_descriptor *desc, size_t size)
59 struct greybus_descriptor_header *desc_header = &desc->header;
60 struct manifest_desc *descriptor;
64 if (size < sizeof(*desc_header)) {
65 pr_err("manifest too small\n");
66 return -EINVAL; /* Must at least have header */
69 desc_size = (int)le16_to_cpu(desc_header->size);
70 if ((size_t)desc_size > size) {
71 pr_err("descriptor too big\n");
75 switch (desc_header->type) {
76 case GREYBUS_TYPE_MODULE:
77 if (desc_size < sizeof(struct greybus_descriptor_module)) {
78 pr_err("module descriptor too small (%u)\n",
83 case GREYBUS_TYPE_DEVICE:
85 case GREYBUS_TYPE_CLASS:
86 pr_err("class descriptor found (ignoring)\n");
88 case GREYBUS_TYPE_STRING:
89 expected_size = sizeof(struct greybus_descriptor_header);
90 expected_size += sizeof(struct greybus_descriptor_string);
91 expected_size += (size_t)desc->string.length;
92 if (desc_size < expected_size) {
93 pr_err("string descriptor too small (%u)\n",
98 case GREYBUS_TYPE_CPORT:
99 if (desc_size < sizeof(struct greybus_descriptor_cport)) {
100 pr_err("cport descriptor too small (%u)\n",
105 case GREYBUS_TYPE_INVALID:
107 pr_err("invalid descriptor type (%hhu)\n", desc_header->type);
111 descriptor = kzalloc(sizeof(*descriptor), GFP_KERNEL);
115 descriptor->size = desc_size;
116 descriptor->data = desc;
117 descriptor->type = desc_header->type;
118 list_add_tail(&descriptor->links, &manifest_descs);
124 * Find the string descriptor having the given id, validate it, and
125 * allocate a duplicate copy of it. The duplicate has an extra byte
126 * which guarantees the returned string is NUL-terminated.
128 * String index 0 is valid (it represents "no string"), and for
129 * that a null pointer is returned.
131 * Otherwise returns a pointer to a newly-allocated copy of the
132 * descriptor string, or an error-coded pointer on failure.
134 static char *gb_string_get(u8 string_id)
136 struct greybus_descriptor_string *desc_string;
137 struct manifest_desc *descriptor;
141 /* A zero string id means no string (but no error) */
145 list_for_each_entry(descriptor, &manifest_descs, links) {
146 struct greybus_descriptor *desc;
148 if (descriptor->type != GREYBUS_TYPE_STRING)
151 desc = descriptor->data;
152 desc_string = &desc->string;
153 if (desc_string->id == string_id) {
159 return ERR_PTR(-ENOENT);
161 /* Allocate an extra byte so we can guarantee it's NUL-terminated */
162 string = kmemdup(&desc_string->string, (size_t)desc_string->length + 1,
165 return ERR_PTR(-ENOMEM);
166 string[desc_string->length] = '\0';
168 /* Ok we've used this string, so we're done with it */
169 release_manifest_descriptor(descriptor);
174 struct gb_module *gb_manifest_parse_module(struct manifest_desc *module_desc)
176 struct greybus_descriptor *desc = module_desc->data;
177 struct greybus_descriptor_module *desc_module = &desc->module;
178 struct gb_module *gmod;
180 gmod = kzalloc(sizeof(*gmod), GFP_KERNEL);
184 /* Handle the strings first--they can fail */
185 gmod->vendor_string = gb_string_get(desc_module->vendor_stringid);
186 if (IS_ERR(gmod->vendor_string)) {
190 gmod->product_string = gb_string_get(desc_module->product_stringid);
191 if (IS_ERR(gmod->product_string)) {
192 kfree(gmod->vendor_string);
197 gmod->vendor = le16_to_cpu(desc_module->vendor);
198 gmod->product = le16_to_cpu(desc_module->product);
199 gmod->version = le16_to_cpu(desc_module->version);
200 gmod->serial_number = le64_to_cpu(desc_module->serial_number);
202 /* Release the module descriptor, now that we're done with it */
203 release_manifest_descriptor(module_desc);
209 * Parse a buffer containing a module manifest.
211 * If we find anything wrong with the content/format of the buffer
214 * The first requirement is that the manifest's version is
217 * We make an initial pass through the buffer and identify all of
218 * the descriptors it contains, keeping track for each its type
219 * and the location size of its data in the buffer.
221 * Next we scan the descriptors, looking for a module descriptor;
222 * there must be exactly one of those. When found, we record the
223 * information it contains, and then remove that descriptor (and any
224 * string descriptors it refers to) from further consideration.
226 * After that we look for the module's interfaces--there must be at
227 * least one of those.
229 * Return a pointer to an initialized gb_module structure
230 * representing the content of the module manifest, or a null
231 * pointer if an error occurs.
233 struct gb_module *gb_manifest_parse(void *data, size_t size)
235 struct greybus_manifest *manifest;
236 struct greybus_manifest_header *header;
237 struct greybus_descriptor *desc;
238 struct manifest_desc *descriptor;
239 struct manifest_desc *module_desc = NULL;
240 struct gb_module *gmod;
244 /* we have to have at _least_ the manifest header */
245 if (size <= sizeof(manifest->header)) {
246 pr_err("short manifest (%zu)\n", size);
250 /* Make sure the size is right */
252 header = &manifest->header;
253 manifest_size = le16_to_cpu(header->size);
254 if (manifest_size != size) {
255 pr_err("manifest size mismatch %zu != %hu\n",
256 size, manifest_size);
260 /* Validate major/minor number */
261 if (header->version_major > GREYBUS_VERSION_MAJOR) {
262 pr_err("manifest version too new (%hhu.%hhu > %hhu.%hhu)\n",
263 header->version_major, header->version_minor,
264 GREYBUS_VERSION_MAJOR, GREYBUS_VERSION_MINOR);
268 /* OK, find all the descriptors */
269 desc = (struct greybus_descriptor *)(header + 1);
270 size -= sizeof(*header);
274 desc_size = identify_descriptor(desc, size);
275 if (desc_size <= 0) {
277 pr_err("zero-sized manifest descriptor\n");
280 desc = (struct greybus_descriptor *)((char *)desc + desc_size);
284 /* There must be a single module descriptor */
285 list_for_each_entry(descriptor, &manifest_descs, links) {
286 if (descriptor->type == GREYBUS_TYPE_MODULE)
288 module_desc = descriptor;
291 pr_err("manifest must have 1 module descriptor (%u found)\n",
296 /* Parse the module manifest, starting with the module descriptor */
297 gmod = gb_manifest_parse_module(module_desc);
300 * We really should have no remaining descriptors, but we
301 * don't know what newer format manifests might leave.
303 if (!list_empty(&manifest_descs)) {
304 pr_info("excess descriptors in module manifest\n");
305 release_manifest_descriptors();
310 release_manifest_descriptors();