nvram.c 22 KB

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