Source code of Windows XP (NT5)
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

434 lines
13 KiB

  1. /*** asl.c - Main module of the ASL assembler
  2. *
  3. * This program compiles the ASL language into AML (p-code).
  4. *
  5. * Copyright (c) 1996 Microsoft Corporation
  6. * Author: Michael Tsang (MikeTs)
  7. * Created 07/23/96
  8. *
  9. * MODIFICATION HISTORY
  10. */
  11. #include "pch.h"
  12. /***EP main - main program
  13. *
  14. * ENTRY
  15. * icArg - command line arguments count
  16. * apszArg - command line arguments array
  17. *
  18. * EXIT-SUCCESS
  19. * program terminates with return code ASLERR_NONE
  20. * EXIT-FAILURE
  21. * program terminates with negative error code
  22. */
  23. int EXPORT main(int icArg, char **apszArg)
  24. {
  25. int rc = ASLERR_NONE;
  26. ParseProgInfo(apszArg[0], &ProgInfo);
  27. icArg--;
  28. apszArg++;
  29. if ((ParseSwitches(&icArg, &apszArg, ArgTypes, &ProgInfo) != ARGERR_NONE) ||
  30. (gpszTabSig == NULL) && ((icArg != 1) || (gdwfASL & ASLF_CREAT_BIN)) ||
  31. (gpszTabSig != NULL) && ((icArg != 0) || (gdwfASL & ASLF_UNASM)))
  32. {
  33. MSG(("invalid command line options"));
  34. PrintUsage();
  35. rc = ASLERR_INVALID_PARAM;
  36. }
  37. else
  38. {
  39. #ifdef __UNASM
  40. PBYTE pbTable = NULL;
  41. DWORD dwTableSig = 0;
  42. PSZ psz;
  43. #endif
  44. OPENTRACE(gpszTraceFile);
  45. PrintLogo();
  46. if ((rc = InitNameSpace()) == ASLERR_NONE)
  47. {
  48. #ifdef __UNASM
  49. #pragma message("Building with __UNASM option\r\n")
  50. if (gdwfASL & ASLF_DUMP_BIN)
  51. {
  52. if (((rc = ReadBinFile(*apszArg, &pbTable, &dwTableSig)) ==
  53. ASLERR_NONE) &&
  54. (gpszLSTFile == NULL))
  55. {
  56. strncpy(gszLSTName, (PSZ)&dwTableSig, sizeof(DWORD));
  57. strcpy(&gszLSTName[sizeof(DWORD)], ".TXT");
  58. gpszLSTFile = gszLSTName;
  59. gdwfASL |= ASLF_DUMP_NONASL | ASLF_UNASM;
  60. }
  61. }
  62. else if (gdwfASL & ASLF_UNASM)
  63. {
  64. gpszAMLFile = *apszArg;
  65. if (gpszLSTFile == NULL)
  66. {
  67. strncpy(gszLSTName, gpszAMLFile, _MAX_FNAME - 1);
  68. if ((psz = strchr(gszLSTName, '.')) != NULL)
  69. {
  70. *psz = '\0';
  71. }
  72. strcpy(&gszLSTName[strlen(gszLSTName)], ".ASL");
  73. gpszLSTFile = gszLSTName;
  74. gdwfASL |= ASLF_GENSRC;
  75. }
  76. }
  77. else if (gpszTabSig != NULL)
  78. {
  79. gdwfASL |= ASLF_UNASM;
  80. if (IsWinNT())
  81. {
  82. gdwfASL |= ASLF_NT;
  83. }
  84. _strupr(gpszTabSig);
  85. if ((strcmp(gpszTabSig, "DSDT") != 0) &&
  86. (strcmp(gpszTabSig, "SSDT") != 0) &&
  87. (strcmp(gpszTabSig, "PSDT") != 0))
  88. {
  89. gdwfASL |= ASLF_DUMP_NONASL;
  90. }
  91. if (gpszLSTFile == NULL)
  92. {
  93. gpszLSTFile = gszLSTName;
  94. if (gdwfASL & ASLF_DUMP_NONASL)
  95. {
  96. if (strcmp(gpszTabSig, "*") == 0)
  97. {
  98. strcpy(gszLSTName, "ACPI");
  99. }
  100. else
  101. {
  102. strcpy(gszLSTName, gpszTabSig);
  103. }
  104. strcpy(&gszLSTName[strlen(gszLSTName)], ".TXT");
  105. }
  106. else
  107. {
  108. strcpy(gszLSTName, gpszTabSig);
  109. strcpy(&gszLSTName[strlen(gszLSTName)], ".ASL");
  110. gdwfASL |= ASLF_GENSRC;
  111. }
  112. }
  113. #ifndef WINNT
  114. if (!(gdwfASL & ASLF_NT) && ((ghVxD = OpenVxD()) == NULL))
  115. {
  116. rc = ASLERR_OPEN_VXD;
  117. }
  118. #endif
  119. }
  120. else
  121. {
  122. #endif
  123. rc = ParseASLFile(*apszArg);
  124. #ifdef __UNASM
  125. }
  126. #endif
  127. }
  128. if (rc == ASLERR_NONE)
  129. {
  130. FILE *pfileOut;
  131. if (!(gdwfASL & ASLF_DUMP_NONASL))
  132. {
  133. if (gpszAMLFile == NULL)
  134. {
  135. if (gpszTabSig == NULL)
  136. {
  137. ERROR(("%s has no DefinitionBlock", *apszArg));
  138. }
  139. else
  140. {
  141. strcpy(gszAMLName, gpszTabSig);
  142. strcat(gszAMLName, ".AML");
  143. }
  144. }
  145. if (gpszASMFile != NULL)
  146. {
  147. if ((pfileOut = fopen(gpszASMFile, "w")) == NULL)
  148. {
  149. ERROR(("failed to open ASM file - %s", gpszASMFile));
  150. }
  151. else
  152. {
  153. gdwfASL |= ASLF_GENASM;
  154. UnAsmFile(gpszAMLFile? gpszAMLFile: gszAMLName,
  155. (PFNPRINT)fprintf, pfileOut);
  156. gdwfASL &= ~ASLF_GENASM;
  157. fclose(pfileOut);
  158. }
  159. }
  160. }
  161. if (gpszLSTFile != NULL)
  162. {
  163. #ifdef __UNASM
  164. if (gdwfASL & ASLF_CREAT_BIN)
  165. {
  166. rc = DumpTableBySig(NULL, *((PDWORD)gpszTabSig));
  167. }
  168. else if ((pfileOut = fopen(gpszLSTFile, "w")) == NULL)
  169. {
  170. ERROR(("failed to open LST file - %s", gpszLSTFile));
  171. }
  172. else
  173. {
  174. if (gdwfASL & ASLF_DUMP_BIN)
  175. {
  176. ASSERT(pbTable != NULL);
  177. ASSERT(dwTableSig != 0);
  178. rc = DumpTable(pfileOut, pbTable, 0, dwTableSig);
  179. MEMFREE(pbTable);
  180. }
  181. else if (gdwfASL & ASLF_DUMP_NONASL)
  182. {
  183. DWORD dwAddr;
  184. if (((dwAddr = strtoul(gpszTabSig, &psz, 16)) != 0) &&
  185. (*psz == 0))
  186. {
  187. rc = DumpTableByAddr(pfileOut, dwAddr);
  188. }
  189. else
  190. {
  191. rc = DumpTableBySig(pfileOut, *((PDWORD)gpszTabSig));
  192. }
  193. }
  194. else
  195. {
  196. rc = UnAsmFile(gpszAMLFile? gpszAMLFile: gszAMLName,
  197. (PFNPRINT)fprintf, pfileOut);
  198. }
  199. fclose(pfileOut);
  200. }
  201. #else
  202. if ((pfileOut = fopen(gpszLSTFile, "w")) != NULL)
  203. {
  204. rc = UnAsmFile(gpszAMLFile? gpszAMLFile: gszAMLName,
  205. (PFNPRINT)fprintf, pfileOut);
  206. fclose(pfileOut);
  207. }
  208. else
  209. {
  210. ERROR(("failed to open LST file - %s", gpszLSTFile));
  211. }
  212. #endif
  213. }
  214. if (gpszNSDFile != NULL)
  215. {
  216. if ((pfileOut = fopen(gpszNSDFile, "w")) == NULL)
  217. {
  218. ERROR(("failed to open NameSpace dump file - %s",
  219. gpszNSDFile));
  220. }
  221. else
  222. {
  223. fprintf(pfileOut, "Name Space Objects:\n");
  224. DumpNameSpacePaths(gpnsNameSpaceRoot, pfileOut);
  225. fclose(pfileOut);
  226. }
  227. }
  228. }
  229. #ifdef __UNASM
  230. #ifndef WINNT
  231. if (ghVxD != NULL)
  232. {
  233. CloseVxD(ghVxD);
  234. ghVxD = NULL;
  235. }
  236. #endif
  237. #endif
  238. CLOSETRACE();
  239. }
  240. return rc;
  241. } //main
  242. #ifdef __UNASM
  243. /***LP ReadBinFile - Read table from binary file
  244. *
  245. * ENTRY
  246. * pszFile -> binary file name
  247. * ppb -> to hold the binary buffer pointer
  248. * pdwTableSig -> to hold the table signature
  249. *
  250. * EXIT-SUCCESS
  251. * returns ASLERR_NONE
  252. * EXIT-FAILURE
  253. * returns negative error code
  254. */
  255. int LOCAL ReadBinFile(PSZ pszFile, PBYTE *ppb, PDWORD pdwTableSig)
  256. {
  257. int rc = ASLERR_NONE;
  258. FILE *pfileBin;
  259. DESCRIPTION_HEADER dh;
  260. ENTER((1, "ReadBinFile(File=%s,ppb=%p,pdwTableSig=%p)\n",
  261. pszFile, ppb, pdwTableSig));
  262. if ((pfileBin = fopen(pszFile, "rb")) == NULL)
  263. {
  264. ERROR(("ReadBinFile: failed to open file %s", pszFile));
  265. rc = ASLERR_OPEN_FILE;
  266. }
  267. else if (fread(&dh, 1, sizeof(dh), pfileBin) < 2*sizeof(DWORD))
  268. {
  269. ERROR(("ReadBinFile: failed to read file %s", pszFile));
  270. rc = ASLERR_READ_FILE;
  271. }
  272. else if (fseek(pfileBin, 0, SEEK_SET) != 0)
  273. {
  274. ERROR(("ReadBinFile: failed to reset file %s", pszFile));
  275. rc = ASLERR_SEEK_FILE;
  276. }
  277. else
  278. {
  279. DWORD dwLen = (dh.Signature == SIG_LOW_RSDP)? sizeof(RSDP): dh.Length;
  280. if ((*ppb = MEMALLOC(dwLen)) == NULL)
  281. {
  282. ERROR(("ReadBinFile: failed to allocate table buffer for %s",
  283. pszFile));
  284. rc = ASLERR_OUT_OF_MEM;
  285. }
  286. else if (fread(*ppb, dwLen, 1, pfileBin) != 1)
  287. {
  288. MEMFREE(*ppb);
  289. *ppb = NULL;
  290. ERROR(("ReadBinFile: failed to read file %s", pszFile));
  291. rc = ASLERR_READ_FILE;
  292. }
  293. else if (dh.Signature == SIG_LOW_RSDP)
  294. {
  295. *pdwTableSig = SIG_RSDP;
  296. }
  297. else
  298. {
  299. *pdwTableSig = dh.Signature;
  300. }
  301. }
  302. if (pfileBin != NULL)
  303. {
  304. fclose(pfileBin);
  305. }
  306. EXIT((1, "ReadBinFile=%d (pbTable=%p,TableSig=%s)\n",
  307. rc, *ppb, GetTableSigStr(*pdwTableSig)));
  308. return rc;
  309. } //ReadBinFile
  310. #endif
  311. /***LP PrintLogo - Print logo message
  312. *
  313. * ENTRY
  314. * None
  315. *
  316. * EXIT
  317. * None
  318. */
  319. VOID LOCAL PrintLogo(VOID)
  320. {
  321. if ((gdwfASL & ASLF_NOLOGO) == 0)
  322. {
  323. printf("%s Version %d.%d.%d%s [%s, %s]\n%s\n",
  324. STR_PROGDESC, VERSION_MAJOR, VERSION_MINOR, VERSION_RELEASE,
  325. #ifdef WINNT
  326. "NT",
  327. #else
  328. "",
  329. #endif
  330. __DATE__, __TIME__, STR_COPYRIGHT);
  331. printf("Compliant with ACPI 1.0 specification\n\n");
  332. }
  333. } //PrintLogo
  334. /***LP PrintHelp - Print help messages
  335. *
  336. * ENTRY
  337. * ppszArg -> pointer to argument (not used)
  338. * pAT -> argument type structure (not used)
  339. *
  340. * EXIT
  341. * program terminated with exit code 0
  342. */
  343. int LOCAL PrintHelp(char **ppszArg, PARGTYPE pAT)
  344. {
  345. DEREF(ppszArg);
  346. DEREF(pAT);
  347. PrintLogo();
  348. PrintUsage();
  349. printf("\t? - Print this help message.\n");
  350. printf("\tnologo - Supress logo banner.\n");
  351. printf("\tFo=<AMLFile> - Override the AML file name in the DefinitionBlock.\n");
  352. printf("\tFa=<ASMFile> - Generate .ASM file with the name <ASMFile>.\n");
  353. printf("\tFl=<LSTFile> - Generate .LST file with the name <LSTFile>.\n");
  354. printf("\tFn=<NSDFile> - Generate NameSpace Dump file with the name <NSDFile>.\n");
  355. #ifdef __UNASM
  356. printf("\td - Dump the binary file in text form.\n");
  357. printf("\tu - Unassemble AML file to an .ASL file (default)\n"
  358. "\t or a .LST file.\n");
  359. printf("\ttab=<TabSig> - Unassemble ASL table to an .ASL file (default)\n"
  360. "\t or a .LST file.\n");
  361. printf("\t Dump non ASL table to an .TXT file.\n");
  362. printf("\t If <TabSig> is '*', all tables are dump to ACPI.TXT.\n");
  363. printf("\t <TabSig> can also be the physical address of the table.\n");
  364. printf("\tc - Create binary files from tables.\n");
  365. #endif
  366. #ifdef TRACING
  367. printf("\tt=<n> - Enable tracing at trace level n.\n");
  368. printf("\tl=<LogFile> - Overriding the default trace log file name.\n");
  369. #endif
  370. exit(0);
  371. return ASLERR_NONE;
  372. } //PrintHelp
  373. /***LP PrintUsage - Print program usage syntax
  374. *
  375. * ENTRY
  376. * None
  377. *
  378. * EXIT
  379. * None
  380. */
  381. VOID LOCAL PrintUsage(VOID)
  382. {
  383. printf("Usage:\n%s /?\n", MODNAME);
  384. #ifdef __UNASM
  385. printf("%s [/nologo] /d <BinFile>\n", MODNAME);
  386. printf("%s [/nologo] /u [/Fa=<ASMFile>] [/Fl=<LSTFile>] [/Fn=<NSDFile>] <AMLFile>\n",
  387. MODNAME);
  388. printf("%s [/nologo] /tab=<TabSig> [/c] [/Fa=<ASMfile>] [/Fl=<LSTFile>] [/Fn=<NSDFile>]\n",
  389. MODNAME);
  390. #endif
  391. #ifdef TRACING
  392. printf("%s [/nologo] [/Fo=<AMLFile>] [/Fa=<ASMFile>] [/Fl=<LSTFile>] [/Fn=<NSDFile>] [/t=<n>] [/l=<LogFile>] <ASLFile>\n",
  393. MODNAME);
  394. #else
  395. printf("%s [/nologo] [/Fo=<AMLFile>] [/Fa=<ASMFile>] [/Fl=<LSTFile>] [/Fn=<NSDFile>] <ASLFile>\n",
  396. MODNAME);
  397. #endif
  398. printf("\n");
  399. } //PrintUsage