ca7ef71b7e446e1ff9e7b0aee902a0f7268ceeec
[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_read(u8 *buf, int buf_size);
32 static int vbc_write_byte(unsigned offset, u8 value);
33
34 /* the following defines are copied from
35  * vboot_reference:firmware/lib/vboot_nvstorage.c.
36  */
37 #define RECOVERY_OFFSET              2
38 #define VBNV_RECOVERY_RW_INVALID_OS  0x43
39
40 int chromeos_set_need_recovery(void)
41 {
42         if (!chromeos_legacy_set_need_recovery())
43                 return 0;
44
45         return vbc_write_byte(RECOVERY_OFFSET, VBNV_RECOVERY_RW_INVALID_OS);
46 }
47
48 /*
49  * Lifted from vboot_reference:firmware/lib/vboot_nvstorage.c and formatted.
50  *
51  * Return CRC-8 of the data, using x^8 + x^2 + x + 1 polynomial. A table-based
52  * algorithm would be faster, but for only 15 bytes isn't worth the code size.
53  */
54 static u8 crc8(const u8 *data, int len)
55 {
56         unsigned crc = 0;
57         int i, j;
58
59         for (j = len; j; j--, data++) {
60                 crc ^= (*data << 8);
61                 for (i = 8; i; i--) {
62                         if (crc & 0x8000)
63                                 crc ^= (0x1070 << 3);
64                         crc <<= 1;
65                 }
66         }
67         return (u8)(crc >> 8);
68 }
69
70 static int vbc_write_byte(unsigned offset, u8 value)
71 {
72         u8 buf[MAX_VBOOT_CONTEXT_BUFFER_SIZE];
73         ssize_t size;
74
75         if (!chromeos_vbc_ptr)
76                 return -ENOSYS;
77
78         size = vbc_read(buf, sizeof(buf));
79         if (size <= 0)
80                 return -EINVAL;
81
82         if (offset >= (size - 1))
83                 return -EINVAL;
84
85         if (buf[offset] == value)
86                 return 0;
87
88         buf[offset] = value;
89         buf[size - 1] = crc8(buf, size - 1);
90
91         return chromeos_vbc_ptr->write(buf, size);
92 }
93
94 /*
95  * Read vboot context and verify it.  If everything checks out, return number
96  * of bytes in the vboot context buffer, -1 on any error (uninitialized
97  * subsystem, corrupted crc8 value, not enough room in the buffer, etc.).
98  */
99 static int vbc_read(u8 *buf, int buf_size)
100 {
101         ssize_t size;
102
103         if (!chromeos_vbc_ptr)
104                 return -ENOSYS;
105
106         size = chromeos_vbc_ptr->read(buf, buf_size);
107         if (size <= 0)
108                 return -1;
109
110         if (buf[size - 1] != crc8(buf, size - 1)) {
111                 pr_err("%s: vboot context contents corrupted\n", __func__);
112                 return -1;
113         }
114         return size;
115 }
116
117 int chromeos_vbc_register(struct chromeos_vbc *chromeos_vbc)
118 {
119         chromeos_vbc_ptr = chromeos_vbc;
120         return 0;
121 }
122
123 #ifndef CONFIG_USE_OF
124
125 /* Set the platform dependent parameters using kernel config parameters */
126 static struct ramoops_platform_data chromeos_ramoops_data = {
127         .mem_size               = CONFIG_CHROMEOS_RAMOOPS_RAM_SIZE,
128         .mem_address            = CONFIG_CHROMEOS_RAMOOPS_RAM_START,
129         .record_size            = CONFIG_CHROMEOS_RAMOOPS_RECORD_SIZE,
130         .dump_oops              = CONFIG_CHROMEOS_RAMOOPS_DUMP_OOPS,
131 };
132
133 static struct platform_device chromeos_ramoops = {
134         .name = "ramoops",
135         .dev = {
136                 .platform_data = &chromeos_ramoops_data,
137         },
138 };
139
140 /*
141  * If you don't set the ramoops module parameters then ramoops needs a
142  * platform device in order to run. So a platform device is registered
143  * which sets the needed parameters.
144  */
145 static int __init chromeos_ramoops_init(void)
146 {
147         int ret;
148
149         ret = platform_device_register(&chromeos_ramoops);
150         if (ret) {
151                 printk(KERN_ERR "unable to register platform device\n");
152                 return ret;
153         }
154
155         return 0;
156 }
157
158 subsys_initcall(chromeos_ramoops_init);
159 #endif