2014-10-01 20:54:16 -06:00
|
|
|
/*
|
|
|
|
* Greybus module manifest parsing
|
|
|
|
*
|
|
|
|
* Copyright 2014 Google Inc.
|
|
|
|
*
|
|
|
|
* Released under the GPLv2 only.
|
|
|
|
*/
|
|
|
|
|
|
|
|
#define pr_fmt(fmt) KBUILD_MODNAME ": " fmt
|
|
|
|
|
|
|
|
#include <linux/err.h>
|
|
|
|
|
|
|
|
#include "greybus.h"
|
|
|
|
|
|
|
|
/*
|
|
|
|
* We scan the manifest once to identify where all the descriptors
|
|
|
|
* are. The result is a list of these manifest_desc structures. We
|
|
|
|
* then pick through them for what we're looking for (starting with
|
|
|
|
* the module descriptor). As each is processed we remove it from
|
|
|
|
* the list. When we're done the list should (probably) be empty.
|
|
|
|
*/
|
|
|
|
struct manifest_desc {
|
|
|
|
struct list_head links;
|
|
|
|
|
|
|
|
size_t size;
|
|
|
|
void *data;
|
|
|
|
enum greybus_descriptor_type type;
|
|
|
|
};
|
|
|
|
|
|
|
|
static LIST_HEAD(manifest_descs);
|
|
|
|
|
|
|
|
static void release_manifest_descriptor(struct manifest_desc *descriptor)
|
|
|
|
{
|
|
|
|
list_del(&descriptor->links);
|
|
|
|
kfree(descriptor);
|
|
|
|
}
|
|
|
|
|
|
|
|
static void release_manifest_descriptors(void)
|
|
|
|
{
|
|
|
|
struct manifest_desc *descriptor;
|
|
|
|
struct manifest_desc *next;
|
|
|
|
|
|
|
|
list_for_each_entry_safe(descriptor, next, &manifest_descs, links)
|
|
|
|
release_manifest_descriptor(descriptor);
|
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
|
|
|
* Validate the given descriptor. Its reported size must fit within
|
2014-11-20 22:56:30 -07:00
|
|
|
* the number of bytes remaining, and it must have a recognized
|
2014-10-01 20:54:16 -06:00
|
|
|
* type. Check that the reported size is at least as big as what
|
|
|
|
* we expect to see. (It could be bigger, perhaps for a new version
|
|
|
|
* of the format.)
|
|
|
|
*
|
|
|
|
* Returns the number of bytes consumed by the descriptor, or a
|
|
|
|
* negative errno.
|
|
|
|
*/
|
2014-11-14 15:37:56 -07:00
|
|
|
static int identify_descriptor(struct greybus_descriptor *desc, size_t size)
|
2014-10-01 20:54:16 -06:00
|
|
|
{
|
|
|
|
struct greybus_descriptor_header *desc_header = &desc->header;
|
|
|
|
struct manifest_desc *descriptor;
|
|
|
|
int desc_size;
|
|
|
|
size_t expected_size;
|
|
|
|
|
|
|
|
if (size < sizeof(*desc_header)) {
|
|
|
|
pr_err("manifest too small\n");
|
|
|
|
return -EINVAL; /* Must at least have header */
|
|
|
|
}
|
|
|
|
|
|
|
|
desc_size = (int)le16_to_cpu(desc_header->size);
|
|
|
|
if ((size_t)desc_size > size) {
|
|
|
|
pr_err("descriptor too big\n");
|
|
|
|
return -EINVAL;
|
|
|
|
}
|
|
|
|
|
|
|
|
switch (desc_header->type) {
|
|
|
|
case GREYBUS_TYPE_MODULE:
|
|
|
|
if (desc_size < sizeof(struct greybus_descriptor_module)) {
|
|
|
|
pr_err("module descriptor too small (%u)\n",
|
|
|
|
desc_size);
|
|
|
|
return -EINVAL;
|
|
|
|
}
|
|
|
|
break;
|
|
|
|
case GREYBUS_TYPE_STRING:
|
2014-11-19 04:54:58 -07:00
|
|
|
expected_size = sizeof(*desc_header);
|
2014-10-01 20:54:16 -06:00
|
|
|
expected_size += sizeof(struct greybus_descriptor_string);
|
|
|
|
expected_size += (size_t)desc->string.length;
|
|
|
|
if (desc_size < expected_size) {
|
|
|
|
pr_err("string descriptor too small (%u)\n",
|
|
|
|
desc_size);
|
|
|
|
return -EINVAL;
|
|
|
|
}
|
|
|
|
break;
|
2014-10-02 11:30:02 -06:00
|
|
|
case GREYBUS_TYPE_INTERFACE:
|
|
|
|
break;
|
2014-10-01 20:54:16 -06:00
|
|
|
case GREYBUS_TYPE_CPORT:
|
|
|
|
if (desc_size < sizeof(struct greybus_descriptor_cport)) {
|
|
|
|
pr_err("cport descriptor too small (%u)\n",
|
|
|
|
desc_size);
|
|
|
|
return -EINVAL;
|
|
|
|
}
|
|
|
|
break;
|
2014-10-02 11:30:02 -06:00
|
|
|
case GREYBUS_TYPE_CLASS:
|
|
|
|
pr_warn("class descriptor found (ignoring)\n");
|
|
|
|
break;
|
2014-10-01 20:54:16 -06:00
|
|
|
case GREYBUS_TYPE_INVALID:
|
|
|
|
default:
|
|
|
|
pr_err("invalid descriptor type (%hhu)\n", desc_header->type);
|
|
|
|
return -EINVAL;
|
|
|
|
}
|
|
|
|
|
|
|
|
descriptor = kzalloc(sizeof(*descriptor), GFP_KERNEL);
|
|
|
|
if (!descriptor)
|
|
|
|
return -ENOMEM;
|
|
|
|
|
|
|
|
descriptor->size = desc_size;
|
2014-10-06 07:58:44 -06:00
|
|
|
descriptor->data = (u8 *)desc + sizeof(*desc_header);
|
2014-10-01 20:54:16 -06:00
|
|
|
descriptor->type = desc_header->type;
|
|
|
|
list_add_tail(&descriptor->links, &manifest_descs);
|
|
|
|
|
|
|
|
return desc_size;
|
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
|
|
|
* Find the string descriptor having the given id, validate it, and
|
|
|
|
* allocate a duplicate copy of it. The duplicate has an extra byte
|
|
|
|
* which guarantees the returned string is NUL-terminated.
|
|
|
|
*
|
|
|
|
* String index 0 is valid (it represents "no string"), and for
|
|
|
|
* that a null pointer is returned.
|
|
|
|
*
|
|
|
|
* Otherwise returns a pointer to a newly-allocated copy of the
|
|
|
|
* descriptor string, or an error-coded pointer on failure.
|
|
|
|
*/
|
|
|
|
static char *gb_string_get(u8 string_id)
|
|
|
|
{
|
|
|
|
struct greybus_descriptor_string *desc_string;
|
|
|
|
struct manifest_desc *descriptor;
|
|
|
|
bool found = false;
|
|
|
|
char *string;
|
|
|
|
|
|
|
|
/* A zero string id means no string (but no error) */
|
|
|
|
if (!string_id)
|
|
|
|
return NULL;
|
|
|
|
|
|
|
|
list_for_each_entry(descriptor, &manifest_descs, links) {
|
|
|
|
|
|
|
|
if (descriptor->type != GREYBUS_TYPE_STRING)
|
|
|
|
continue;
|
|
|
|
|
2014-10-06 07:58:44 -06:00
|
|
|
desc_string = descriptor->data;
|
2014-10-01 20:54:16 -06:00
|
|
|
if (desc_string->id == string_id) {
|
|
|
|
found = true;
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
if (!found)
|
|
|
|
return ERR_PTR(-ENOENT);
|
|
|
|
|
|
|
|
/* Allocate an extra byte so we can guarantee it's NUL-terminated */
|
|
|
|
string = kmemdup(&desc_string->string, (size_t)desc_string->length + 1,
|
|
|
|
GFP_KERNEL);
|
|
|
|
if (!string)
|
|
|
|
return ERR_PTR(-ENOMEM);
|
|
|
|
string[desc_string->length] = '\0';
|
|
|
|
|
|
|
|
/* Ok we've used this string, so we're done with it */
|
|
|
|
release_manifest_descriptor(descriptor);
|
|
|
|
|
|
|
|
return string;
|
|
|
|
}
|
|
|
|
|
2014-10-01 20:54:18 -06:00
|
|
|
/*
|
|
|
|
* Find cport descriptors in the manifest and set up data structures
|
|
|
|
* for the functions that use them. Returns the number of interfaces
|
|
|
|
* set up for the given module, or 0 if there is an error.
|
|
|
|
*/
|
2014-10-02 22:26:26 -06:00
|
|
|
static u32 gb_manifest_parse_cports(struct gb_interface *interface)
|
2014-10-01 20:54:18 -06:00
|
|
|
{
|
|
|
|
u32 count = 0;
|
|
|
|
|
|
|
|
while (true) {
|
|
|
|
struct manifest_desc *descriptor;
|
|
|
|
struct greybus_descriptor_cport *desc_cport;
|
2014-10-28 18:35:59 -06:00
|
|
|
u8 protocol_id;
|
2014-10-01 20:54:18 -06:00
|
|
|
u16 cport_id;
|
2014-11-13 05:44:31 -07:00
|
|
|
bool found = false;
|
2014-10-01 20:54:18 -06:00
|
|
|
|
|
|
|
/* Find a cport descriptor */
|
|
|
|
list_for_each_entry(descriptor, &manifest_descs, links) {
|
|
|
|
if (descriptor->type == GREYBUS_TYPE_CPORT) {
|
2014-10-02 11:30:02 -06:00
|
|
|
desc_cport = descriptor->data;
|
|
|
|
if (desc_cport->interface == interface->id) {
|
|
|
|
found = true;
|
|
|
|
break;
|
|
|
|
}
|
2014-10-01 20:54:18 -06:00
|
|
|
}
|
|
|
|
}
|
|
|
|
if (!found)
|
|
|
|
break;
|
|
|
|
|
|
|
|
/* Found one. Set up its function structure */
|
2014-10-28 18:35:59 -06:00
|
|
|
protocol_id = desc_cport->protocol_id;
|
2014-10-01 20:54:18 -06:00
|
|
|
cport_id = le16_to_cpu(desc_cport->id);
|
2014-10-28 18:35:59 -06:00
|
|
|
if (!gb_connection_create(interface, cport_id, protocol_id))
|
2014-10-01 20:54:18 -06:00
|
|
|
return 0; /* Error */
|
|
|
|
|
|
|
|
count++;
|
|
|
|
/* Release the cport descriptor */
|
|
|
|
release_manifest_descriptor(descriptor);
|
|
|
|
}
|
|
|
|
|
|
|
|
return count;
|
|
|
|
}
|
|
|
|
|
2014-10-01 20:54:17 -06:00
|
|
|
/*
|
|
|
|
* Find interface descriptors in the manifest and set up their data
|
|
|
|
* structures. Returns the number of interfaces set up for the
|
|
|
|
* given module.
|
|
|
|
*/
|
|
|
|
static u32 gb_manifest_parse_interfaces(struct gb_module *gmod)
|
|
|
|
{
|
|
|
|
u32 count = 0;
|
|
|
|
|
|
|
|
while (true) {
|
|
|
|
struct manifest_desc *descriptor;
|
|
|
|
struct greybus_descriptor_interface *desc_interface;
|
|
|
|
struct gb_interface *interface;
|
|
|
|
bool found = false;
|
|
|
|
|
|
|
|
/* Find an interface descriptor */
|
|
|
|
list_for_each_entry(descriptor, &manifest_descs, links) {
|
2014-10-02 11:30:02 -06:00
|
|
|
if (descriptor->type == GREYBUS_TYPE_INTERFACE) {
|
2014-10-01 20:54:17 -06:00
|
|
|
found = true;
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
if (!found)
|
|
|
|
break;
|
|
|
|
|
|
|
|
/* Found one. Set up its interface structure*/
|
|
|
|
desc_interface = descriptor->data;
|
|
|
|
interface = gb_interface_create(gmod, desc_interface->id);
|
|
|
|
if (!interface)
|
|
|
|
return 0; /* Error */
|
2014-10-01 20:54:18 -06:00
|
|
|
|
|
|
|
/* Now go set up this interface's functions and cports */
|
|
|
|
if (!gb_manifest_parse_cports(interface))
|
|
|
|
return 0; /* Error parsing cports */
|
|
|
|
|
2014-10-01 20:54:17 -06:00
|
|
|
count++;
|
|
|
|
|
|
|
|
/* Done with this interface descriptor */
|
|
|
|
release_manifest_descriptor(descriptor);
|
|
|
|
}
|
|
|
|
|
|
|
|
return count;
|
|
|
|
}
|
|
|
|
|
2014-10-03 13:14:25 -06:00
|
|
|
static bool gb_manifest_parse_module(struct gb_module *gmod,
|
|
|
|
struct manifest_desc *module_desc)
|
2014-10-01 20:54:16 -06:00
|
|
|
{
|
2014-10-06 07:58:44 -06:00
|
|
|
struct greybus_descriptor_module *desc_module = module_desc->data;
|
2014-10-01 20:54:16 -06:00
|
|
|
|
|
|
|
/* Handle the strings first--they can fail */
|
|
|
|
gmod->vendor_string = gb_string_get(desc_module->vendor_stringid);
|
2014-10-03 13:14:25 -06:00
|
|
|
if (IS_ERR(gmod->vendor_string))
|
|
|
|
return false;
|
|
|
|
|
2014-10-01 20:54:16 -06:00
|
|
|
gmod->product_string = gb_string_get(desc_module->product_stringid);
|
|
|
|
if (IS_ERR(gmod->product_string)) {
|
2014-11-13 05:44:32 -07:00
|
|
|
goto out_free_vendor_string;
|
2014-10-01 20:54:16 -06:00
|
|
|
}
|
|
|
|
|
|
|
|
gmod->vendor = le16_to_cpu(desc_module->vendor);
|
|
|
|
gmod->product = le16_to_cpu(desc_module->product);
|
|
|
|
gmod->version = le16_to_cpu(desc_module->version);
|
2014-10-02 11:30:02 -06:00
|
|
|
gmod->unique_id = le64_to_cpu(desc_module->unique_id);
|
2014-10-01 20:54:16 -06:00
|
|
|
|
|
|
|
/* Release the module descriptor, now that we're done with it */
|
|
|
|
release_manifest_descriptor(module_desc);
|
|
|
|
|
2014-10-01 20:54:17 -06:00
|
|
|
/* A module must have at least one interface descriptor */
|
|
|
|
if (!gb_manifest_parse_interfaces(gmod)) {
|
|
|
|
pr_err("manifest interface descriptors not valid\n");
|
2014-10-03 13:14:25 -06:00
|
|
|
goto out_err;
|
2014-10-01 20:54:17 -06:00
|
|
|
}
|
|
|
|
|
2014-10-03 13:14:25 -06:00
|
|
|
return true;
|
|
|
|
out_err:
|
|
|
|
kfree(gmod->product_string);
|
|
|
|
gmod->product_string = NULL;
|
2014-11-13 05:44:32 -07:00
|
|
|
out_free_vendor_string:
|
2014-10-03 13:14:25 -06:00
|
|
|
kfree(gmod->vendor_string);
|
|
|
|
gmod->vendor_string = NULL;
|
|
|
|
|
|
|
|
return false;
|
2014-10-01 20:54:16 -06:00
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
|
|
|
* Parse a buffer containing a module manifest.
|
|
|
|
*
|
|
|
|
* If we find anything wrong with the content/format of the buffer
|
|
|
|
* we reject it.
|
|
|
|
*
|
|
|
|
* The first requirement is that the manifest's version is
|
|
|
|
* one we can parse.
|
|
|
|
*
|
|
|
|
* We make an initial pass through the buffer and identify all of
|
|
|
|
* the descriptors it contains, keeping track for each its type
|
|
|
|
* and the location size of its data in the buffer.
|
|
|
|
*
|
2014-11-14 15:37:56 -07:00
|
|
|
* Next we scan the descriptors, looking for a module descriptor;
|
2014-10-01 20:54:16 -06:00
|
|
|
* there must be exactly one of those. When found, we record the
|
|
|
|
* information it contains, and then remove that descriptor (and any
|
|
|
|
* string descriptors it refers to) from further consideration.
|
|
|
|
*
|
|
|
|
* After that we look for the module's interfaces--there must be at
|
|
|
|
* least one of those.
|
|
|
|
*
|
2014-10-03 13:14:25 -06:00
|
|
|
* Returns true if parsing was successful, false otherwise.
|
2014-10-01 20:54:16 -06:00
|
|
|
*/
|
2014-10-03 13:14:25 -06:00
|
|
|
bool gb_manifest_parse(struct gb_module *gmod, void *data, size_t size)
|
2014-10-01 20:54:16 -06:00
|
|
|
{
|
|
|
|
struct greybus_manifest *manifest;
|
|
|
|
struct greybus_manifest_header *header;
|
|
|
|
struct greybus_descriptor *desc;
|
|
|
|
struct manifest_desc *descriptor;
|
2014-10-04 19:43:41 -06:00
|
|
|
struct manifest_desc *module_desc = NULL;
|
2014-10-01 20:54:16 -06:00
|
|
|
u16 manifest_size;
|
|
|
|
u32 found = 0;
|
2014-11-13 05:44:30 -07:00
|
|
|
bool result;
|
2014-10-01 20:54:16 -06:00
|
|
|
|
2014-11-14 04:55:03 -07:00
|
|
|
/* Manifest descriptor list should be empty here */
|
|
|
|
if (WARN_ON(!list_empty(&manifest_descs)))
|
|
|
|
return false;
|
|
|
|
|
2014-10-01 20:54:16 -06:00
|
|
|
/* we have to have at _least_ the manifest header */
|
|
|
|
if (size <= sizeof(manifest->header)) {
|
|
|
|
pr_err("short manifest (%zu)\n", size);
|
2014-10-03 13:14:25 -06:00
|
|
|
return false;
|
2014-10-01 20:54:16 -06:00
|
|
|
}
|
|
|
|
|
|
|
|
/* Make sure the size is right */
|
|
|
|
manifest = data;
|
|
|
|
header = &manifest->header;
|
|
|
|
manifest_size = le16_to_cpu(header->size);
|
|
|
|
if (manifest_size != size) {
|
|
|
|
pr_err("manifest size mismatch %zu != %hu\n",
|
|
|
|
size, manifest_size);
|
2014-10-03 13:14:25 -06:00
|
|
|
return false;
|
2014-10-01 20:54:16 -06:00
|
|
|
}
|
|
|
|
|
|
|
|
/* Validate major/minor number */
|
|
|
|
if (header->version_major > GREYBUS_VERSION_MAJOR) {
|
|
|
|
pr_err("manifest version too new (%hhu.%hhu > %hhu.%hhu)\n",
|
|
|
|
header->version_major, header->version_minor,
|
|
|
|
GREYBUS_VERSION_MAJOR, GREYBUS_VERSION_MINOR);
|
2014-10-03 13:14:25 -06:00
|
|
|
return false;
|
2014-10-01 20:54:16 -06:00
|
|
|
}
|
|
|
|
|
|
|
|
/* OK, find all the descriptors */
|
|
|
|
desc = (struct greybus_descriptor *)(header + 1);
|
|
|
|
size -= sizeof(*header);
|
|
|
|
while (size) {
|
|
|
|
int desc_size;
|
|
|
|
|
2014-11-14 15:37:56 -07:00
|
|
|
desc_size = identify_descriptor(desc, size);
|
2014-10-01 20:54:16 -06:00
|
|
|
if (desc_size <= 0) {
|
|
|
|
if (!desc_size)
|
|
|
|
pr_err("zero-sized manifest descriptor\n");
|
2014-10-06 11:46:36 -06:00
|
|
|
result = false;
|
2014-10-03 13:14:25 -06:00
|
|
|
goto out;
|
2014-10-01 20:54:16 -06:00
|
|
|
}
|
|
|
|
desc = (struct greybus_descriptor *)((char *)desc + desc_size);
|
|
|
|
size -= desc_size;
|
2014-11-14 15:37:56 -07:00
|
|
|
}
|
2014-10-01 20:54:16 -06:00
|
|
|
|
2014-11-14 15:37:56 -07:00
|
|
|
/* There must be a single module descriptor */
|
|
|
|
list_for_each_entry(descriptor, &manifest_descs, links) {
|
|
|
|
if (descriptor->type == GREYBUS_TYPE_MODULE)
|
|
|
|
if (!found++)
|
2014-10-01 20:54:16 -06:00
|
|
|
module_desc = descriptor;
|
2014-11-14 15:37:56 -07:00
|
|
|
}
|
|
|
|
if (found != 1) {
|
|
|
|
pr_err("manifest must have 1 module descriptor (%u found)\n",
|
|
|
|
found);
|
|
|
|
result = false;
|
|
|
|
goto out;
|
2014-10-01 20:54:16 -06:00
|
|
|
}
|
|
|
|
|
|
|
|
/* Parse the module manifest, starting with the module descriptor */
|
2014-10-03 13:14:25 -06:00
|
|
|
result = gb_manifest_parse_module(gmod, module_desc);
|
2014-10-01 20:54:16 -06:00
|
|
|
|
|
|
|
/*
|
|
|
|
* We really should have no remaining descriptors, but we
|
|
|
|
* don't know what newer format manifests might leave.
|
|
|
|
*/
|
2014-11-05 15:03:11 -07:00
|
|
|
if (result && !list_empty(&manifest_descs))
|
2014-10-01 20:54:16 -06:00
|
|
|
pr_info("excess descriptors in module manifest\n");
|
2014-10-03 13:14:25 -06:00
|
|
|
out:
|
2014-10-01 20:54:16 -06:00
|
|
|
release_manifest_descriptors();
|
|
|
|
|
2014-10-06 11:46:36 -06:00
|
|
|
return result;
|
2014-10-01 20:54:16 -06:00
|
|
|
}
|