nvram.c 22 KB

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