nvram.c 22 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922
  1. /*
  2. * CMOS/NV-RAM driver for Linux
  3. *
  4. * Copyright (C) 1997 Roman Hodek <Roman.Hodek@informatik.uni-erlangen.de>
  5. * idea by and with help from Richard Jelinek <rj@suse.de>
  6. * Portions copyright (c) 2001,2002 Sun Microsystems (thockin@sun.com)
  7. *
  8. * This driver allows you to access the contents of the non-volatile memory in
  9. * the mc146818rtc.h real-time clock. This chip is built into all PCs and into
  10. * many Atari machines. In the former it's called "CMOS-RAM", in the latter
  11. * "NVRAM" (NV stands for non-volatile).
  12. *
  13. * The data are supplied as a (seekable) character device, /dev/nvram. The
  14. * size of this file is dependent on the controller. The usual size is 114,
  15. * the number of freely available bytes in the memory (i.e., not used by the
  16. * RTC itself).
  17. *
  18. * Checksums over the NVRAM contents are managed by this driver. In case of a
  19. * bad checksum, reads and writes return -EIO. The checksum can be initialized
  20. * to a sane state either by ioctl(NVRAM_INIT) (clear whole NVRAM) or
  21. * ioctl(NVRAM_SETCKS) (doesn't change contents, just makes checksum valid
  22. * again; use with care!)
  23. *
  24. * This file also provides some functions for other parts of the kernel that
  25. * want to access the NVRAM: nvram_{read,write,check_checksum,set_checksum}.
  26. * Obviously this can be used only if this driver is always configured into
  27. * the kernel and is not a module. Since the functions are used by some Atari
  28. * drivers, this is the case on the Atari.
  29. *
  30. *
  31. * 1.1 Cesar Barros: SMP locking fixes
  32. * added changelog
  33. * 1.2 Erik Gilling: Cobalt Networks support
  34. * Tim Hockin: general cleanup, Cobalt support
  35. */
  36. #define NVRAM_VERSION "1.2"
  37. #include <linux/module.h>
  38. #include <linux/smp_lock.h>
  39. #include <linux/nvram.h>
  40. #define PC 1
  41. #define ATARI 2
  42. #define COBALT 3
  43. /* select machine configuration */
  44. #if defined(CONFIG_ATARI)
  45. # define MACH ATARI
  46. #elif defined(__i386__) || defined(__x86_64__) || defined(__arm__) /* and others?? */
  47. #define MACH PC
  48. # if defined(CONFIG_COBALT)
  49. # include <linux/cobalt-nvram.h>
  50. # define MACH COBALT
  51. # else
  52. # define MACH PC
  53. # endif
  54. #else
  55. # error Cannot build nvram driver for this machine configuration.
  56. #endif
  57. #if MACH == PC
  58. /* RTC in a PC */
  59. #define CHECK_DRIVER_INIT() 1
  60. /* On PCs, the checksum is built only over bytes 2..31 */
  61. #define PC_CKS_RANGE_START 2
  62. #define PC_CKS_RANGE_END 31
  63. #define PC_CKS_LOC 32
  64. #define NVRAM_BYTES (128-NVRAM_FIRST_BYTE)
  65. #define mach_check_checksum pc_check_checksum
  66. #define mach_set_checksum pc_set_checksum
  67. #define mach_proc_infos pc_proc_infos
  68. #endif
  69. #if MACH == COBALT
  70. #define CHECK_DRIVER_INIT() 1
  71. #define NVRAM_BYTES (128-NVRAM_FIRST_BYTE)
  72. #define mach_check_checksum cobalt_check_checksum
  73. #define mach_set_checksum cobalt_set_checksum
  74. #define mach_proc_infos cobalt_proc_infos
  75. #endif
  76. #if MACH == ATARI
  77. /* Special parameters for RTC in Atari machines */
  78. #include <asm/atarihw.h>
  79. #include <asm/atariints.h>
  80. #define RTC_PORT(x) (TT_RTC_BAS + 2*(x))
  81. #define CHECK_DRIVER_INIT() (MACH_IS_ATARI && ATARIHW_PRESENT(TT_CLK))
  82. #define NVRAM_BYTES 50
  83. /* On Ataris, the checksum is over all bytes except the checksum bytes
  84. * themselves; these are at the very end */
  85. #define ATARI_CKS_RANGE_START 0
  86. #define ATARI_CKS_RANGE_END 47
  87. #define ATARI_CKS_LOC 48
  88. #define mach_check_checksum atari_check_checksum
  89. #define mach_set_checksum atari_set_checksum
  90. #define mach_proc_infos atari_proc_infos
  91. #endif
  92. /* Note that *all* calls to CMOS_READ and CMOS_WRITE must be done with
  93. * rtc_lock held. Due to the index-port/data-port design of the RTC, we
  94. * don't want two different things trying to get to it at once. (e.g. the
  95. * periodic 11 min sync from time.c vs. this driver.)
  96. */
  97. #include <linux/types.h>
  98. #include <linux/errno.h>
  99. #include <linux/miscdevice.h>
  100. #include <linux/slab.h>
  101. #include <linux/ioport.h>
  102. #include <linux/fcntl.h>
  103. #include <linux/mc146818rtc.h>
  104. #include <linux/init.h>
  105. #include <linux/proc_fs.h>
  106. #include <linux/spinlock.h>
  107. #include <asm/io.h>
  108. #include <asm/uaccess.h>
  109. #include <asm/system.h>
  110. static DEFINE_SPINLOCK(nvram_state_lock);
  111. static int nvram_open_cnt; /* #times opened */
  112. static int nvram_open_mode; /* special open modes */
  113. #define NVRAM_WRITE 1 /* opened for writing (exclusive) */
  114. #define NVRAM_EXCL 2 /* opened with O_EXCL */
  115. static int mach_check_checksum(void);
  116. static void mach_set_checksum(void);
  117. #ifdef CONFIG_PROC_FS
  118. static int mach_proc_infos(unsigned char *contents, char *buffer, int *len,
  119. off_t *begin, off_t offset, int size);
  120. #endif
  121. /*
  122. * These functions are provided to be called internally or by other parts of
  123. * the kernel. It's up to the caller to ensure correct checksum before reading
  124. * or after writing (needs to be done only once).
  125. *
  126. * It is worth noting that these functions all access bytes of general
  127. * purpose memory in the NVRAM - that is to say, they all add the
  128. * NVRAM_FIRST_BYTE offset. Pass them offsets into NVRAM as if you did not
  129. * know about the RTC cruft.
  130. */
  131. unsigned char
  132. __nvram_read_byte(int i)
  133. {
  134. return CMOS_READ(NVRAM_FIRST_BYTE + i);
  135. }
  136. unsigned char
  137. nvram_read_byte(int i)
  138. {
  139. unsigned long flags;
  140. unsigned char c;
  141. spin_lock_irqsave(&rtc_lock, flags);
  142. c = __nvram_read_byte(i);
  143. spin_unlock_irqrestore(&rtc_lock, flags);
  144. return c;
  145. }
  146. /* This races nicely with trying to read with checksum checking (nvram_read) */
  147. void
  148. __nvram_write_byte(unsigned char c, int i)
  149. {
  150. CMOS_WRITE(c, NVRAM_FIRST_BYTE + i);
  151. }
  152. void
  153. nvram_write_byte(unsigned char c, int i)
  154. {
  155. unsigned long flags;
  156. spin_lock_irqsave(&rtc_lock, flags);
  157. __nvram_write_byte(c, i);
  158. spin_unlock_irqrestore(&rtc_lock, flags);
  159. }
  160. int
  161. __nvram_check_checksum(void)
  162. {
  163. return mach_check_checksum();
  164. }
  165. int
  166. nvram_check_checksum(void)
  167. {
  168. unsigned long flags;
  169. int rv;
  170. spin_lock_irqsave(&rtc_lock, flags);
  171. rv = __nvram_check_checksum();
  172. spin_unlock_irqrestore(&rtc_lock, flags);
  173. return rv;
  174. }
  175. static void
  176. __nvram_set_checksum(void)
  177. {
  178. mach_set_checksum();
  179. }
  180. #if 0
  181. void
  182. nvram_set_checksum(void)
  183. {
  184. unsigned long flags;
  185. spin_lock_irqsave(&rtc_lock, flags);
  186. __nvram_set_checksum();
  187. spin_unlock_irqrestore(&rtc_lock, flags);
  188. }
  189. #endif /* 0 */
  190. /*
  191. * The are the file operation function for user access to /dev/nvram
  192. */
  193. static loff_t nvram_llseek(struct file *file,loff_t offset, int origin )
  194. {
  195. lock_kernel();
  196. switch (origin) {
  197. case 0:
  198. /* nothing to do */
  199. break;
  200. case 1:
  201. offset += file->f_pos;
  202. break;
  203. case 2:
  204. offset += NVRAM_BYTES;
  205. break;
  206. }
  207. unlock_kernel();
  208. return (offset >= 0) ? (file->f_pos = offset) : -EINVAL;
  209. }
  210. static ssize_t
  211. nvram_read(struct file *file, char __user *buf, size_t count, loff_t *ppos)
  212. {
  213. unsigned char contents[NVRAM_BYTES];
  214. unsigned i = *ppos;
  215. unsigned char *tmp;
  216. spin_lock_irq(&rtc_lock);
  217. if (!__nvram_check_checksum())
  218. goto checksum_err;
  219. for (tmp = contents; count-- > 0 && i < NVRAM_BYTES; ++i, ++tmp)
  220. *tmp = __nvram_read_byte(i);
  221. spin_unlock_irq(&rtc_lock);
  222. if (copy_to_user(buf, contents, tmp - contents))
  223. return -EFAULT;
  224. *ppos = i;
  225. return tmp - contents;
  226. checksum_err:
  227. spin_unlock_irq(&rtc_lock);
  228. return -EIO;
  229. }
  230. static ssize_t
  231. nvram_write(struct file *file, const char __user *buf, size_t count, loff_t *ppos)
  232. {
  233. unsigned char contents[NVRAM_BYTES];
  234. unsigned i = *ppos;
  235. unsigned char *tmp;
  236. int len;
  237. len = (NVRAM_BYTES - i) < count ? (NVRAM_BYTES - i) : count;
  238. if (copy_from_user(contents, buf, len))
  239. return -EFAULT;
  240. spin_lock_irq(&rtc_lock);
  241. if (!__nvram_check_checksum())
  242. goto checksum_err;
  243. for (tmp = contents; count-- > 0 && i < NVRAM_BYTES; ++i, ++tmp)
  244. __nvram_write_byte(*tmp, i);
  245. __nvram_set_checksum();
  246. spin_unlock_irq(&rtc_lock);
  247. *ppos = i;
  248. return tmp - contents;
  249. checksum_err:
  250. spin_unlock_irq(&rtc_lock);
  251. return -EIO;
  252. }
  253. static int
  254. nvram_ioctl(struct inode *inode, struct file *file,
  255. unsigned int cmd, unsigned long arg)
  256. {
  257. int i;
  258. switch (cmd) {
  259. case NVRAM_INIT:
  260. /* initialize NVRAM contents and checksum */
  261. if (!capable(CAP_SYS_ADMIN))
  262. return -EACCES;
  263. spin_lock_irq(&rtc_lock);
  264. for (i = 0; i < NVRAM_BYTES; ++i)
  265. __nvram_write_byte(0, i);
  266. __nvram_set_checksum();
  267. spin_unlock_irq(&rtc_lock);
  268. return 0;
  269. case NVRAM_SETCKS:
  270. /* just set checksum, contents unchanged (maybe useful after
  271. * checksum garbaged somehow...) */
  272. if (!capable(CAP_SYS_ADMIN))
  273. return -EACCES;
  274. spin_lock_irq(&rtc_lock);
  275. __nvram_set_checksum();
  276. spin_unlock_irq(&rtc_lock);
  277. return 0;
  278. default:
  279. return -ENOTTY;
  280. }
  281. }
  282. static int
  283. nvram_open(struct inode *inode, struct file *file)
  284. {
  285. spin_lock(&nvram_state_lock);
  286. if ((nvram_open_cnt && (file->f_flags & O_EXCL)) ||
  287. (nvram_open_mode & NVRAM_EXCL) ||
  288. ((file->f_mode & 2) && (nvram_open_mode & NVRAM_WRITE))) {
  289. spin_unlock(&nvram_state_lock);
  290. return -EBUSY;
  291. }
  292. if (file->f_flags & O_EXCL)
  293. nvram_open_mode |= NVRAM_EXCL;
  294. if (file->f_mode & 2)
  295. nvram_open_mode |= NVRAM_WRITE;
  296. nvram_open_cnt++;
  297. spin_unlock(&nvram_state_lock);
  298. return 0;
  299. }
  300. static int
  301. nvram_release(struct inode *inode, struct file *file)
  302. {
  303. spin_lock(&nvram_state_lock);
  304. nvram_open_cnt--;
  305. /* if only one instance is open, clear the EXCL bit */
  306. if (nvram_open_mode & NVRAM_EXCL)
  307. nvram_open_mode &= ~NVRAM_EXCL;
  308. if (file->f_mode & 2)
  309. nvram_open_mode &= ~NVRAM_WRITE;
  310. spin_unlock(&nvram_state_lock);
  311. return 0;
  312. }
  313. #ifndef CONFIG_PROC_FS
  314. static int
  315. nvram_read_proc(char *buffer, char **start, off_t offset,
  316. int size, int *eof, void *data)
  317. {
  318. return 0;
  319. }
  320. #else
  321. static int
  322. nvram_read_proc(char *buffer, char **start, off_t offset,
  323. int size, int *eof, void *data)
  324. {
  325. unsigned char contents[NVRAM_BYTES];
  326. int i, len = 0;
  327. off_t begin = 0;
  328. spin_lock_irq(&rtc_lock);
  329. for (i = 0; i < NVRAM_BYTES; ++i)
  330. contents[i] = __nvram_read_byte(i);
  331. spin_unlock_irq(&rtc_lock);
  332. *eof = mach_proc_infos(contents, buffer, &len, &begin, offset, size);
  333. if (offset >= begin + len)
  334. return 0;
  335. *start = buffer + (offset - begin);
  336. return (size < begin + len - offset) ? size : begin + len - offset;
  337. }
  338. /* This macro frees the machine specific function from bounds checking and
  339. * this like that... */
  340. #define PRINT_PROC(fmt,args...) \
  341. do { \
  342. *len += sprintf(buffer+*len, fmt, ##args); \
  343. if (*begin + *len > offset + size) \
  344. return 0; \
  345. if (*begin + *len < offset) { \
  346. *begin += *len; \
  347. *len = 0; \
  348. } \
  349. } while(0)
  350. #endif /* CONFIG_PROC_FS */
  351. static const struct file_operations nvram_fops = {
  352. .owner = THIS_MODULE,
  353. .llseek = nvram_llseek,
  354. .read = nvram_read,
  355. .write = nvram_write,
  356. .ioctl = nvram_ioctl,
  357. .open = nvram_open,
  358. .release = nvram_release,
  359. };
  360. static struct miscdevice nvram_dev = {
  361. NVRAM_MINOR,
  362. "nvram",
  363. &nvram_fops
  364. };
  365. static int __init
  366. nvram_init(void)
  367. {
  368. int ret;
  369. /* First test whether the driver should init at all */
  370. if (!CHECK_DRIVER_INIT())
  371. return -ENXIO;
  372. ret = misc_register(&nvram_dev);
  373. if (ret) {
  374. printk(KERN_ERR "nvram: can't misc_register on minor=%d\n",
  375. NVRAM_MINOR);
  376. goto out;
  377. }
  378. if (!create_proc_read_entry("driver/nvram", 0, NULL, nvram_read_proc,
  379. NULL)) {
  380. printk(KERN_ERR "nvram: can't create /proc/driver/nvram\n");
  381. ret = -ENOMEM;
  382. goto outmisc;
  383. }
  384. ret = 0;
  385. printk(KERN_INFO "Non-volatile memory driver v" NVRAM_VERSION "\n");
  386. out:
  387. return ret;
  388. outmisc:
  389. misc_deregister(&nvram_dev);
  390. goto out;
  391. }
  392. static void __exit
  393. nvram_cleanup_module(void)
  394. {
  395. remove_proc_entry("driver/nvram", NULL);
  396. misc_deregister(&nvram_dev);
  397. }
  398. module_init(nvram_init);
  399. module_exit(nvram_cleanup_module);
  400. /*
  401. * Machine specific functions
  402. */
  403. #if MACH == PC
  404. static int
  405. pc_check_checksum(void)
  406. {
  407. int i;
  408. unsigned short sum = 0;
  409. unsigned short expect;
  410. for (i = PC_CKS_RANGE_START; i <= PC_CKS_RANGE_END; ++i)
  411. sum += __nvram_read_byte(i);
  412. expect = __nvram_read_byte(PC_CKS_LOC)<<8 |
  413. __nvram_read_byte(PC_CKS_LOC+1);
  414. return ((sum & 0xffff) == expect);
  415. }
  416. static void
  417. pc_set_checksum(void)
  418. {
  419. int i;
  420. unsigned short sum = 0;
  421. for (i = PC_CKS_RANGE_START; i <= PC_CKS_RANGE_END; ++i)
  422. sum += __nvram_read_byte(i);
  423. __nvram_write_byte(sum >> 8, PC_CKS_LOC);
  424. __nvram_write_byte(sum & 0xff, PC_CKS_LOC + 1);
  425. }
  426. #ifdef CONFIG_PROC_FS
  427. static char *floppy_types[] = {
  428. "none", "5.25'' 360k", "5.25'' 1.2M", "3.5'' 720k", "3.5'' 1.44M",
  429. "3.5'' 2.88M", "3.5'' 2.88M"
  430. };
  431. static char *gfx_types[] = {
  432. "EGA, VGA, ... (with BIOS)",
  433. "CGA (40 cols)",
  434. "CGA (80 cols)",
  435. "monochrome",
  436. };
  437. static int
  438. pc_proc_infos(unsigned char *nvram, char *buffer, int *len,
  439. off_t *begin, off_t offset, int size)
  440. {
  441. int checksum;
  442. int type;
  443. spin_lock_irq(&rtc_lock);
  444. checksum = __nvram_check_checksum();
  445. spin_unlock_irq(&rtc_lock);
  446. PRINT_PROC("Checksum status: %svalid\n", checksum ? "" : "not ");
  447. PRINT_PROC("# floppies : %d\n",
  448. (nvram[6] & 1) ? (nvram[6] >> 6) + 1 : 0);
  449. PRINT_PROC("Floppy 0 type : ");
  450. type = nvram[2] >> 4;
  451. if (type < ARRAY_SIZE(floppy_types))
  452. PRINT_PROC("%s\n", floppy_types[type]);
  453. else
  454. PRINT_PROC("%d (unknown)\n", type);
  455. PRINT_PROC("Floppy 1 type : ");
  456. type = nvram[2] & 0x0f;
  457. if (type < ARRAY_SIZE(floppy_types))
  458. PRINT_PROC("%s\n", floppy_types[type]);
  459. else
  460. PRINT_PROC("%d (unknown)\n", type);
  461. PRINT_PROC("HD 0 type : ");
  462. type = nvram[4] >> 4;
  463. if (type)
  464. PRINT_PROC("%02x\n", type == 0x0f ? nvram[11] : type);
  465. else
  466. PRINT_PROC("none\n");
  467. PRINT_PROC("HD 1 type : ");
  468. type = nvram[4] & 0x0f;
  469. if (type)
  470. PRINT_PROC("%02x\n", type == 0x0f ? nvram[12] : type);
  471. else
  472. PRINT_PROC("none\n");
  473. PRINT_PROC("HD type 48 data: %d/%d/%d C/H/S, precomp %d, lz %d\n",
  474. nvram[18] | (nvram[19] << 8),
  475. nvram[20], nvram[25],
  476. nvram[21] | (nvram[22] << 8), nvram[23] | (nvram[24] << 8));
  477. PRINT_PROC("HD type 49 data: %d/%d/%d C/H/S, precomp %d, lz %d\n",
  478. nvram[39] | (nvram[40] << 8),
  479. nvram[41], nvram[46],
  480. nvram[42] | (nvram[43] << 8), nvram[44] | (nvram[45] << 8));
  481. PRINT_PROC("DOS base memory: %d kB\n", nvram[7] | (nvram[8] << 8));
  482. PRINT_PROC("Extended memory: %d kB (configured), %d kB (tested)\n",
  483. nvram[9] | (nvram[10] << 8), nvram[34] | (nvram[35] << 8));
  484. PRINT_PROC("Gfx adapter : %s\n", gfx_types[(nvram[6] >> 4) & 3]);
  485. PRINT_PROC("FPU : %sinstalled\n",
  486. (nvram[6] & 2) ? "" : "not ");
  487. return 1;
  488. }
  489. #endif
  490. #endif /* MACH == PC */
  491. #if MACH == COBALT
  492. /* the cobalt CMOS has a wider range of its checksum */
  493. static int cobalt_check_checksum(void)
  494. {
  495. int i;
  496. unsigned short sum = 0;
  497. unsigned short expect;
  498. for (i = COBT_CMOS_CKS_START; i <= COBT_CMOS_CKS_END; ++i) {
  499. if ((i == COBT_CMOS_CHECKSUM) || (i == (COBT_CMOS_CHECKSUM+1)))
  500. continue;
  501. sum += __nvram_read_byte(i);
  502. }
  503. expect = __nvram_read_byte(COBT_CMOS_CHECKSUM) << 8 |
  504. __nvram_read_byte(COBT_CMOS_CHECKSUM+1);
  505. return ((sum & 0xffff) == expect);
  506. }
  507. static void cobalt_set_checksum(void)
  508. {
  509. int i;
  510. unsigned short sum = 0;
  511. for (i = COBT_CMOS_CKS_START; i <= COBT_CMOS_CKS_END; ++i) {
  512. if ((i == COBT_CMOS_CHECKSUM) || (i == (COBT_CMOS_CHECKSUM+1)))
  513. continue;
  514. sum += __nvram_read_byte(i);
  515. }
  516. __nvram_write_byte(sum >> 8, COBT_CMOS_CHECKSUM);
  517. __nvram_write_byte(sum & 0xff, COBT_CMOS_CHECKSUM+1);
  518. }
  519. #ifdef CONFIG_PROC_FS
  520. static int cobalt_proc_infos(unsigned char *nvram, char *buffer, int *len,
  521. off_t *begin, off_t offset, int size)
  522. {
  523. int i;
  524. unsigned int checksum;
  525. unsigned int flags;
  526. char sernum[14];
  527. char *key = "cNoEbTaWlOtR!";
  528. unsigned char bto_csum;
  529. spin_lock_irq(&rtc_lock);
  530. checksum = __nvram_check_checksum();
  531. spin_unlock_irq(&rtc_lock);
  532. PRINT_PROC("Checksum status: %svalid\n", checksum ? "" : "not ");
  533. flags = nvram[COBT_CMOS_FLAG_BYTE_0] << 8
  534. | nvram[COBT_CMOS_FLAG_BYTE_1];
  535. PRINT_PROC("Console: %s\n",
  536. flags & COBT_CMOS_CONSOLE_FLAG ? "on": "off");
  537. PRINT_PROC("Firmware Debug Messages: %s\n",
  538. flags & COBT_CMOS_DEBUG_FLAG ? "on": "off");
  539. PRINT_PROC("Auto Prompt: %s\n",
  540. flags & COBT_CMOS_AUTO_PROMPT_FLAG ? "on": "off");
  541. PRINT_PROC("Shutdown Status: %s\n",
  542. flags & COBT_CMOS_CLEAN_BOOT_FLAG ? "clean": "dirty");
  543. PRINT_PROC("Hardware Probe: %s\n",
  544. flags & COBT_CMOS_HW_NOPROBE_FLAG ? "partial": "full");
  545. PRINT_PROC("System Fault: %sdetected\n",
  546. flags & COBT_CMOS_SYSFAULT_FLAG ? "": "not ");
  547. PRINT_PROC("Panic on OOPS: %s\n",
  548. flags & COBT_CMOS_OOPSPANIC_FLAG ? "yes": "no");
  549. PRINT_PROC("Delayed Cache Initialization: %s\n",
  550. flags & COBT_CMOS_DELAY_CACHE_FLAG ? "yes": "no");
  551. PRINT_PROC("Show Logo at Boot: %s\n",
  552. flags & COBT_CMOS_NOLOGO_FLAG ? "no": "yes");
  553. PRINT_PROC("Boot Method: ");
  554. switch (nvram[COBT_CMOS_BOOT_METHOD]) {
  555. case COBT_CMOS_BOOT_METHOD_DISK:
  556. PRINT_PROC("disk\n");
  557. break;
  558. case COBT_CMOS_BOOT_METHOD_ROM:
  559. PRINT_PROC("rom\n");
  560. break;
  561. case COBT_CMOS_BOOT_METHOD_NET:
  562. PRINT_PROC("net\n");
  563. break;
  564. default:
  565. PRINT_PROC("unknown\n");
  566. break;
  567. }
  568. PRINT_PROC("Primary Boot Device: %d:%d\n",
  569. nvram[COBT_CMOS_BOOT_DEV0_MAJ],
  570. nvram[COBT_CMOS_BOOT_DEV0_MIN] );
  571. PRINT_PROC("Secondary Boot Device: %d:%d\n",
  572. nvram[COBT_CMOS_BOOT_DEV1_MAJ],
  573. nvram[COBT_CMOS_BOOT_DEV1_MIN] );
  574. PRINT_PROC("Tertiary Boot Device: %d:%d\n",
  575. nvram[COBT_CMOS_BOOT_DEV2_MAJ],
  576. nvram[COBT_CMOS_BOOT_DEV2_MIN] );
  577. PRINT_PROC("Uptime: %d\n",
  578. nvram[COBT_CMOS_UPTIME_0] << 24 |
  579. nvram[COBT_CMOS_UPTIME_1] << 16 |
  580. nvram[COBT_CMOS_UPTIME_2] << 8 |
  581. nvram[COBT_CMOS_UPTIME_3]);
  582. PRINT_PROC("Boot Count: %d\n",
  583. nvram[COBT_CMOS_BOOTCOUNT_0] << 24 |
  584. nvram[COBT_CMOS_BOOTCOUNT_1] << 16 |
  585. nvram[COBT_CMOS_BOOTCOUNT_2] << 8 |
  586. nvram[COBT_CMOS_BOOTCOUNT_3]);
  587. /* 13 bytes of serial num */
  588. for (i=0 ; i<13 ; i++) {
  589. sernum[i] = nvram[COBT_CMOS_SYS_SERNUM_0 + i];
  590. }
  591. sernum[13] = '\0';
  592. checksum = 0;
  593. for (i=0 ; i<13 ; i++) {
  594. checksum += sernum[i] ^ key[i];
  595. }
  596. checksum = ((checksum & 0x7f) ^ (0xd6)) & 0xff;
  597. PRINT_PROC("Serial Number: %s", sernum);
  598. if (checksum != nvram[COBT_CMOS_SYS_SERNUM_CSUM]) {
  599. PRINT_PROC(" (invalid checksum)");
  600. }
  601. PRINT_PROC("\n");
  602. PRINT_PROC("Rom Revison: %d.%d.%d\n", nvram[COBT_CMOS_ROM_REV_MAJ],
  603. nvram[COBT_CMOS_ROM_REV_MIN], nvram[COBT_CMOS_ROM_REV_REV]);
  604. PRINT_PROC("BTO Server: %d.%d.%d.%d", nvram[COBT_CMOS_BTO_IP_0],
  605. nvram[COBT_CMOS_BTO_IP_1], nvram[COBT_CMOS_BTO_IP_2],
  606. nvram[COBT_CMOS_BTO_IP_3]);
  607. bto_csum = nvram[COBT_CMOS_BTO_IP_0] + nvram[COBT_CMOS_BTO_IP_1]
  608. + nvram[COBT_CMOS_BTO_IP_2] + nvram[COBT_CMOS_BTO_IP_3];
  609. if (bto_csum != nvram[COBT_CMOS_BTO_IP_CSUM]) {
  610. PRINT_PROC(" (invalid checksum)");
  611. }
  612. PRINT_PROC("\n");
  613. if (flags & COBT_CMOS_VERSION_FLAG
  614. && nvram[COBT_CMOS_VERSION] >= COBT_CMOS_VER_BTOCODE) {
  615. PRINT_PROC("BTO Code: 0x%x\n",
  616. nvram[COBT_CMOS_BTO_CODE_0] << 24 |
  617. nvram[COBT_CMOS_BTO_CODE_1] << 16 |
  618. nvram[COBT_CMOS_BTO_CODE_2] << 8 |
  619. nvram[COBT_CMOS_BTO_CODE_3]);
  620. }
  621. return 1;
  622. }
  623. #endif /* CONFIG_PROC_FS */
  624. #endif /* MACH == COBALT */
  625. #if MACH == ATARI
  626. static int
  627. atari_check_checksum(void)
  628. {
  629. int i;
  630. unsigned char sum = 0;
  631. for (i = ATARI_CKS_RANGE_START; i <= ATARI_CKS_RANGE_END; ++i)
  632. sum += __nvram_read_byte(i);
  633. return (__nvram_read_byte(ATARI_CKS_LOC) == (~sum & 0xff) &&
  634. __nvram_read_byte(ATARI_CKS_LOC + 1) == (sum & 0xff));
  635. }
  636. static void
  637. atari_set_checksum(void)
  638. {
  639. int i;
  640. unsigned char sum = 0;
  641. for (i = ATARI_CKS_RANGE_START; i <= ATARI_CKS_RANGE_END; ++i)
  642. sum += __nvram_read_byte(i);
  643. __nvram_write_byte(~sum, ATARI_CKS_LOC);
  644. __nvram_write_byte(sum, ATARI_CKS_LOC + 1);
  645. }
  646. #ifdef CONFIG_PROC_FS
  647. static struct {
  648. unsigned char val;
  649. char *name;
  650. } boot_prefs[] = {
  651. { 0x80, "TOS" },
  652. { 0x40, "ASV" },
  653. { 0x20, "NetBSD (?)" },
  654. { 0x10, "Linux" },
  655. { 0x00, "unspecified" }
  656. };
  657. static char *languages[] = {
  658. "English (US)",
  659. "German",
  660. "French",
  661. "English (UK)",
  662. "Spanish",
  663. "Italian",
  664. "6 (undefined)",
  665. "Swiss (French)",
  666. "Swiss (German)"
  667. };
  668. static char *dateformat[] = {
  669. "MM%cDD%cYY",
  670. "DD%cMM%cYY",
  671. "YY%cMM%cDD",
  672. "YY%cDD%cMM",
  673. "4 (undefined)",
  674. "5 (undefined)",
  675. "6 (undefined)",
  676. "7 (undefined)"
  677. };
  678. static char *colors[] = {
  679. "2", "4", "16", "256", "65536", "??", "??", "??"
  680. };
  681. static int
  682. atari_proc_infos(unsigned char *nvram, char *buffer, int *len,
  683. off_t *begin, off_t offset, int size)
  684. {
  685. int checksum = nvram_check_checksum();
  686. int i;
  687. unsigned vmode;
  688. PRINT_PROC("Checksum status : %svalid\n", checksum ? "" : "not ");
  689. PRINT_PROC("Boot preference : ");
  690. for (i = ARRAY_SIZE(boot_prefs) - 1; i >= 0; --i) {
  691. if (nvram[1] == boot_prefs[i].val) {
  692. PRINT_PROC("%s\n", boot_prefs[i].name);
  693. break;
  694. }
  695. }
  696. if (i < 0)
  697. PRINT_PROC("0x%02x (undefined)\n", nvram[1]);
  698. PRINT_PROC("SCSI arbitration : %s\n",
  699. (nvram[16] & 0x80) ? "on" : "off");
  700. PRINT_PROC("SCSI host ID : ");
  701. if (nvram[16] & 0x80)
  702. PRINT_PROC("%d\n", nvram[16] & 7);
  703. else
  704. PRINT_PROC("n/a\n");
  705. /* the following entries are defined only for the Falcon */
  706. if ((atari_mch_cookie >> 16) != ATARI_MCH_FALCON)
  707. return 1;
  708. PRINT_PROC("OS language : ");
  709. if (nvram[6] < ARRAY_SIZE(languages))
  710. PRINT_PROC("%s\n", languages[nvram[6]]);
  711. else
  712. PRINT_PROC("%u (undefined)\n", nvram[6]);
  713. PRINT_PROC("Keyboard language: ");
  714. if (nvram[7] < ARRAY_SIZE(languages))
  715. PRINT_PROC("%s\n", languages[nvram[7]]);
  716. else
  717. PRINT_PROC("%u (undefined)\n", nvram[7]);
  718. PRINT_PROC("Date format : ");
  719. PRINT_PROC(dateformat[nvram[8] & 7],
  720. nvram[9] ? nvram[9] : '/', nvram[9] ? nvram[9] : '/');
  721. PRINT_PROC(", %dh clock\n", nvram[8] & 16 ? 24 : 12);
  722. PRINT_PROC("Boot delay : ");
  723. if (nvram[10] == 0)
  724. PRINT_PROC("default");
  725. else
  726. PRINT_PROC("%ds%s\n", nvram[10],
  727. nvram[10] < 8 ? ", no memory test" : "");
  728. vmode = (nvram[14] << 8) || nvram[15];
  729. PRINT_PROC("Video mode : %s colors, %d columns, %s %s monitor\n",
  730. colors[vmode & 7],
  731. vmode & 8 ? 80 : 40,
  732. vmode & 16 ? "VGA" : "TV", vmode & 32 ? "PAL" : "NTSC");
  733. PRINT_PROC(" %soverscan, compat. mode %s%s\n",
  734. vmode & 64 ? "" : "no ",
  735. vmode & 128 ? "on" : "off",
  736. vmode & 256 ?
  737. (vmode & 16 ? ", line doubling" : ", half screen") : "");
  738. return 1;
  739. }
  740. #endif
  741. #endif /* MACH == ATARI */
  742. MODULE_LICENSE("GPL");
  743. EXPORT_SYMBOL(__nvram_read_byte);
  744. EXPORT_SYMBOL(nvram_read_byte);
  745. EXPORT_SYMBOL(__nvram_write_byte);
  746. EXPORT_SYMBOL(nvram_write_byte);
  747. EXPORT_SYMBOL(__nvram_check_checksum);
  748. EXPORT_SYMBOL(nvram_check_checksum);
  749. MODULE_ALIAS_MISCDEV(NVRAM_MINOR);