read/write chromeos vbc with sysfs
[cascardo/linux.git] / drivers / platform / chromeos.c
1 /*
2  *  ChromeOS platform support code. Glue layer between higher level functions
3  *  and per-platform firmware interfaces.
4  *
5  *  Copyright (C) 2010 The Chromium OS Authors
6  *
7  *  This program is free software; you can redistribute it and/or modify
8  *  it under the terms of the GNU General Public License as published by
9  *  the Free Software Foundation; either version 2 of the License, or
10  *  (at your option) any later version.
11  *
12  *  This program is distributed in the hope that it will be useful,
13  *  but WITHOUT ANY WARRANTY; without even the implied warranty of
14  *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
15  *  GNU General Public License for more details.
16  *
17  *  You should have received a copy of the GNU General Public License
18  *  along with this program; if not, write to the Free Software
19  *  Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
20  */
21
22 #include <linux/types.h>
23 #include <linux/chromeos_platform.h>
24 #include <linux/module.h>
25 #include <linux/platform_device.h>
26 #include <linux/ramoops.h>
27 #include "chromeos.h"
28
29 static struct chromeos_vbc *chromeos_vbc_ptr;
30
31 static int vbc_write_byte(unsigned offset, u8 value);
32
33 /* the following defines are copied from
34  * vboot_reference:firmware/lib/vboot_nvstorage.c.
35  */
36 #define RECOVERY_OFFSET              2
37 #define VBNV_RECOVERY_RW_INVALID_OS  0x43
38
39 int chromeos_set_need_recovery(void)
40 {
41         if (!chromeos_legacy_set_need_recovery())
42                 return 0;
43
44         return vbc_write_byte(RECOVERY_OFFSET, VBNV_RECOVERY_RW_INVALID_OS);
45 }
46
47 int chromeos_set_dev_usb(void)
48 {
49         return vbc_write_byte(4, 0x01);
50 }
51
52 /*
53  * Lifted from vboot_reference:firmware/lib/vboot_nvstorage.c and formatted.
54  *
55  * Return CRC-8 of the data, using x^8 + x^2 + x + 1 polynomial. A table-based
56  * algorithm would be faster, but for only 15 bytes isn't worth the code size.
57  */
58 static u8 crc8(const u8 *data, int len)
59 {
60         unsigned crc = 0;
61         int i, j;
62
63         for (j = len; j; j--, data++) {
64                 crc ^= (*data << 8);
65                 for (i = 8; i; i--) {
66                         if (crc & 0x8000)
67                                 crc ^= (0x1070 << 3);
68                         crc <<= 1;
69                 }
70         }
71         return (u8)(crc >> 8);
72 }
73
74 int chromeos_vbc_write(const u8 *buf, ssize_t size)
75 {
76         return chromeos_vbc_ptr->write(buf, size);
77 }
78
79 static int vbc_write_byte(unsigned offset, u8 value)
80 {
81         u8 buf[MAX_VBOOT_CONTEXT_BUFFER_SIZE];
82         ssize_t size;
83
84         if (!chromeos_vbc_ptr)
85                 return -ENOSYS;
86
87         size = chromeos_vbc_read(buf, sizeof(buf));
88         if (size <= 0)
89                 return -EINVAL;
90
91         if (offset >= (size - 1))
92                 return -EINVAL;
93
94         if (buf[offset] == value)
95                 return 0;
96
97         buf[offset] = value;
98         buf[size - 1] = crc8(buf, size - 1);
99
100         return chromeos_vbc_ptr->write(buf, size);
101 }
102
103 /*
104  * Read vboot context and verify it.  If everything checks out, return number
105  * of bytes in the vboot context buffer, -1 on any error (uninitialized
106  * subsystem, corrupted crc8 value, not enough room in the buffer, etc.).
107  */
108 int chromeos_vbc_read(u8 *buf, int buf_size)
109 {
110         ssize_t size;
111
112         if (!chromeos_vbc_ptr)
113                 return -ENOSYS;
114
115         size = chromeos_vbc_ptr->read(buf, buf_size);
116         if (size <= 0)
117                 return -1;
118
119         if (buf[size - 1] != crc8(buf, size - 1)) {
120                 pr_err("%s: vboot context contents corrupted\n", __func__);
121                 return -1;
122         }
123         return size;
124 }
125
126 int chromeos_vbc_register(struct chromeos_vbc *chromeos_vbc)
127 {
128         chromeos_vbc_ptr = chromeos_vbc;
129         return 0;
130 }
131
132 #ifndef CONFIG_USE_OF
133
134 /* Set the platform dependent parameters using kernel config parameters */
135 static struct ramoops_platform_data chromeos_ramoops_data = {
136         .mem_size               = CONFIG_CHROMEOS_RAMOOPS_RAM_SIZE,
137         .mem_address            = CONFIG_CHROMEOS_RAMOOPS_RAM_START,
138         .record_size            = CONFIG_CHROMEOS_RAMOOPS_RECORD_SIZE,
139         .dump_oops              = CONFIG_CHROMEOS_RAMOOPS_DUMP_OOPS,
140 };
141
142 static struct platform_device chromeos_ramoops = {
143         .name = "ramoops",
144         .dev = {
145                 .platform_data = &chromeos_ramoops_data,
146         },
147 };
148
149 /*
150  * If you don't set the ramoops module parameters then ramoops needs a
151  * platform device in order to run. So a platform device is registered
152  * which sets the needed parameters.
153  */
154 static int __init chromeos_ramoops_init(void)
155 {
156         int ret;
157
158         ret = platform_device_register(&chromeos_ramoops);
159         if (ret) {
160                 printk(KERN_ERR "unable to register platform device\n");
161                 return ret;
162         }
163
164         return 0;
165 }
166
167 subsys_initcall(chromeos_ramoops_init);
168 #endif