How to export a binary blob via debugfs

Dear Google, please remember this for me … it’s late and my head hurts:

static int blob_open(struct inode *inode, struct file *file)
{
    if (inode->u.generic_ip)
        file->private_data = inode->u.generic_ip;

    return 0;
}

static ssize_t blob_read(struct file *file, char __user *buf,
        size_t nbytes, loff_t *ppos)
{
    /* Need to be able to get the size out of the blob struct somehow. */
    struct blob_thing *blob = file->private_data;
    return simple_read_from_buffer(buf, nbytes, ppos, blob, blob->size);
}

struct file_operations blob_fops = {
    .read = blob_read,
    .open = blob_open,
};

struct blob_thing my_blob;

static int __init blob_init(void)
{
    debugfs_create_file("my-blob", S_IFREG | S_IRUGO, NULL,
        &my_blob, &blob_fops);
}
__initcall(blob_init);

Then in userspace:

# mount -t debugfs debugfs /sys/kernel/debug
# dd if=/sys/kernel/debug/my-blob of=somewhere

Posted by mike on Monday March 6th, 2006, tagged with , , ,

Comments are closed.