tree: drop last paragraph of GPL copyright header
[coreboot.git] / src / cpu / via / nano / update_ucode.c
blob44b074ed318dcb7b77ab70133258490bbb2dc55d
1 /*
2 * This file is part of the coreboot project.
4 * Copyright (C) 2012 Alexandru Gagniuc <mr.nuke.me@gmail.com>
6 * This program is free software: you can redistribute it and/or modify
7 * it under the terms of the GNU General Public License as published by
8 * the Free Software Foundation, either version 2 of the License, or
9 * (at your option) any later version.
11 * This program is distributed in the hope that it will be useful,
12 * but WITHOUT ANY WARRANTY; without even the implied warranty of
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 * GNU General Public License for more details.
17 #include "update_ucode.h"
18 #include <cpu/x86/msr.h>
19 #include <console/console.h>
20 #include <stddef.h>
21 #include <cpu/cpu.h>
22 #include <arch/cpu.h>
23 #include <cbfs.h>
25 static ucode_update_status nano_apply_ucode(const nano_ucode_header *ucode)
27 printk(BIOS_SPEW, "Attempting to apply microcode update\n");
29 msr_t msr;
30 /* Address of ucode block goes in msr.lo for 32-bit mode
31 * Now remember, we need to pass the address of the actual microcode,
32 * not the header. The header is just there to help us. */
33 msr.lo = (unsigned int)(&(ucode->ucode_start));
34 msr.hi = 0;
35 wrmsr(MSR_IA32_BIOS_UPDT_TRIG, msr);
37 /* Let's see if we updated successfully */
38 msr = rdmsr(MSR_UCODE_UPDATE_STATUS);
40 return msr.lo & 0x07;
43 static void nano_print_ucode_info(const nano_ucode_header *ucode)
45 printk(BIOS_SPEW, "Microcode update information:\n");
46 printk(BIOS_SPEW, "Name: %8s\n", ucode->name );
47 printk(BIOS_SPEW, "Date: %u/%u/%u\n", ucode->month,
48 ucode->day, ucode->year );
51 static ucode_validity nano_ucode_is_valid(const nano_ucode_header *ucode)
53 /* We must have a valid signature */
54 if(ucode->signature != NANO_UCODE_SIGNATURE)
55 return NANO_UCODE_SIGNATURE_ERROR;
56 /* The size of the head must be exactly 12 double words */
57 if( (ucode->total_size - ucode->payload_size) != NANO_UCODE_HEADER_SIZE)
58 return NANO_UCODE_WRONG_SIZE;
60 /* How about a checksum ? Checksum must be 0
61 * Two's complement done over the entire file, including the header */
62 int i;
63 u32 check = 0;
64 u32 *raw = (void*) ucode;
65 for(i = 0 ; i < ((ucode->total_size) >> 2); i++) {
66 check += raw[i];
68 if(check != 0)
69 return NANO_UCODE_CHECKSUM_FAIL;
70 /* Made it here huh? Then it looks valid to us.
71 * If there's anything else wrong, the CPU will reject the update */
72 return NANO_UCODE_VALID;
75 static void nano_print_ucode_status(ucode_update_status stat)
77 switch(stat)
79 case UCODE_UPDATE_SUCCESS:
80 printk(BIOS_INFO, "Microcode update successful.\n");
81 break;
82 case UCODE_UPDATE_FAIL:
83 printk(BIOS_ALERT, "Microcode update failed, bad environment."
84 "Update was not applied.\n");
85 break;
86 case UCODE_UPDATE_WRONG_CPU:
87 printk(BIOS_ALERT, "Update not applicable to this CPU.\n");
88 break;
89 case UCODE_INVALID_UPDATE_BLOCK:
90 printk(BIOS_ALERT, "Microcode block invalid."
91 "Update was not applied.\n");
92 break;
93 default:
94 printk(BIOS_ALERT, "Unknown status. No update applied.\n");
98 unsigned int nano_update_ucode(void)
100 size_t i;
101 unsigned int n_updates = 0;
102 u32 fms = cpuid_eax(0x1);
103 /* Considering we are running with eXecute-In-Place (XIP), there's no
104 * need to worry that accessing data from ROM will slow us down.
105 * Microcode data should be aligned to a 4-byte boundary, but CBFS
106 * already does that for us (Do you, CBFS?) */
107 u32 *ucode_data;
108 size_t ucode_len;
110 ucode_data = cbfs_boot_map_with_leak("cpu_microcode_blob.bin",
111 CBFS_TYPE_MICROCODE, &ucode_len);
112 /* Oops, did you forget to include the microcode ? */
113 if(ucode_data == NULL) {
114 printk(BIOS_ALERT, "WARNING: No microcode file found in CBFS. "
115 "Aborting microcode updates\n");
116 return 0;
119 /* We might do a lot of loops searching for the microcode updates, but
120 * keep in mind, nano_ucode_is_valid searches for the signature before
121 * doing anything else. */
122 for( i = 0; i < (ucode_len >> 2); /* don't increment i here */ )
124 ucode_update_status stat;
125 const nano_ucode_header * ucode = (void *)(&ucode_data[i]);
126 if(nano_ucode_is_valid(ucode) != NANO_UCODE_VALID) {
127 i++;
128 continue;
130 /* Since we have a valid microcode, there's no need to search
131 * in this region, so we restart our search at the end of this
132 * microcode */
133 i += (ucode->total_size >> 2);
134 /* Is the microcode compatible with our CPU? */
135 if(ucode->applicable_fms != fms) continue;
136 /* For our most curious users */
137 nano_print_ucode_info(ucode);
138 /* The meat of the pie */
139 stat = nano_apply_ucode(ucode);
140 /* The user might want to know how the update went */
141 nano_print_ucode_status(stat);
142 if(stat == UCODE_UPDATE_SUCCESS) n_updates++;
145 return n_updates;