libpayload: Add get_option_from()
[coreboot.git] / src / pc80 / mc146818rtc.c
index 69787093796c76b678d361801a7f8b6c6282df89..4bbfa9ea045025a2e3f6b4ab3dd41d30aa48ca0d 100644 (file)
@@ -1,8 +1,11 @@
 #include <console/console.h>
-#include <arch/io.h>
 #include <pc80/mc146818rtc.h>
 #include <boot/coreboot_tables.h>
 #include <string.h>
+#if CONFIG_USE_OPTION_TABLE
+#include "option_table.h"
+#include <cbfs.h>
+#endif
 
 /* control registers - Moto names
  */
 # define RTC_VRT 0x80          /* valid RAM and time */
 /**********************************************************************/
 
-static inline unsigned char cmos_read(unsigned char addr)
-{
-       int offs = 0;
-       if (addr >= 128) {
-               offs = 2;
-               addr -= 128;
-       }
-       outb(addr, RTC_BASE_PORT + offs + 0);
-       return inb(RTC_BASE_PORT + offs + 1);
-}
-
-static inline void cmos_write(unsigned char val, unsigned char addr)
-{
-       int offs = 0;
-       if (addr >= 128) {
-               offs = 2;
-               addr -= 128;
-       }
-       outb(addr, RTC_BASE_PORT + offs + 0);
-       outb(val, RTC_BASE_PORT + offs + 1);
-}
-
+#if CONFIG_USE_OPTION_TABLE
 static int rtc_checksum_valid(int range_start, int range_end, int cks_loc)
 {
        int i;
@@ -120,6 +102,7 @@ static void rtc_set_checksum(int range_start, int range_end, int cks_loc)
        cmos_write(((sum >> 8) & 0x0ff), cks_loc);
        cmos_write(((sum >> 0) & 0x0ff), cks_loc+1);
 }
+#endif
 
 #if CONFIG_ARCH_X86
 #define RTC_CONTROL_DEFAULT (RTC_24H)
@@ -133,14 +116,14 @@ static void rtc_set_checksum(int range_start, int range_end, int cks_loc)
 
 void rtc_init(int invalid)
 {
-#if CONFIG_HAVE_OPTION_TABLE
+#if CONFIG_USE_OPTION_TABLE
        unsigned char x;
        int cmos_invalid, checksum_invalid;
 #endif
 
-       printk_debug("RTC Init\n");
+       printk(BIOS_DEBUG, "RTC Init\n");
 
-#if CONFIG_HAVE_OPTION_TABLE
+#if CONFIG_USE_OPTION_TABLE
        /* See if there has been a CMOS power problem. */
        x = cmos_read(RTC_VALID);
        cmos_invalid = !(x & RTC_VRT);
@@ -150,8 +133,8 @@ void rtc_init(int invalid)
                        PC_CKS_RANGE_END,PC_CKS_LOC);
 
        if (invalid || cmos_invalid || checksum_invalid) {
-               printk_warning("RTC:%s%s%s zeroing cmos\n",
-                       invalid?" Clear requested":"", 
+               printk(BIOS_WARNING, "RTC:%s%s%s zeroing cmos\n",
+                       invalid?" Clear requested":"",
                        cmos_invalid?" Power Problem":"",
                        checksum_invalid?" Checksum invalid":"");
 #if 0
@@ -161,7 +144,7 @@ void rtc_init(int invalid)
                for(i = 10; i < 48; i++) {
                        cmos_write(0, i);
                }
-               
+
                if (cmos_invalid) {
                        /* Now setup a default date of Sat 1 January 2000 */
                        cmos_write(0, 0x00); /* seconds */
@@ -181,12 +164,12 @@ void rtc_init(int invalid)
        /* Setup the frequency it operates at */
        cmos_write(RTC_FREQ_SELECT_DEFAULT, RTC_FREQ_SELECT);
 
-#if CONFIG_HAVE_OPTION_TABLE
+#if CONFIG_USE_OPTION_TABLE
        /* See if there is a LB CMOS checksum error */
-       checksum_invalid = !rtc_checksum_valid(CONFIG_LB_CKS_RANGE_START,
-                       CONFIG_LB_CKS_RANGE_END,CONFIG_LB_CKS_LOC);
+       checksum_invalid = !rtc_checksum_valid(LB_CKS_RANGE_START,
+                       LB_CKS_RANGE_END,LB_CKS_LOC);
        if(checksum_invalid)
-               printk_debug("Invalid CMOS LB checksum\n");
+               printk(BIOS_DEBUG, "Invalid CMOS LB checksum\n");
 
        /* Make certain we have a valid checksum */
        rtc_set_checksum(PC_CKS_RANGE_START,
@@ -198,7 +181,7 @@ void rtc_init(int invalid)
 }
 
 
-#if CONFIG_USE_OPTION_TABLE == 1
+#if CONFIG_USE_OPTION_TABLE
 /* This routine returns the value of the requested bits
        input bit = bit count from the beginning of the cmos image
              length = number of bits to include in the value
@@ -213,7 +196,7 @@ static int get_cmos_value(unsigned long bit, unsigned long length, void *vret)
        unsigned long i;
        unsigned char uchar;
 
-       /* The table is checked when it is built to ensure all 
+       /* The table is checked when it is built to ensure all
                values are valid. */
        ret = vret;
        byte=bit/8;     /* find the byte where the data starts */
@@ -235,7 +218,6 @@ static int get_cmos_value(unsigned long bit, unsigned long length, void *vret)
 
 int get_option(void *dest, const char *name)
 {
-       extern struct cmos_option_table option_table;
        struct cmos_option_table *ct;
        struct cmos_entries *ce;
        size_t namelen;
@@ -243,9 +225,13 @@ int get_option(void *dest, const char *name)
 
        /* Figure out how long name is */
        namelen = strnlen(name, CMOS_MAX_NAME_LENGTH);
-       
+
        /* find the requested entry record */
-       ct=&option_table;
+       ct=cbfs_find_file("cmos_layout.bin", CBFS_COMPONENT_CMOS_LAYOUT);
+       if (!ct) {
+               printk(BIOS_ERR, "cmos_layout.bin could not be found. Options are disabled\n");
+               return(-2);
+       }
        ce=(struct cmos_entries*)((unsigned char *)ct + ct->header_length);
        for(;ce->tag==LB_TAG_OPTION;
                ce=(struct cmos_entries*)((unsigned char *)ce + ce->size)) {
@@ -255,15 +241,98 @@ int get_option(void *dest, const char *name)
                }
        }
        if(!found) {
-               printk_debug("WARNING: No cmos option '%s'\n", name);
+               printk(BIOS_DEBUG, "WARNING: No CMOS option '%s'.\n", name);
                return(-2);
        }
-       
+
        if(get_cmos_value(ce->bit, ce->length, dest))
                return(-3);
-       if(!rtc_checksum_valid(CONFIG_LB_CKS_RANGE_START,
-                       CONFIG_LB_CKS_RANGE_END,CONFIG_LB_CKS_LOC))
+       if(!rtc_checksum_valid(LB_CKS_RANGE_START,
+                       LB_CKS_RANGE_END,LB_CKS_LOC))
                return(-4);
        return(0);
 }
+
+static int set_cmos_value(unsigned long bit, unsigned long length, void *vret)
+{
+       unsigned char *ret;
+       unsigned long byte,byte_bit;
+       unsigned long i;
+       unsigned char uchar, mask;
+       unsigned int chksum_update_needed = 0;
+
+       ret = vret;
+       byte = bit / 8;                 /* find the byte where the data starts */
+       byte_bit = bit % 8;             /* find the bit in the byte where the data starts */
+       if(length <= 8) {               /* one byte or less */
+               mask = (1 << length) - 1;
+               mask <<= byte_bit;
+
+               uchar = cmos_read(byte);
+               uchar &= ~mask;
+               uchar |= (ret[0] << byte_bit);
+               cmos_write(uchar, byte);
+               if (byte >= LB_CKS_RANGE_START && byte <= LB_CKS_RANGE_END)
+                       chksum_update_needed = 1;
+       } else {                        /* more that one byte so transfer the whole bytes */
+               if (byte_bit || length % 8)
+                       return -1;
+
+               for(i=0; length; i++, length-=8, byte++)
+                       cmos_write(ret[i], byte);
+                       if (byte >= LB_CKS_RANGE_START && byte <= LB_CKS_RANGE_END)
+                               chksum_update_needed = 1;
+       }
+
+       if (chksum_update_needed) {
+               rtc_set_checksum(LB_CKS_RANGE_START,
+                       LB_CKS_RANGE_END,LB_CKS_LOC);
+       }
+       return 0;
+}
+
+
+int set_option(const char *name, void *value)
+{
+       struct cmos_option_table *ct;
+       struct cmos_entries *ce;
+       unsigned long length;
+       size_t namelen;
+       int found=0;
+
+       /* Figure out how long name is */
+       namelen = strnlen(name, CMOS_MAX_NAME_LENGTH);
+
+       /* find the requested entry record */
+       ct=cbfs_find_file("cmos_layout.bin", CBFS_COMPONENT_CMOS_LAYOUT);
+       if (!ct) {
+               printk(BIOS_ERR, "cmos_layout.bin could not be found. Options are disabled\n");
+               return(-2);
+       }
+       ce=(struct cmos_entries*)((unsigned char *)ct + ct->header_length);
+       for(;ce->tag==LB_TAG_OPTION;
+               ce=(struct cmos_entries*)((unsigned char *)ce + ce->size)) {
+               if (memcmp(ce->name, name, namelen) == 0) {
+                       found=1;
+                       break;
+               }
+       }
+       if(!found) {
+               printk(BIOS_DEBUG, "WARNING: No CMOS option '%s'.\n", name);
+               return(-2);
+       }
+
+       length = ce->length;
+       if (ce->config == 's') {
+               length = MAX(strlen((const char *)value) * 8, ce->length - 8);
+               /* make sure the string is null terminated */
+               if ((set_cmos_value(ce->bit + ce->length - 8, 8, &(u8[]){0})))
+                       return (-3);
+       }
+
+       if ((set_cmos_value(ce->bit, length, value)))
+               return (-3);
+
+       return 0;
+}
 #endif /* CONFIG_USE_OPTION_TABLE */