prep_nvram.c 2.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137
  1. /*
  2. * arch/ppc/kernel/prep_nvram.c
  3. *
  4. * Copyright (C) 1998 Corey Minyard
  5. *
  6. * This reads the NvRAM on PReP compliant machines (generally from IBM or
  7. * Motorola). Motorola kept the format of NvRAM in their ROM, PPCBUG, the
  8. * same, long after they had stopped producing PReP compliant machines. So
  9. * this code is useful in those cases as well.
  10. *
  11. */
  12. #include <linux/init.h>
  13. #include <linux/delay.h>
  14. #include <linux/slab.h>
  15. #include <linux/ioport.h>
  16. #include <asm/sections.h>
  17. #include <asm/io.h>
  18. #include <asm/machdep.h>
  19. #include <asm/prep_nvram.h>
  20. static char nvramData[MAX_PREP_NVRAM];
  21. static NVRAM_MAP *nvram=(NVRAM_MAP *)&nvramData[0];
  22. unsigned char prep_nvram_read_val(int addr)
  23. {
  24. outb(addr, PREP_NVRAM_AS0);
  25. outb(addr>>8, PREP_NVRAM_AS1);
  26. return inb(PREP_NVRAM_DATA);
  27. }
  28. void prep_nvram_write_val(int addr,
  29. unsigned char val)
  30. {
  31. outb(addr, PREP_NVRAM_AS0);
  32. outb(addr>>8, PREP_NVRAM_AS1);
  33. outb(val, PREP_NVRAM_DATA);
  34. }
  35. void __init init_prep_nvram(void)
  36. {
  37. unsigned char *nvp;
  38. int i;
  39. int nvramSize;
  40. /*
  41. * The following could fail if the NvRAM were corrupt but
  42. * we expect the boot firmware to have checked its checksum
  43. * before boot
  44. */
  45. nvp = (char *) &nvram->Header;
  46. for (i=0; i<sizeof(HEADER); i++)
  47. {
  48. *nvp = ppc_md.nvram_read_val(i);
  49. nvp++;
  50. }
  51. /*
  52. * The PReP NvRAM may be any size so read in the header to
  53. * determine how much we must read in order to get the complete
  54. * GE area
  55. */
  56. nvramSize=(int)nvram->Header.GEAddress+nvram->Header.GELength;
  57. if(nvramSize>MAX_PREP_NVRAM)
  58. {
  59. /*
  60. * NvRAM is too large
  61. */
  62. nvram->Header.GELength=0;
  63. return;
  64. }
  65. /*
  66. * Read the remainder of the PReP NvRAM
  67. */
  68. nvp = (char *) &nvram->GEArea[0];
  69. for (i=sizeof(HEADER); i<nvramSize; i++)
  70. {
  71. *nvp = ppc_md.nvram_read_val(i);
  72. nvp++;
  73. }
  74. }
  75. char *prep_nvram_get_var(const char *name)
  76. {
  77. char *cp;
  78. int namelen;
  79. namelen = strlen(name);
  80. cp = prep_nvram_first_var();
  81. while (cp != NULL) {
  82. if ((strncmp(name, cp, namelen) == 0)
  83. && (cp[namelen] == '='))
  84. {
  85. return cp+namelen+1;
  86. }
  87. cp = prep_nvram_next_var(cp);
  88. }
  89. return NULL;
  90. }
  91. char *prep_nvram_first_var(void)
  92. {
  93. if (nvram->Header.GELength == 0) {
  94. return NULL;
  95. } else {
  96. return (((char *)nvram)
  97. + ((unsigned int) nvram->Header.GEAddress));
  98. }
  99. }
  100. char *prep_nvram_next_var(char *name)
  101. {
  102. char *cp;
  103. cp = name;
  104. while (((cp - ((char *) nvram->GEArea)) < nvram->Header.GELength)
  105. && (*cp != '\0'))
  106. {
  107. cp++;
  108. }
  109. /* Skip over any null characters. */
  110. while (((cp - ((char *) nvram->GEArea)) < nvram->Header.GELength)
  111. && (*cp == '\0'))
  112. {
  113. cp++;
  114. }
  115. if ((cp - ((char *) nvram->GEArea)) < nvram->Header.GELength) {
  116. return cp;
  117. } else {
  118. return NULL;
  119. }
  120. }