nvram.c 24 KB

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