superiotool: Cosmetics and coding style fixes.
[coreboot.git] / util / msrtool / msrutils.c
index 3c64242a873c757374abe1a66deaad272053d84b..2ceb60cd068f827e009657752e2bfc929d60a0a6 100644 (file)
@@ -25,7 +25,7 @@
 
 static void print_bitdef(FILE *f, const struct msrbits *mb, const char *tail) {
        uint8_t endbit;
-       if (!reserved && !strcmp(mb->name, "RSVD"))
+       if (!reserved && 0 == strcmp(mb->name, "RSVD"))
                return;
        if (1 == mb->size)
                fprintf(f, "# %5d", mb->start);
@@ -33,7 +33,7 @@ static void print_bitdef(FILE *f, const struct msrbits *mb, const char *tail) {
                endbit = mb->start - mb->size + 1;
                fprintf(f, "# %*d:%d", endbit < 10 ? 3 : 2, mb->start, endbit);
        }
-       if (!strcmp(mb->name, "RSVD"))
+       if (0 == strcmp(mb->name, "RSVD"))
                fprintf(f, " [%s]", mb->desc);
        else
                fprintf(f, " %s %s", mb->name, mb->desc);
@@ -55,7 +55,6 @@ static void print_bitval(FILE *f, const struct msrbits *mb, const struct msr val
                        fprintf(f, "%d", (tmp.hi || tmp.lo) ? 1 : 0);
                        mask = msr_shr(mask, 1);
                }
-               /* TODO */
                break;
        case PRESENT_DEC:
                fprintf(f, "%d", val.lo);
@@ -78,13 +77,35 @@ static void print_bitval(FILE *f, const struct msrbits *mb, const struct msr val
 
 void hexprint(FILE *f, const struct msr val, const uint8_t bits) {
        if (bits <= 4)
-               fprintf(f, "0x%x", (uint8_t)(val.lo & 0x0f));
+               fprintf(f, "0x%01x", val.lo & 0xf);
        else if (bits <= 8)
-               fprintf(f, "0x%02x", (uint8_t)(val.lo & 0xff));
+               fprintf(f, "0x%02x", val.lo & 0xff);
+       else if (bits <= 12)
+               fprintf(f, "0x%03x", val.lo & 0xfff);
        else if (bits <= 16)
-               fprintf(f, "0x%04x", (uint16_t)(val.lo & 0xffff));
+               fprintf(f, "0x%04x", val.lo & 0xffff);
+       else if (bits <= 20)
+               fprintf(f, "0x%05x", val.lo & 0xfffff);
+       else if (bits <= 24)
+               fprintf(f, "0x%06x", val.lo & 0xffffff);
+       else if (bits <= 28)
+               fprintf(f, "0x%07x", val.lo & 0xfffffff);
        else if (bits <= 32)
                fprintf(f, "0x%08x", val.lo);
+       else if (bits <= 36)
+               fprintf(f, "0x%01x%08x", val.hi & 0xf, val.lo);
+       else if (bits <= 40)
+               fprintf(f, "0x%02x%08x", val.hi & 0xff, val.lo);
+       else if (bits <= 44)
+               fprintf(f, "0x%03x%08x", val.hi & 0xfff, val.lo);
+       else if (bits <= 48)
+               fprintf(f, "0x%04x%08x", val.hi & 0xffff, val.lo);
+       else if (bits <= 52)
+               fprintf(f, "0x%05x%08x", val.hi & 0xfffff, val.lo);
+       else if (bits <= 56)
+               fprintf(f, "0x%06x%08x", val.hi & 0xffffff, val.lo);
+       else if (bits <= 60)
+               fprintf(f, "0x%07x%08x", val.hi & 0xfffffff, val.lo);
        else
                fprintf(f, "0x%08x%08x", val.hi, val.lo);
 }
@@ -138,10 +159,26 @@ const struct msrdef *findmsrdef(const uint32_t addr) {
        return NULL;
 }
 
+uint32_t msraddrbyname(const char *name) {
+       uint8_t t;
+       const uint32_t addr = strtoul(name, NULL, 16);
+       const struct msrdef *m;
+       if (!targets)
+               return addr;
+       for (t = 0; t < targets_found; t++)
+               for (m = targets[t]->msrs; !MSR_ISEOT(*m); m++) {
+                       if (addr == m->addr)
+                               return m->addr;
+                       if (!strcasecmp(name, m->symbol))
+                               return m->addr;
+               }
+       return addr;
+}
+
 void dumpmsrdefs(const struct targetdef *t) {
        const struct msrdef *m;
        const struct msrbits *mb;
-       if (!t)
+       if (NULL == t)
                return;
        printf("# %s MSRs:\n", t->name);
        for (m = t->msrs; !MSR_ISEOT(*m); m++) {
@@ -158,7 +195,7 @@ int dumpmsrdefsvals(FILE *f, const struct targetdef *t, const uint8_t cpu) {
        struct msr val = MSR1(0);
        const struct msrdef *m;
        const struct msrbits *mb;
-       if (!t)
+       if (NULL == t)
                return 1;
        fprintf(f, "# %s MSRs:\n", t->name);
        for (m = t->msrs; !MSR_ISEOT(*m); m++) {
@@ -176,9 +213,9 @@ int dumpmsrdefsvals(FILE *f, const struct targetdef *t, const uint8_t cpu) {
 
 /**
  * Parse a hexadecimal string into an MSR value.
- * 
+ *
  * Leading 0x or 0X is optional, the string is always parsed as hexadecimal.
- * Any non-hexadecimal character can be used to separate the high 32 bits and
+ * Any non-hexadecimal character except ' ' can separate the high 32 bits and
  * the low 32 bits. If there is such a separator, high and low values do not
  * need to be zero padded. If there is no separator, the last <=8 digits are
  * the low 32 bits and any characters before them are the high 32 bits.
@@ -190,15 +227,16 @@ int dumpmsrdefsvals(FILE *f, const struct targetdef *t, const uint8_t cpu) {
  * @param str The string to parse. The string must be writable but will be
  * restored before return.
  * @param msr Pointer to the struct msr where the value will be stored.
+ * @param endptr If endptr is not NULL, *endptr will point to after the MSR.
  * @return 1 on success, 0 on parse failure. msr is unchanged on failure.
  */
-uint8_t str2msr(char *str, struct msr *msr) {
+uint8_t str2msr(char *str, struct msr *msr, char **endptr) {
        char c;
        size_t len, lo;
        if (0 == strncmp(str, "0x", 2) || 0 == strncmp(str, "0X", 2))
                str += 2;
        len = strspn(str, HEXCHARS);
-       if (len <= 8 && 0 == str[len]) {
+       if (len <= 8 && (0 == str[len] || ' ' == str[len])) {
                msr->hi = 0;
                lo = 0;
        } else if (len <= 8) {
@@ -216,7 +254,7 @@ uint8_t str2msr(char *str, struct msr *msr) {
                msr->hi = strtoul(str, NULL, 16);
                str[lo] = c;
        }
-       msr->lo = strtoul(str + lo, NULL, 16);
+       msr->lo = strtoul(str + lo, endptr, 16);
        return 1;
 }
 
@@ -225,10 +263,10 @@ void decodemsr(const uint8_t cpu, const uint32_t addr, const struct msr val) {
        const struct msrdef *m = findmsrdef(addr);
        const struct msrbits *mb;
 
-       if (m)
+       if (NULL != m)
                printf("# %s ", m->symbol);
        printf("0x%08x = 0x%08x%08x\n", addr, val.hi, val.lo);
-       if (!m) {
+       if (NULL == m) {
                fprintf(stderr, "Sorry - no definition exists for this MSR! Please add it and send a signed-off\n");
                fprintf(stderr, "patch to coreboot@coreboot.org. Thanks for your help!\n");
                return;
@@ -266,7 +304,7 @@ uint8_t diff_msr(FILE *f, const uint32_t addr, const struct msr a, const struct
        if (a.hi == b.hi && a.lo == b.lo)
                return 0;
 
-       if (!m) {
+       if (NULL == m) {
                fprintf(stderr, "MSR 0x%08x has no definition! Please add it and send a Signed-off-by patch\n", addr);
                fprintf(stderr, "to coreboot@coreboot.org. Thank you for your help!\n");
                return 1;