routerboot.c 6.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358
  1. /*
  2. * RouterBoot helper routines
  3. *
  4. * Copyright (C) 2012 Gabor Juhos <juhosg@openwrt.org>
  5. *
  6. * This program is free software; you can redistribute it and/or modify it
  7. * under the terms of the GNU General Public License version 2 as published
  8. * by the Free Software Foundation.
  9. */
  10. #define pr_fmt(fmt) "rb: " fmt
  11. #include <linux/kernel.h>
  12. #include <linux/kobject.h>
  13. #include <linux/slab.h>
  14. #include <linux/errno.h>
  15. #include <linux/routerboot.h>
  16. #include <linux/rle.h>
  17. #include <linux/lzo.h>
  18. #include "routerboot.h"
  19. #define RB_BLOCK_SIZE 0x1000
  20. #define RB_ART_SIZE 0x10000
  21. #define RB_MAGIC_ERD 0x00455244 /* extended radio data */
  22. static struct rb_info rb_info;
  23. static u32 get_u32(void *buf)
  24. {
  25. u8 *p = buf;
  26. return ((u32) p[3] + ((u32) p[2] << 8) + ((u32) p[1] << 16) +
  27. ((u32) p[0] << 24));
  28. }
  29. static u16 get_u16(void *buf)
  30. {
  31. u8 *p = buf;
  32. return (u16) p[1] + ((u16) p[0] << 8);
  33. }
  34. __init int
  35. routerboot_find_magic(u8 *buf, unsigned int buflen, u32 *offset, bool hard)
  36. {
  37. u32 magic_ref = hard ? RB_MAGIC_HARD : RB_MAGIC_SOFT;
  38. u32 magic;
  39. u32 cur = *offset;
  40. while (cur < buflen) {
  41. magic = get_u32(buf + cur);
  42. if (magic == magic_ref) {
  43. *offset = cur;
  44. return 0;
  45. }
  46. cur += 0x1000;
  47. }
  48. return -ENOENT;
  49. }
  50. __init int
  51. routerboot_find_tag(u8 *buf, unsigned int buflen, u16 tag_id,
  52. u8 **tag_data, u16 *tag_len)
  53. {
  54. uint32_t magic;
  55. bool align = false;
  56. int ret;
  57. if (buflen < 4)
  58. return -EINVAL;
  59. magic = get_u32(buf);
  60. switch (magic) {
  61. case RB_MAGIC_ERD:
  62. align = true;
  63. /* fall trough */
  64. case RB_MAGIC_HARD:
  65. /* skip magic value */
  66. buf += 4;
  67. buflen -= 4;
  68. break;
  69. case RB_MAGIC_SOFT:
  70. if (buflen < 8)
  71. return -EINVAL;
  72. /* skip magic and CRC value */
  73. buf += 8;
  74. buflen -= 8;
  75. break;
  76. default:
  77. return -EINVAL;
  78. }
  79. ret = -ENOENT;
  80. while (buflen > 2) {
  81. u16 id;
  82. u16 len;
  83. len = get_u16(buf);
  84. buf += 2;
  85. buflen -= 2;
  86. if (buflen < 2)
  87. break;
  88. id = get_u16(buf);
  89. buf += 2;
  90. buflen -= 2;
  91. if (id == RB_ID_TERMINATOR)
  92. break;
  93. if (buflen < len)
  94. break;
  95. if (id == tag_id) {
  96. if (tag_len)
  97. *tag_len = len;
  98. if (tag_data)
  99. *tag_data = buf;
  100. ret = 0;
  101. break;
  102. }
  103. if (align)
  104. len = (len + 3) / 4;
  105. buf += len;
  106. buflen -= len;
  107. }
  108. return ret;
  109. }
  110. static inline int
  111. rb_find_hard_cfg_tag(u16 tag_id, u8 **tag_data, u16 *tag_len)
  112. {
  113. if (!rb_info.hard_cfg_data ||
  114. !rb_info.hard_cfg_size)
  115. return -ENOENT;
  116. return routerboot_find_tag(rb_info.hard_cfg_data,
  117. rb_info.hard_cfg_size,
  118. tag_id, tag_data, tag_len);
  119. }
  120. __init const char *
  121. rb_get_board_name(void)
  122. {
  123. u16 tag_len;
  124. u8 *tag;
  125. int err;
  126. err = rb_find_hard_cfg_tag(RB_ID_BOARD_NAME, &tag, &tag_len);
  127. if (err)
  128. return NULL;
  129. return tag;
  130. }
  131. __init u32
  132. rb_get_hw_options(void)
  133. {
  134. u16 tag_len;
  135. u8 *tag;
  136. int err;
  137. err = rb_find_hard_cfg_tag(RB_ID_HW_OPTIONS, &tag, &tag_len);
  138. if (err)
  139. return 0;
  140. return get_u32(tag);
  141. }
  142. static void * __init
  143. __rb_get_wlan_data(u16 id)
  144. {
  145. u16 tag_len;
  146. u8 *tag;
  147. void *buf;
  148. int err;
  149. u32 magic;
  150. size_t src_done;
  151. size_t dst_done;
  152. err = rb_find_hard_cfg_tag(RB_ID_WLAN_DATA, &tag, &tag_len);
  153. if (err) {
  154. pr_err("no calibration data found\n");
  155. goto err;
  156. }
  157. buf = kmalloc(RB_ART_SIZE, GFP_KERNEL);
  158. if (buf == NULL) {
  159. pr_err("no memory for calibration data\n");
  160. goto err;
  161. }
  162. magic = get_u32(tag);
  163. if (magic == RB_MAGIC_ERD) {
  164. u8 *erd_data;
  165. u16 erd_len;
  166. if (id == 0)
  167. goto err_free;
  168. err = routerboot_find_tag(tag, tag_len, id,
  169. &erd_data, &erd_len);
  170. if (err) {
  171. pr_err("no ERD data found for id %u\n", id);
  172. goto err_free;
  173. }
  174. dst_done = RB_ART_SIZE;
  175. err = lzo1x_decompress_safe(erd_data, erd_len, buf, &dst_done);
  176. if (err) {
  177. pr_err("unable to decompress calibration data %d\n",
  178. err);
  179. goto err_free;
  180. }
  181. } else {
  182. if (id != 0)
  183. goto err_free;
  184. err = rle_decode((char *) tag, tag_len, buf, RB_ART_SIZE,
  185. &src_done, &dst_done);
  186. if (err) {
  187. pr_err("unable to decode calibration data\n");
  188. goto err_free;
  189. }
  190. }
  191. return buf;
  192. err_free:
  193. kfree(buf);
  194. err:
  195. return NULL;
  196. }
  197. __init void *
  198. rb_get_wlan_data(void)
  199. {
  200. return __rb_get_wlan_data(0);
  201. }
  202. __init void *
  203. rb_get_ext_wlan_data(u16 id)
  204. {
  205. return __rb_get_wlan_data(id);
  206. }
  207. __init const struct rb_info *
  208. rb_init_info(void *data, unsigned int size)
  209. {
  210. unsigned int offset;
  211. if (size == 0 || (size % RB_BLOCK_SIZE) != 0)
  212. return NULL;
  213. for (offset = 0; offset < size; offset += RB_BLOCK_SIZE) {
  214. u32 magic;
  215. magic = get_u32(data + offset);
  216. switch (magic) {
  217. case RB_MAGIC_HARD:
  218. rb_info.hard_cfg_offs = offset;
  219. break;
  220. case RB_MAGIC_SOFT:
  221. rb_info.soft_cfg_offs = offset;
  222. break;
  223. }
  224. }
  225. if (!rb_info.hard_cfg_offs) {
  226. pr_err("could not find a valid RouterBOOT hard config\n");
  227. return NULL;
  228. }
  229. if (!rb_info.soft_cfg_offs) {
  230. pr_err("could not find a valid RouterBOOT soft config\n");
  231. return NULL;
  232. }
  233. rb_info.hard_cfg_size = RB_BLOCK_SIZE;
  234. rb_info.hard_cfg_data = kmemdup(data + rb_info.hard_cfg_offs,
  235. RB_BLOCK_SIZE, GFP_KERNEL);
  236. if (!rb_info.hard_cfg_data)
  237. return NULL;
  238. rb_info.board_name = rb_get_board_name();
  239. rb_info.hw_options = rb_get_hw_options();
  240. return &rb_info;
  241. }
  242. static char *rb_ext_wlan_data;
  243. static ssize_t
  244. rb_ext_wlan_data_read(struct file *filp, struct kobject *kobj,
  245. struct bin_attribute *attr, char *buf,
  246. loff_t off, size_t count)
  247. {
  248. if (off + count > attr->size)
  249. return -EFBIG;
  250. memcpy(buf, &rb_ext_wlan_data[off], count);
  251. return count;
  252. }
  253. static const struct bin_attribute rb_ext_wlan_data_attr = {
  254. .attr = {
  255. .name = "ext_wlan_data",
  256. .mode = S_IRUSR | S_IWUSR,
  257. },
  258. .read = rb_ext_wlan_data_read,
  259. .size = RB_ART_SIZE,
  260. };
  261. static int __init rb_sysfs_init(void)
  262. {
  263. struct kobject *rb_kobj;
  264. int ret;
  265. rb_ext_wlan_data = rb_get_ext_wlan_data(1);
  266. if (rb_ext_wlan_data == NULL)
  267. return -ENOENT;
  268. rb_kobj = kobject_create_and_add("routerboot", firmware_kobj);
  269. if (rb_kobj == NULL) {
  270. ret = -ENOMEM;
  271. pr_err("unable to create sysfs entry\n");
  272. goto err_free_wlan_data;
  273. }
  274. ret = sysfs_create_bin_file(rb_kobj, &rb_ext_wlan_data_attr);
  275. if (ret) {
  276. pr_err("unable to create sysfs file, %d\n", ret);
  277. goto err_put_kobj;
  278. }
  279. return 0;
  280. err_put_kobj:
  281. kobject_put(rb_kobj);
  282. err_free_wlan_data:
  283. kfree(rb_ext_wlan_data);
  284. return ret;
  285. }
  286. late_initcall(rb_sysfs_init);