summaryrefslogtreecommitdiffstats
path: root/ipa-client/config.c
blob: 69bd9cb3398bea013928bfabc1810d21abd93524 (plain)
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
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
/* Authors: Rob Crittenden <rcritten@redhat.com>
 *
 * Copyright (C) 2009  Red Hat
 * see file 'COPYING' for use and warranty information
 *
 * This program is free software; you can redistribute it and/or
 * modify it under the terms of the GNU General Public License as
 * published by the Free Software Foundation; version 2 only
 *
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with this program; if not, write to the Free Software
 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
 */

/* Simple and INI-style file reader.
 *
 * usage is:
 * char * data = read_config_file("/path/to/something.conf")
 * char * entry = get_config_entry(data, "section", "mykey")
 *
 * caller must free data and entry.
 */

#define _GNU_SOURCE

#include <sys/types.h>
#include <sys/stat.h>
#include <fcntl.h>
#include <string.h>
#include <unistd.h>
#include <stdio.h>
#include <stdlib.h>
#include <ctype.h>

#include <errno.h>
#include "config.h"
#include <libintl.h>
#define _(STRING) gettext(STRING)

char *
read_config_file(const char *filename)
{
    int fd;
    struct stat st;
    char *data, *dest;
    size_t left;

    fd = open(filename, O_RDONLY);
    if (fd == -1) {
        fprintf(stderr, _("cannot open configuration file %s\n"), filename);
        return NULL;
    }

    /* stat() the file so we know the size and can pre-allocate the right
     * amount of memory. */
    if (fstat(fd, &st) == -1) {
        fprintf(stderr, _("cannot stat() configuration file %s\n"), filename);
        return NULL;
    }
    left = st.st_size;
    data = malloc(st.st_size + 1);
    dest = data;
    while (left != 0) {
        ssize_t res;

        res = read(fd, dest, left);
        if (res == 0)
            break;
        if (res < 0) {
            fprintf(stderr, _("read error\n"));
            close(fd);
            free(dest);
            return NULL;
        }
        dest += res;
        left -= res;
    }
    close(fd);
    *dest = 0;
    return data;
}

char *
get_config_entry(char * in_data, const char *section, const char *key)
{
    char *ptr, *p, *tmp;
    char *line;
    int in_section = 0;
    char * data;

    if (NULL == in_data)
        return NULL;
    else
        data = strdup(in_data);

    for (line = strtok_r(data, "\n", &ptr); line != NULL;
         line = strtok_r(NULL, "\n", &ptr)) {
        /* Skip initial whitespace. */
        while (isspace((unsigned char)*line) && (*line != '\0'))
            line++;

        /* If it's a comment, bail. */
        if (*line == '#') {
            continue;
        }

        /* If it's the beginning of a section, process it and clear the key
         * and value values. */
        if (*line == '[') {
            line++;
            p = strchr(line, ']');
            if (p) {
                tmp = strndup(line, p - line);
                if (in_section) {
                    /* We exited the matching section without a match */
                    free(data);
                    return NULL;
                }
                if (strcmp(section, tmp) == 0) {
                    free(tmp);
                    in_section = 1;
                    continue;
                }
            }
        } /* [ */

        p = strchr(line, '=');
        if (p != NULL && in_section) {
            /* Trim any trailing whitespace off the key name. */
            while (p != line && isspace((unsigned char)p[-1]))
                p--;

            /* Save the key. */
            tmp = strndup(line, p - line);
            if (strcmp(key, tmp) != 0) {
                free(tmp);
            } else {
                free(tmp);

                /* Skip over any whitespace after the equal sign. */
                line = strchr(line, '=');
                line++;
                while (isspace((unsigned char)*line) && (*line != '\0'))
                    line++;

                /* Trim off any trailing whitespace. */
                p = strchr(line, '\0');
                while (p != line && isspace((unsigned char)p[-1]))
                    p--;

                /* Save the value. */
                tmp = strndup(line, p - line);

                free(data);
                return tmp;
            }
        }
    }
    free(data);
    return NULL;
}

int init_gettext(void)
{
    char *c;

    c = setlocale(LC_ALL, "");
    if (!c) {
        return EIO;
    }

    errno = 0;
    c = bindtextdomain(PACKAGE, LOCALEDIR);
    if (c == NULL) {
        return errno;
    }

    errno = 0;
    c = textdomain(PACKAGE);
    if (c == NULL) {
        return errno;
    }

    return 0;
}
">#include <linux/init.h> #include <linux/module.h> #include <linux/string.h> #include <linux/errno.h> #include <linux/blkdev.h> #include <linux/device.h> #include <linux/spinlock.h> #include <linux/moduleparam.h> #include <linux/firmware.h> #include <linux/dma-mapping.h> MODULE_AUTHOR("Abhay Salunke <abhay_salunke@dell.com>"); MODULE_DESCRIPTION("Driver for updating BIOS image on DELL systems"); MODULE_LICENSE("GPL"); MODULE_VERSION("3.0"); #define BIOS_SCAN_LIMIT 0xffffffff #define MAX_IMAGE_LENGTH 16 static struct _rbu_data { void *image_update_buffer; unsigned long image_update_buffer_size; unsigned long bios_image_size; int image_update_ordernum; int dma_alloc; spinlock_t lock; unsigned long packet_read_count; unsigned long num_packets; unsigned long packetsize; unsigned long imagesize; int entry_created; } rbu_data; static char image_type[MAX_IMAGE_LENGTH + 1] = "mono"; module_param_string(image_type, image_type, sizeof (image_type), 0); MODULE_PARM_DESC(image_type, "BIOS image type. choose- mono or packet or init"); struct packet_data { struct list_head list; size_t length; void *data; int ordernum; }; static struct packet_data packet_data_head; static struct platform_device *rbu_device; static int context; static dma_addr_t dell_rbu_dmaaddr; static void init_packet_head(void) { INIT_LIST_HEAD(&packet_data_head.list); rbu_data.packet_read_count = 0; rbu_data.num_packets = 0; rbu_data.packetsize = 0; rbu_data.imagesize = 0; } static int create_packet(void *data, size_t length) { struct packet_data *newpacket; int ordernum = 0; pr_debug("create_packet: entry \n"); if (!rbu_data.packetsize) { pr_debug("create_packet: packetsize not specified\n"); return -EINVAL; } spin_unlock(&rbu_data.lock); newpacket = kmalloc(sizeof (struct packet_data), GFP_KERNEL); spin_lock(&rbu_data.lock); if (!newpacket) { printk(KERN_WARNING "dell_rbu:%s: failed to allocate new " "packet\n", __FUNCTION__); return -ENOMEM; } ordernum = get_order(length); /* * there is no upper limit on memory * address for packetized mechanism */ spin_unlock(&rbu_data.lock); newpacket->data = (unsigned char *) __get_free_pages(GFP_KERNEL, ordernum); spin_lock(&rbu_data.lock); pr_debug("create_packet: newpacket %p\n", newpacket->data); if (!newpacket->data) { printk(KERN_WARNING "dell_rbu:%s: failed to allocate new " "packet\n", __FUNCTION__); kfree(newpacket); return -ENOMEM; } newpacket->ordernum = ordernum; ++rbu_data.num_packets; /* * initialize the newly created packet headers */ INIT_LIST_HEAD(&newpacket->list); list_add_tail(&newpacket->list, &packet_data_head.list); /* * packets may not have fixed size */ newpacket->length = length; memcpy(newpacket->data, data, length); pr_debug("create_packet: exit \n"); return 0; } static int packetize_data(void *data, size_t length) { int rc = 0; int done = 0; int packet_length; u8 *temp; u8 *end = (u8 *) data + length; pr_debug("packetize_data: data length %d\n", length); if (!rbu_data.packetsize) { printk(KERN_WARNING "dell_rbu: packetsize not specified\n"); return -EIO; } temp = (u8 *) data; /* packetize the hunk */ while (!done) { if ((temp + rbu_data.packetsize) < end) packet_length = rbu_data.packetsize; else { /* this is the last packet */ packet_length = end - temp; done = 1; } if ((rc = create_packet(temp, packet_length))) return rc; pr_debug("%lu:%lu\n", temp, (end - temp)); temp += packet_length; } rbu_data.imagesize = length; return rc; } static int do_packet_read(char *data, struct list_head *ptemp_list, int length, int bytes_read, int *list_read_count) { void *ptemp_buf; struct packet_data *newpacket = NULL; int bytes_copied = 0; int j = 0; newpacket = list_entry(ptemp_list, struct packet_data, list); *list_read_count += newpacket->length; if (*list_read_count > bytes_read) { /* point to the start of unread data */ j = newpacket->length - (*list_read_count - bytes_read); /* point to the offset in the packet buffer */ ptemp_buf = (u8 *) newpacket->data + j; /* * check if there is enough room in * * the incoming buffer */ if (length > (*list_read_count - bytes_read)) /* * copy what ever is there in this * packet and move on */ bytes_copied = (*list_read_count - bytes_read); else /* copy the remaining */ bytes_copied = length; memcpy(data, ptemp_buf, bytes_copied); } return bytes_copied; } static int packet_read_list(char *data, size_t * pread_length) { struct list_head *ptemp_list; int temp_count = 0; int bytes_copied = 0; int bytes_read = 0; int remaining_bytes = 0; char *pdest = data; /* check if we have any packets */ if (0 == rbu_data.num_packets) return -ENOMEM; remaining_bytes = *pread_length; bytes_read = rbu_data.packet_read_count; ptemp_list = (&packet_data_head.list)->next; while (!list_empty(ptemp_list)) { bytes_copied = do_packet_read(pdest, ptemp_list, remaining_bytes, bytes_read, &temp_count); remaining_bytes -= bytes_copied; bytes_read += bytes_copied; pdest += bytes_copied; /* * check if we reached end of buffer before reaching the * last packet */ if (remaining_bytes == 0) break; ptemp_list = ptemp_list->next; } /*finally set the bytes read */ *pread_length = bytes_read - rbu_data.packet_read_count; rbu_data.packet_read_count = bytes_read; return 0; } static void packet_empty_list(void) { struct list_head *ptemp_list; struct list_head *pnext_list; struct packet_data *newpacket; ptemp_list = (&packet_data_head.list)->next; while (!list_empty(ptemp_list)) { newpacket = list_entry(ptemp_list, struct packet_data, list); pnext_list = ptemp_list->next; list_del(ptemp_list); ptemp_list = pnext_list; /* * zero out the RBU packet memory before freeing * to make sure there are no stale RBU packets left in memory */ memset(newpacket->data, 0, rbu_data.packetsize); free_pages((unsigned long) newpacket->data, newpacket->ordernum); kfree(newpacket); } rbu_data.packet_read_count = 0; rbu_data.num_packets = 0; rbu_data.imagesize = 0; } /* * img_update_free: Frees the buffer allocated for storing BIOS image * Always called with lock held and returned with lock held */ static void img_update_free(void) { if (!rbu_data.image_update_buffer) return; /* * zero out this buffer before freeing it to get rid of any stale * BIOS image copied in memory. */ memset(rbu_data.image_update_buffer, 0, rbu_data.image_update_buffer_size); if (rbu_data.dma_alloc == 1) dma_free_coherent(NULL, rbu_data.bios_image_size, rbu_data.image_update_buffer, dell_rbu_dmaaddr); else free_pages((unsigned long) rbu_data.image_update_buffer, rbu_data.image_update_ordernum); /* * Re-initialize the rbu_data variables after a free */ rbu_data.image_update_ordernum = -1; rbu_data.image_update_buffer = NULL; rbu_data.image_update_buffer_size = 0; rbu_data.bios_image_size = 0; rbu_data.dma_alloc = 0; } /* * img_update_realloc: This function allocates the contiguous pages to * accommodate the requested size of data. The memory address and size * values are stored globally and on every call to this function the new * size is checked to see if more data is required than the existing size. * If true the previous memory is freed and new allocation is done to * accommodate the new size. If the incoming size is less then than the * already allocated size, then that memory is reused. This function is * called with lock held and returns with lock held. */ static int img_update_realloc(unsigned long size) { unsigned char *image_update_buffer = NULL; unsigned long rc; unsigned long img_buf_phys_addr; int ordernum; int dma_alloc = 0; /* * check if the buffer of sufficient size has been * already allocated */ if (rbu_data.image_update_buffer_size >= size) { /* * check for corruption */ if ((size != 0) && (rbu_data.image_update_buffer == NULL)) { printk(KERN_ERR "dell_rbu:%s: corruption " "check failed\n", __FUNCTION__); return -EINVAL; } /* * we have a valid pre-allocated buffer with * sufficient size */ return 0; } /* * free any previously allocated buffer */ img_update_free(); spin_unlock(&rbu_data.lock); ordernum = get_order(size); image_update_buffer = (unsigned char *) __get_free_pages(GFP_KERNEL, ordernum); img_buf_phys_addr = (unsigned long) virt_to_phys(image_update_buffer); if (img_buf_phys_addr > BIOS_SCAN_LIMIT) { free_pages((unsigned long) image_update_buffer, ordernum); ordernum = -1; image_update_buffer = dma_alloc_coherent(NULL, size, &dell_rbu_dmaaddr, GFP_KERNEL); dma_alloc = 1; } spin_lock(&rbu_data.lock); if (image_update_buffer != NULL) { rbu_data.image_update_buffer = image_update_buffer; rbu_data.image_update_buffer_size = size; rbu_data.bios_image_size = rbu_data.image_update_buffer_size; rbu_data.image_update_ordernum = ordernum; rbu_data.dma_alloc = dma_alloc; rc = 0; } else { pr_debug("Not enough memory for image update:" "size = %ld\n", size); rc = -ENOMEM; } return rc; } static ssize_t read_packet_data(char *buffer, loff_t pos, size_t count) { int retval; size_t bytes_left; size_t data_length; char *ptempBuf = buffer; /* check to see if we have something to return */ if (rbu_data.num_packets == 0) { pr_debug("read_packet_data: no packets written\n"); retval = -ENOMEM; goto read_rbu_data_exit; } if (pos > rbu_data.imagesize) { retval = 0; printk(KERN_WARNING "dell_rbu:read_packet_data: " "data underrun\n"); goto read_rbu_data_exit; } bytes_left = rbu_data.imagesize - pos; data_length = min(bytes_left, count); if ((retval = packet_read_list(ptempBuf, &data_length)) < 0) goto read_rbu_data_exit; if ((pos + count) > rbu_data.imagesize) { rbu_data.packet_read_count = 0; /* this was the last copy */ retval = bytes_left; } else retval = count; read_rbu_data_exit: return retval; } static ssize_t read_rbu_mono_data(char *buffer, loff_t pos, size_t count) { unsigned char *ptemp = NULL; size_t bytes_left = 0; size_t data_length = 0; ssize_t ret_count = 0; /* check to see if we have something to return */ if ((rbu_data.image_update_buffer == NULL) || (rbu_data.bios_image_size == 0)) { pr_debug("read_rbu_data_mono: image_update_buffer %p ," "bios_image_size %lu\n", rbu_data.image_update_buffer, rbu_data.bios_image_size); ret_count = -ENOMEM; goto read_rbu_data_exit; } if (pos > rbu_data.bios_image_size) { ret_count = 0; goto read_rbu_data_exit; } bytes_left = rbu_data.bios_image_size - pos; data_length = min(bytes_left, count); ptemp = rbu_data.image_update_buffer; memcpy(buffer, (ptemp + pos), data_length); if ((pos + count) > rbu_data.bios_image_size) /* this was the last copy */ ret_count = bytes_left; else ret_count = count; read_rbu_data_exit: return ret_count; } static ssize_t read_rbu_data(struct kobject *kobj, char *buffer, loff_t pos, size_t count) { ssize_t ret_count = 0; spin_lock(&rbu_data.lock); if (!strcmp(image_type, "mono")) ret_count = read_rbu_mono_data(buffer, pos, count); else if (!strcmp(image_type, "packet")) ret_count = read_packet_data(buffer, pos, count); else pr_debug("read_rbu_data: invalid image type specified\n"); spin_unlock(&rbu_data.lock); return ret_count; } static void callbackfn_rbu(const struct firmware *fw, void *context) { int rc = 0; if (!fw || !fw->size) { rbu_data.entry_created = 0; return; } spin_lock(&rbu_data.lock); if (!strcmp(image_type, "mono")) { if (!img_update_realloc(fw->size)) memcpy(rbu_data.image_update_buffer, fw->data, fw->size); } else if (!strcmp(image_type, "packet")) { /* * we need to free previous packets if a * new hunk of packets needs to be downloaded */ packet_empty_list(); if (packetize_data(fw->data, fw->size)) /* Incase something goes wrong when we are * in middle of packetizing the data, we * need to free up whatever packets might * have been created before we quit. */ packet_empty_list(); } else pr_debug("invalid image type specified.\n"); spin_unlock(&rbu_data.lock); rc = request_firmware_nowait(THIS_MODULE, FW_ACTION_NOHOTPLUG, "dell_rbu", &rbu_device->dev, &context, callbackfn_rbu); if (rc) printk(KERN_ERR "dell_rbu:%s request_firmware_nowait failed" " %d\n", __FUNCTION__, rc); else rbu_data.entry_created = 1; } static ssize_t read_rbu_image_type(struct kobject *kobj, char *buffer, loff_t pos, size_t count) { int size = 0; if (!pos) size = sprintf(buffer, "%s\n", image_type); return size; } static ssize_t write_rbu_image_type(struct kobject *kobj, char *buffer, loff_t pos, size_t count) { int rc = count; int req_firm_rc = 0; int i; spin_lock(&rbu_data.lock); /* * Find the first newline or space */ for (i = 0; i < count; ++i) if (buffer[i] == '\n' || buffer[i] == ' ') { buffer[i] = '\0'; break; } if (i == count) buffer[count] = '\0'; if (strstr(buffer, "mono")) strcpy(image_type, "mono"); else if (strstr(buffer, "packet")) strcpy(image_type, "packet"); else if (strstr(buffer, "init")) { /* * If due to the user error the driver gets in a bad * state where even though it is loaded , the * /sys/class/firmware/dell_rbu entries are missing. * to cover this situation the user can recreate entries * by writing init to image_type. */ if (!rbu_data.entry_created) { spin_unlock(&rbu_data.lock); req_firm_rc = request_firmware_nowait(THIS_MODULE, FW_ACTION_NOHOTPLUG, "dell_rbu", &rbu_device->dev, &context, callbackfn_rbu); if (req_firm_rc) { printk(KERN_ERR "dell_rbu:%s request_firmware_nowait" " failed %d\n", __FUNCTION__, rc); rc = -EIO; } else rbu_data.entry_created = 1; spin_lock(&rbu_data.lock); } } else { printk(KERN_WARNING "dell_rbu: image_type is invalid\n"); spin_unlock(&rbu_data.lock); return -EINVAL; } /* we must free all previous allocations */ packet_empty_list(); img_update_free(); spin_unlock(&rbu_data.lock); return rc; } static ssize_t read_rbu_packet_size(struct kobject *kobj, char *buffer, loff_t pos, size_t count) { int size = 0; if (!pos) { spin_lock(&rbu_data.lock); size = sprintf(buffer, "%lu\n", rbu_data.packetsize); spin_unlock(&rbu_data.lock); } return size; } static ssize_t write_rbu_packet_size(struct kobject *kobj, char *buffer, loff_t pos, size_t count) { unsigned long temp; spin_lock(&rbu_data.lock); packet_empty_list(); sscanf(buffer, "%lu", &temp); if (temp < 0xffffffff) rbu_data.packetsize = temp; spin_unlock(&rbu_data.lock); return count; } static struct bin_attribute rbu_data_attr = { .attr = {.name = "data",.owner = THIS_MODULE,.mode = 0444}, .read = read_rbu_data, }; static struct bin_attribute rbu_image_type_attr = { .attr = {.name = "image_type",.owner = THIS_MODULE,.mode = 0644}, .read = read_rbu_image_type, .write = write_rbu_image_type, }; static struct bin_attribute rbu_packet_size_attr = { .attr = {.name = "packet_size",.owner = THIS_MODULE,.mode = 0644}, .read = read_rbu_packet_size, .write = write_rbu_packet_size, }; static int __init dcdrbu_init(void) { int rc = 0; spin_lock_init(&rbu_data.lock); init_packet_head(); rbu_device = platform_device_register_simple("dell_rbu", -1, NULL, 0); if (!rbu_device) { printk(KERN_ERR "dell_rbu:%s:platform_device_register_simple " "failed\n", __FUNCTION__); return -EIO; } sysfs_create_bin_file(&rbu_device->dev.kobj, &rbu_data_attr); sysfs_create_bin_file(&rbu_device->dev.kobj, &rbu_image_type_attr); sysfs_create_bin_file(&rbu_device->dev.kobj, &rbu_packet_size_attr); rc = request_firmware_nowait(THIS_MODULE, FW_ACTION_NOHOTPLUG, "dell_rbu", &rbu_device->dev, &context, callbackfn_rbu); if (rc) printk(KERN_ERR "dell_rbu:%s:request_firmware_nowait" " failed %d\n", __FUNCTION__, rc); else rbu_data.entry_created = 1; return rc; } static __exit void dcdrbu_exit(void) { spin_lock(&rbu_data.lock); packet_empty_list(); img_update_free(); spin_unlock(&rbu_data.lock); platform_device_unregister(rbu_device); } module_exit(dcdrbu_exit); module_init(dcdrbu_init);