This week only. Pastebin PRO Accounts Christmas Special! Don't miss out!Want more features on Pastebin? Sign Up, it's FREE!
Guest

Untitled

By: a guest on Jul 19th, 2012  |  syntax: None  |  size: 4.94 KB  |  views: 24  |  expires: Never
download  |  raw  |  embed  |  report abuse  |  print
Text below is selected. Please press Ctrl+C to copy to your clipboard. (⌘+C on Mac)
  1. """
  2. Quick and dirty python script to update coreboot's cpu list in powernow_acpi.c cpuentry entr[]
  3. Extracts P-states from AMD documentation, e.g. http://support.amd.com/us/Processor_TechDocs/30430.pdf
  4. For this script to work, you'll first have to save the cpu tables as csv text
  5. Some manual editing is probably required,
  6. I'd recommend using something like PDFEdit, http://pdfedit.cz and a text editor with column mode.
  7. Example csv format:
  8.  
  9. Parameter/OPN                    ;    ADA2800AEP4AP          ;     ADA3000AEP4AP      ;ADA3200AEP5AP
  10. Model Number                     ;           2800+           ;           3000+        ;       3200+
  11. (BrandID)                        ;            04h            ;             04h        ;        04h
  12. (CPUID)                          ;            00000F48h      ;             00000F48h  ;        00000F48h
  13. """
  14. import os
  15. import sys
  16. import string
  17. import re
  18.  
  19. if len(sys.argv) < 1:
  20.     print "Usage: python ", sys.argv[0], " inputfile.csv > outputfile"
  21.     sys.exit()
  22.  
  23.    
  24. inputFile = sys.argv[1]
  25. if not os.path.exists(inputFile):
  26.     print "Inputfile does not exist ", inputFile    
  27.     sys.exit()
  28.  
  29.    
  30. #fields to extract
  31. OPN = "Parameter/OPN"
  32. MODEL = "ModelNumber"
  33. BRANDID = "(BrandID)"
  34. CPUID = "(CPUID)"
  35. MAXFID = "FID/VIDStatusMaxFIDField"
  36. STARTFID = "FID/VIDStatusStartFIDField"
  37. TDP = "ThermalDesignPower"
  38. IPS = "IntermediateP-State"
  39. MPS = "MinP-State"
  40. VID = "VIDCode/VID_VDD"
  41.  
  42. #how many columns to skip
  43. SKIPCOLS = 1
  44. #csv field separator
  45. SEPARATOR = ";"
  46.  
  47. file = open(inputFile, 'r')
  48.  
  49. #dictionary format:
  50. #[{'model':'', 'brandid':'', 'cpuid':'', 'maxfid':'', 'startfid':'', 'tdp':'', 'pstates':[{'pstate':'', 'vid':'', 'tdp':''}]}]
  51. cpus = []
  52. tmp = []
  53.  
  54. def parseValue(columns, regex, key):
  55.     global tmp
  56.     global cpus
  57.     if key == OPN:
  58.         cpus.extend(tmp)
  59.         tmp = []
  60.         for i in range(1, len(columns)):
  61.             val = re.sub(regex, "", columns[i]).strip()
  62.             if not val:
  63.                 continue
  64.             cpu = {}
  65.             cpu[IPS] = []
  66.             cpu[key] = val
  67.             tmp.append(cpu)
  68.            
  69.     elif key == IPS or key == MPS:
  70.         for i in range(1, len(columns)):
  71.             val = re.sub(regex, "", columns[i]).strip()
  72.             if not val:
  73.                 continue
  74.             cpu = tmp[i-SKIPCOLS]
  75.             cpu[IPS].append({IPS: val})
  76.  
  77.     elif key == VID:
  78.         for i in range(1, len(columns)):
  79.             val = re.sub(regex, "", columns[i]).strip()
  80.             if re.match("^0+$", val):
  81.                 val = '0'  
  82.             else:
  83.                 val = re.sub("^0+", "", val)
  84.             if not val:
  85.                 continue
  86.             cpu = tmp[i-SKIPCOLS]
  87.             pstates = cpu[IPS]
  88.             count = len(pstates)
  89.             if count > 0:
  90.                 pstate = pstates[count - 1]
  91.                 pstate[key] = val
  92.  
  93.     elif key == TDP:
  94.         for i in range(1, len(columns)):
  95.             val = re.sub(regex, "", columns[i]).strip()
  96.             if not val:
  97.                 continue
  98.             cpu = tmp[i-SKIPCOLS]
  99.             pstates = cpu[IPS]
  100.             count = len(pstates)
  101.             if count > 0:
  102.                 pstate = pstates[count - 1]
  103.                 pstate[key] = val
  104.             else:
  105.                 cpu[key] = val
  106.  
  107.     else:
  108.         for i in range(1, len(columns)):
  109.             val = re.sub(regex, "", columns[i]).strip()
  110.             if re.match("^0+$", val):
  111.                 val = '0'  
  112.             else:
  113.                 val = re.sub("^0+", "", val)
  114.             if not val:
  115.                 continue
  116.             cpu = tmp[i-SKIPCOLS]
  117.             cpu[key] = val
  118.  
  119. while 1:
  120.     line = file.readline()
  121.     if not line:
  122.         break
  123.  
  124.     line = line.strip()
  125.     if not line:
  126.         continue
  127.        
  128.     columns = string.split(line,SEPARATOR)
  129.  
  130.     key = re.sub("\s|#.*", "", columns[0]).strip()
  131.     opts = {
  132.       OPN: lambda: parseValue(columns, "\s", OPN),
  133.       MODEL: lambda: parseValue(columns, "\D", MODEL),
  134.       BRANDID: lambda: parseValue(columns, "h", BRANDID),
  135.       CPUID: lambda: parseValue(columns, "h", CPUID),
  136.       MAXFID: lambda: parseValue(columns, "h", MAXFID),
  137.       STARTFID: lambda: parseValue(columns, "h", STARTFID),
  138.       TDP: lambda: parseValue(columns, "\D", TDP),
  139.       IPS: lambda: parseValue(columns, "\D", IPS),
  140.       VID: lambda: parseValue(columns, ".*h|\.|V", VID),
  141.       MPS: lambda: parseValue(columns, "\D", MPS)
  142.     }.get(key, lambda: None)()        
  143.            
  144.            
  145.            
  146. for i in cpus:
  147.     tmpl = "/* {0} */ \n{{{1}, 0x{2}, 0x{3}, 0x{4}, 0x{5}, {6}, \n {{{7}}}}},"
  148.     pstmpl = "{{{0}, {1}, {2}}}"
  149.     pstates = []
  150.     for j in i[IPS]:
  151.         pstates.append( pstmpl.format(j[IPS], j[VID], j[TDP]) )
  152.    
  153.     print tmpl.format(i[OPN], i[MODEL], string.lower(i[BRANDID]), string.lower(i[CPUID]), string.lower(i[MAXFID]), string.lower(i[STARTFID]), i[TDP], string.join(pstates, ", "))
  154.  
  155. file.close()
clone this paste RAW Paste Data