export.c 19 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708
  1. /* export.c: Routines for file exports
  2. Copyright (C) 2002-2004 Sebastian Reichelt
  3. Copyright (C) 2003-2005 Kevin Kofler
  4. Copyright (C) 2004 Billy Charvet
  5. This program is free software; you can redistribute it and/or modify
  6. it under the terms of the GNU General Public License as published by
  7. the Free Software Foundation; either version 2, or (at your option)
  8. any later version.
  9. This program is distributed in the hope that it will be useful,
  10. but WITHOUT ANY WARRANTY; without even the implied warranty of
  11. MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  12. GNU General Public License for more details.
  13. You should have received a copy of the GNU General Public License
  14. along with this program; if not, write to the Free Software Foundation,
  15. Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. */
  16. #include "export.h"
  17. #ifdef TIOS_SUPPORT
  18. #include "exp_tios.h"
  19. #endif /* TIOS_SUPPORT */
  20. #ifdef FLASH_OS_SUPPORT
  21. #include "exp_os.h"
  22. #endif /* FLASH_OS_SUPPORT */
  23. #ifdef NOSTUB_DLL_SUPPORT
  24. #include "exp_ndll.h"
  25. #endif /* NOSTUB_DLL_SUPPORT */
  26. #ifdef FARGO_SUPPORT
  27. #include "exp_farg.h"
  28. #endif /* FARGO_SUPPORT */
  29. #ifdef DATA_VAR_SUPPORT
  30. #include "exp_data.h"
  31. #endif /* DATA_VAR_SUPPORT */
  32. #ifdef DEBUGGING_INFO_SUPPORT
  33. #include "exp_dbg.h"
  34. #endif /* DEBUGGING_INFO_SUPPORT */
  35. #include "../formats/tios.h"
  36. #include "../formats/tiosupgd.h"
  37. #include "../special.h"
  38. #include <string.h>
  39. typedef struct {
  40. OUTPUT_FILE_FUNCTION GetOutputFile;
  41. OUTPUT_FILE_FINALIZE_FUNCTION FinalizeOutputFile;
  42. GET_FILE_SIZE_FUNCTION GetFileSize;
  43. EXPORT_FILE_FUNCTION ExportFile, ExportDataFile;
  44. FileFormats FileFormat, DataFileFormat;
  45. unsigned int FileType, DataFileType;
  46. const char *Extension;
  47. } EXPORT_STRUCT;
  48. // Return the maximum file size allowed for a given format
  49. // (without header and footer).
  50. static SIZE GetMaxFileSize (FileFormats FileFormat, ProgramCalcs Calc)
  51. {
  52. switch (FileFormat)
  53. {
  54. case FF_TIOS:
  55. return MAX_TIOS_FILE_SIZE;
  56. case FF_TIOS_UPGRADE:
  57. if (Calc == CALC_V200 || (Calc == (CALC_TI89 | CALC_FLAG_TITANIUM)))
  58. return MAX_TIOS_UPGRADE_FILE_SIZE_NEW;
  59. else
  60. return MAX_TIOS_UPGRADE_FILE_SIZE_OLD;
  61. default:
  62. return 0;
  63. }
  64. }
  65. // Export the internal data structures to an external file for the given
  66. // calculator.
  67. static BOOLEAN ExportProgramToFormat (const PROGRAM *Program, const EXPORT_STRUCT *ExportStruct, ProgramCalcs Calc)
  68. {
  69. BOOLEAN Success;
  70. EXP_FILE File;
  71. SIZE Size, MaxSize;
  72. I4 EffectiveSize = 0;
  73. // Fill the output file struct with zeroes.
  74. memset (&File, 0, sizeof (File));
  75. // Get the file size needed to export the file.
  76. Size = ExportStruct->GetFileSize (Program);
  77. // A size of 0 indicates an error.
  78. if (Size <= 0)
  79. return FALSE;
  80. // Check for maximum file size.
  81. MaxSize = GetMaxFileSize (ExportStruct->FileFormat, Calc);
  82. if (Size > MaxSize)
  83. {
  84. Error (NULL, "Program size of %ld bytes exceeds maximum by %ld.", (long) Size, (long) (Size - MaxSize));
  85. return FALSE;
  86. }
  87. // Create an output file with the specified format.
  88. if (!(ExportStruct->GetOutputFile (&(File.File), Size, Calc, FR_MAIN, ExportStruct->FileFormat, ExportStruct->FileType, ExportStruct->Extension, !(Program->Library), &EffectiveSize)))
  89. return FALSE;
  90. // Export the program into the output file.
  91. Success = ExportStruct->ExportFile (Program, &File, Size, Calc);
  92. // Finalize and close the output file.
  93. if (ExportStruct->FinalizeOutputFile)
  94. ExportStruct->FinalizeOutputFile (&(File.File));
  95. if (Success)
  96. {
  97. if (Program->OptimizeInfo->ProgramSize < (SI4) EffectiveSize)
  98. Program->OptimizeInfo->ProgramSize = (SI4) EffectiveSize;
  99. }
  100. else
  101. return FALSE;
  102. #ifdef DATA_VAR_SUPPORT
  103. // Write the data file, if any.
  104. if (Program->DataVarInfo->Name && Program->DataSection)
  105. {
  106. if (ExportStruct->DataFileFormat == FF_NONE)
  107. {
  108. Error (NULL, "Data variable is not supported in this file format.");
  109. return FALSE;
  110. }
  111. // Fill the output file struct with zeroes.
  112. memset (&File, 0, sizeof (File));
  113. // Get the file size needed to export the file.
  114. Size = GetDataFileSize (Program);
  115. // A size of 0 indicates an error.
  116. if (Size <= 0)
  117. return FALSE;
  118. MaxSize = GetMaxFileSize (ExportStruct->DataFileFormat, Calc);
  119. // Check for maximum file size.
  120. if (Size > MaxSize)
  121. {
  122. Error (NULL, "Data variable size of %ld bytes exceeds maximum by %ld.", (long) Size, (long) (MaxSize - Size));
  123. return FALSE;
  124. }
  125. // Create an output file with the specified format.
  126. if (!(ExportStruct->GetOutputFile (&(File.File), Size, Calc, FR_DATA, ExportStruct->DataFileFormat, ExportStruct->DataFileType, "DAT", FALSE, &EffectiveSize)))
  127. return FALSE;
  128. // Export the program into the output file.
  129. Success = ExportStruct->ExportDataFile (Program, &File, Size, Calc);
  130. // Finalize and close the output file.
  131. if (ExportStruct->FinalizeOutputFile)
  132. ExportStruct->FinalizeOutputFile (&(File.File));
  133. if (Success)
  134. {
  135. if (Program->OptimizeInfo->DataSize < (SI4) EffectiveSize)
  136. Program->OptimizeInfo->DataSize = (SI4) EffectiveSize;
  137. }
  138. else
  139. return FALSE;
  140. }
  141. #endif /* DATA_VAR_SUPPORT */
  142. return TRUE;
  143. }
  144. #ifdef PUCRUNCH_SUPPORT
  145. // Specifies whether to pack the main file with pucrunch.
  146. BOOLEAN Pack = FALSE;
  147. #endif /* PUCRUNCH_SUPPORT */
  148. // Export the internal data structures to external files, creating as many
  149. // files as needed.
  150. BOOLEAN ExportProgram (const PROGRAM *Program, OUTPUT_FILE_FUNCTION GetOutputFile, OUTPUT_FILE_FINALIZE_FUNCTION FinalizeOutputFile)
  151. {
  152. EXPORT_STRUCT ExportStruct = {GetOutputFile, FinalizeOutputFile, NULL, NULL, NULL, FF_TIOS, FF_TIOS, TIOS_TAG_ASM, TIOS_TAG_OTH, NULL};
  153. if (!GetOutputFile)
  154. return FALSE;
  155. #ifdef DEBUGGING_INFO_SUPPORT
  156. // If we have debugging information to export, do it.
  157. if (Program->HaveDebuggingInfo)
  158. {
  159. if (!ExportDebuggingInfo (Program, GetOutputFile, FinalizeOutputFile))
  160. return FALSE;
  161. }
  162. #endif
  163. switch (Program->Type)
  164. {
  165. #ifdef FLASH_OS_SUPPORT
  166. case PT_FLASH_OS:
  167. ExportStruct.GetFileSize = GetFlashOSFileSize;
  168. ExportStruct.ExportFile = ExportFlashOSFile;
  169. ExportStruct.FileFormat = FF_TIOS_UPGRADE;
  170. ExportStruct.FileType = 0; // Different types for Flash OSs do not exist.
  171. ExportStruct.DataFileFormat = FF_NONE;
  172. ExportStruct.DataFileType = 0;
  173. break;
  174. #endif /* FLASH_OS_SUPPORT */
  175. #ifdef NOSTUB_DLL_SUPPORT
  176. case PT_NOSTUB_DLL:
  177. ExportStruct.GetFileSize = GetNostubDLLFileSize;
  178. ExportStruct.ExportFile = ExportNostubDLLFile;
  179. ExportStruct.FileType = TIOS_TAG_OTH;
  180. ExportStruct.Extension = "DLL";
  181. break;
  182. #endif /* NOSTUB_DLL_SUPPORT */
  183. #ifdef FARGO_SUPPORT
  184. case PT_FARGO:
  185. ExportStruct.GetFileSize = GetFargoFileSize;
  186. ExportStruct.ExportFile = ExportFargoFile;
  187. ExportStruct.FileType = TIOS_TAG_PRGM;
  188. break;
  189. #endif /* FARGO_SUPPORT */
  190. default:
  191. #ifdef TIOS_SUPPORT
  192. ExportStruct.GetFileSize = GetTIOSFileSize;
  193. ExportStruct.ExportFile = ExportTIOSFile;
  194. #ifdef PUCRUNCH_SUPPORT
  195. if (Pack)
  196. {
  197. ExportStruct.ExportFile = ExportPackedTIOSFile;
  198. ExportStruct.FileType = TIOS_TAG_OTH;
  199. ExportStruct.Extension = "ppg";
  200. }
  201. #endif /* !PUCRUNCH_SUPPORT */
  202. #else /* !TIOS_SUPPORT */
  203. Error (NULL, "No default export target for this program type.");
  204. return FALSE;
  205. #endif /* !TIOS_SUPPORT */
  206. }
  207. #ifdef DATA_VAR_SUPPORT
  208. ExportStruct.ExportDataFile = ExportDataFile;
  209. #endif /* DATA_VAR_SUPPORT */
  210. {
  211. ProgramCalcs CurCalc;
  212. // For all calculators...
  213. for (CurCalc = 1; CurCalc <= HIGHEST_CALC; CurCalc <<= 1)
  214. {
  215. if (Program->Calcs & CurCalc)
  216. {
  217. if (!(ExportProgramToFormat (Program, &ExportStruct, CurCalc)))
  218. return FALSE;
  219. if (Program->Type == PT_FLASH_OS && CurCalc == CALC_TI89 && (Program->Calcs & CALC_FLAG_TITANIUM))
  220. {
  221. if (!(ExportProgramToFormat (Program, &ExportStruct, CurCalc | CALC_FLAG_TITANIUM)))
  222. return FALSE;
  223. }
  224. }
  225. }
  226. }
  227. Program->OptimizeInfo->RelocCount += Program->OptimizeInfo->NativeRelocCount;
  228. return TRUE;
  229. }
  230. // Non-embedded file output functions as defined in intrface.h.
  231. #ifndef TARGET_EMBEDDED
  232. #include <stdio.h>
  233. // Destination file name without extension.
  234. const char *DestFile = NULL;
  235. SIZE DestFileSize = 0;
  236. // Folder and variable name for main file.
  237. char ProgramFolder[MAX_NAME_LEN+1] = "main", ProgramName[MAX_NAME_LEN+1] = "program";
  238. #ifdef DATA_VAR_SUPPORT
  239. // Folder and variable name for data file.
  240. char DataFolder[MAX_NAME_LEN+1] = "", DataName[MAX_NAME_LEN+1] = "data";
  241. #endif /* DATA_VAR_SUPPORT */
  242. // Specifies whether to output only the binary image of the program.
  243. BOOLEAN OutputBin = FALSE;
  244. // Specifies whether to output only the binary image of the main file.
  245. BOOLEAN OutputBinMainOnly = FALSE;
  246. // Maximum length of a file extension.
  247. #define MAX_FILE_EXT_LEN 3
  248. BOOLEAN GetOutputFile (INT_EXP_FILE *File, SIZE FileSize, unsigned int DestCalc, unsigned int FileRole, unsigned int FileFormat, unsigned int FileType, const char *Extension, BOOLEAN Executable ATTRIBUTE_UNUSED, I4 *EffectiveSize)
  249. {
  250. // Keep some of the parameters for finalization.
  251. File->OutputBin = OutputBin || (OutputBinMainOnly && FileRole == FR_MAIN);
  252. File->FileFormat = FileFormat;
  253. File->FileType = FileType;
  254. File->Extension = Extension;
  255. // Determine the destination file format and act accordingly.
  256. switch (FileFormat)
  257. {
  258. #ifdef TIOS_FILE_SUPPORT
  259. case FF_TIOS:
  260. {
  261. const char *FolderName, *VarName;
  262. const char *FileExtCalc, *FileExtType;
  263. unsigned int LinkType;
  264. // Calculate the size needed for the two size bytes and the
  265. // tag.
  266. SIZE VarFileSize = 2 + FileSize + 1;
  267. // If the file has an extension, add the size needed for it.
  268. #ifdef PUCRUNCH_SUPPORT
  269. if (!Pack || FileRole != FR_MAIN)
  270. #endif /* !PUCRUNCH_SUPPORT */
  271. if (Extension)
  272. VarFileSize += 1 + strlen (Extension) + 1;
  273. // The variable size is what the user will see.
  274. *EffectiveSize = VarFileSize;
  275. // Determine the role of the file and set the folder and
  276. // variable names according to the user settings.
  277. switch (FileRole)
  278. {
  279. case FR_MAIN:
  280. FolderName = ProgramFolder;
  281. VarName = ProgramName;
  282. break;
  283. #ifdef DATA_VAR_SUPPORT
  284. case FR_DATA:
  285. if (*DataFolder)
  286. FolderName = DataFolder;
  287. else
  288. FolderName = ProgramFolder;
  289. VarName = DataName;
  290. break;
  291. #endif /* DATA_VAR_SUPPORT */
  292. default:
  293. Error (NULL, "Unknown file role.");
  294. return FALSE;
  295. }
  296. // Determine the destination calculator and set part of the
  297. // file extension.
  298. switch (DestCalc)
  299. {
  300. case CALC_TI92:
  301. FileExtCalc = "92";
  302. break;
  303. case CALC_TI89:
  304. FileExtCalc = "89";
  305. break;
  306. case CALC_TI92PLUS:
  307. FileExtCalc = "9x";
  308. break;
  309. case CALC_V200:
  310. FileExtCalc = "v2";
  311. break;
  312. default:
  313. Error (NULL, "Unknown calculator.");
  314. return FALSE;
  315. }
  316. // Determine the file type and set the other part of the file
  317. // extension.
  318. switch (FileType)
  319. {
  320. case TIOS_TAG_STR:
  321. FileExtType = "s";
  322. LinkType = TIOS_LINK_TYPE_STR;
  323. break;
  324. case TIOS_TAG_PRGM:
  325. FileExtType = "p";
  326. LinkType = TIOS_LINK_TYPE_PRGM;
  327. break;
  328. case TIOS_TAG_ASM:
  329. FileExtType = "z";
  330. LinkType = TIOS_LINK_TYPE_ASM;
  331. break;
  332. case TIOS_TAG_OTH:
  333. FileExtType = "y";
  334. LinkType = TIOS_LINK_TYPE_OTH;
  335. break;
  336. default:
  337. Error (NULL, "Unknown file type.");
  338. return FALSE;
  339. }
  340. {
  341. SIZE FileNameSize = DestFileSize;
  342. // Create a temporary file name string.
  343. char CurOutputFileName[FileNameSize+5+1+MAX_FILE_EXT_LEN+1];
  344. strncpy (CurOutputFileName, DestFile, FileNameSize);
  345. #if defined(DATA_VAR_SUPPORT) && defined(PUCRUNCH_SUPPORT)
  346. if (Pack && FileRole == FR_DATA)
  347. {
  348. strncpy (CurOutputFileName + FileNameSize, "-data", 5);
  349. FileNameSize += 5;
  350. }
  351. #endif /* defined(DATA_VAR_SUPPORT) && defined(PUCRUNCH_SUPPORT) */
  352. // Insert the dot into the file name.
  353. CurOutputFileName [FileNameSize++] = '.';
  354. // Insert the extension. Reverse in binary mode.
  355. if (File->OutputBin)
  356. {
  357. strcpy (CurOutputFileName + FileNameSize, FileExtType);
  358. FileNameSize += strlen (FileExtType);
  359. strcpy (CurOutputFileName + FileNameSize, FileExtCalc);
  360. FileNameSize += strlen (FileExtCalc);
  361. }
  362. else
  363. {
  364. strcpy (CurOutputFileName + FileNameSize, FileExtCalc);
  365. FileNameSize += strlen (FileExtCalc);
  366. strcpy (CurOutputFileName + FileNameSize, FileExtType);
  367. FileNameSize += strlen (FileExtType);
  368. }
  369. // Zero-terminate the string.
  370. CurOutputFileName [FileNameSize] = 0;
  371. // Open a file with the specified name.
  372. if (!(File->File = fopen (CurOutputFileName, "wb")))
  373. {
  374. Error (CurOutputFileName, "Could not open file for writing.");
  375. return FALSE;
  376. }
  377. // Write the host file header, if desired.
  378. if (!File->OutputBin)
  379. {
  380. TIOS_HOST_FILE_HEADER Header;
  381. memset (&Header, 0, sizeof (Header));
  382. // Write the signature.
  383. switch (DestCalc)
  384. {
  385. case CALC_TI92:
  386. strncpy (Header.Signature, "**TI92**", 8);
  387. break;
  388. case CALC_TI89:
  389. strncpy (Header.Signature, "**TI89**", 8);
  390. break;
  391. case CALC_TI92PLUS:
  392. case CALC_V200:
  393. strncpy (Header.Signature, "**TI92P*", 8);
  394. break;
  395. default:
  396. Error (NULL, "Unknown calculator.");
  397. return FALSE;
  398. }
  399. // Write folder name.
  400. strncpy (Header.FolderName, FolderName, sizeof (Header.FolderName));
  401. // Write variable name.
  402. strncpy (Header.VarName, VarName, sizeof (Header.VarName));
  403. // Write link type.
  404. WriteHI1 (Header.LinkType, LinkType);
  405. // Write file size.
  406. WriteHI4 (Header.FileSize, VarFileSize + sizeof (TIOS_HOST_FILE_HEADER) + sizeof (TIOS_HOST_FILE_FOOTER));
  407. // Write reserved bytes.
  408. Header.Reserved1 [0] = 0x01;
  409. Header.Reserved2 [0] = 0x01;
  410. Header.Reserved2 [2] = 0x52;
  411. Header.Reserved4 [0] = 0xA5;
  412. Header.Reserved4 [1] = 0x5A;
  413. // Export the header.
  414. fwrite (&Header, sizeof (Header), 1, File->File);
  415. }
  416. // Write the on-calc file header.
  417. #ifdef PUCRUNCH_SUPPORT
  418. if (!Pack || FileRole != FR_MAIN)
  419. #endif /* !PUCRUNCH_SUPPORT */
  420. {
  421. TI2 EncVarFileSize;
  422. // Encode the embedded size into the target
  423. // endianness.
  424. WriteTI2 (EncVarFileSize, VarFileSize - 2);
  425. // Export the file size.
  426. fwrite (&EncVarFileSize, 2, 1, File->File);
  427. // Update the checksum, since this is still included
  428. // in the on-calc file image.
  429. File->CheckSum += ReadTI1 (EncVarFileSize.Hi) + ReadTI1 (EncVarFileSize.Lo);
  430. }
  431. return TRUE;
  432. }
  433. }
  434. break;
  435. #endif /* TIOS_FILE_SUPPORT */
  436. #ifdef TIOS_UPGRADE_FILE_SUPPORT
  437. case FF_TIOS_UPGRADE:
  438. *EffectiveSize = FileSize + sizeof (TIOS_UPGRADE_CALC_FOOTER);
  439. if (File->OutputBin)
  440. {
  441. const char *FileExtCalc;
  442. // Determine the destination calculator and set part of the
  443. // file extension.
  444. switch (DestCalc)
  445. {
  446. case CALC_TI92:
  447. FileExtCalc = "92";
  448. break;
  449. case CALC_TI89:
  450. FileExtCalc = "89";
  451. break;
  452. case CALC_TI89 | CALC_FLAG_TITANIUM:
  453. FileExtCalc = "89ti";
  454. break;
  455. case CALC_TI92PLUS:
  456. FileExtCalc = "9x";
  457. break;
  458. case CALC_V200:
  459. FileExtCalc = "v2";
  460. break;
  461. default:
  462. Error (NULL, "Unknown calculator.");
  463. return FALSE;
  464. }
  465. {
  466. SIZE FileNameSize = DestFileSize;
  467. // Create a temporary file name string.
  468. char CurOutputFileName[FileNameSize+1+4+1+sizeof("tib")];
  469. strncpy (CurOutputFileName, DestFile, FileNameSize);
  470. // Insert a dash into the file name.
  471. CurOutputFileName [FileNameSize++] = '-';
  472. // Insert the calculator model into the file name.
  473. strcpy (CurOutputFileName + FileNameSize, FileExtCalc);
  474. FileNameSize += strlen (FileExtCalc);
  475. // Insert the dot into the file name.
  476. CurOutputFileName [FileNameSize++] = '.';
  477. // Insert the .tib extension.
  478. strcpy (CurOutputFileName + FileNameSize, "tib");
  479. FileNameSize += sizeof ("tib") - 1;
  480. // Zero-terminate the string.
  481. CurOutputFileName [FileNameSize] = 0;
  482. // Open a file with the specified name.
  483. if (!(File->File = fopen (CurOutputFileName, "wb")))
  484. {
  485. Error (CurOutputFileName, "Could not open file for writing.");
  486. return FALSE;
  487. }
  488. return TRUE;
  489. }
  490. }
  491. else
  492. Error (NULL, "Support for `.??u' files is not implemented yet. Use `--outputbin'.");
  493. break;
  494. #endif /* TIOS_UPGRADE_FILE_SUPPORT */
  495. #ifdef DEBUGGING_INFO_SUPPORT
  496. case FF_GDB_COFF:
  497. *EffectiveSize = FileSize;
  498. {
  499. SIZE FileNameSize = DestFileSize+4;
  500. // Create a temporary file name string.
  501. char CurOutputFileName[FileNameSize+1];
  502. strncpy (CurOutputFileName, DestFile, DestFileSize);
  503. // Append ".dbg" to the file name.
  504. strcpy (CurOutputFileName + DestFileSize, ".dbg");
  505. // Zero-terminate the string.
  506. CurOutputFileName [FileNameSize] = 0;
  507. // Open a file with the specified name.
  508. if (!(File->File = fopen (CurOutputFileName, "wb")))
  509. {
  510. Error (CurOutputFileName, "Could not open file for writing.");
  511. return FALSE;
  512. }
  513. return TRUE;
  514. }
  515. break;
  516. #endif /* DEBUGGING_INFO_SUPPORT */
  517. default:
  518. Error (NULL, "Unrecognized output file format.");
  519. }
  520. return FALSE;
  521. }
  522. void FinalizeOutputFile (INT_EXP_FILE *File)
  523. {
  524. // Determine the destination file format and finish it.
  525. switch (File->FileFormat)
  526. {
  527. #ifdef TIOS_FILE_SUPPORT
  528. case FF_TIOS:
  529. {
  530. // Write the on-calc file footer.
  531. {
  532. TI1 EncFileTag;
  533. // Export the possible file extension.
  534. if (File->Extension)
  535. {
  536. // Export the leading 0 byte.
  537. WriteTI1 (EncFileTag, 0);
  538. fwrite (&EncFileTag, 1, 1, File->File);
  539. // Export the extension string and terminating 0
  540. // byte.
  541. fwrite (File->Extension, 1, strlen (File->Extension) + 1, File->File);
  542. // Update the checksum.
  543. while (*(File->Extension))
  544. File->CheckSum += (*(File->Extension++));
  545. }
  546. // Encode the file tag.
  547. WriteTI1 (EncFileTag, File->FileType);
  548. // Export the file tag.
  549. fwrite (&EncFileTag, 1, 1, File->File);
  550. File->CheckSum += (I1) (File->FileType);
  551. }
  552. // Write the host file footer, if desired.
  553. if (!File->OutputBin)
  554. {
  555. TIOS_HOST_FILE_FOOTER Footer;
  556. memset (&Footer, 0, sizeof (Footer));
  557. // Write the checksum.
  558. WriteHI2 (Footer.CheckSum, File->CheckSum);
  559. // Export the footer.
  560. fwrite (&Footer, sizeof (Footer), 1, File->File);
  561. }
  562. }
  563. break;
  564. #endif /* TIOS_FILE_SUPPORT */
  565. #ifdef TIOS_UPGRADE_FILE_SUPPORT
  566. case FF_TIOS_UPGRADE:
  567. {
  568. // Write the on-calc upgrade file footer.
  569. {
  570. TIOS_UPGRADE_CALC_FOOTER Footer;
  571. // Since we do not have TI's private key, we cannot
  572. // write a real checksum and signature.
  573. memset (&Footer, 0, sizeof (Footer));
  574. // Encode the fixed data we know.
  575. WriteTI2 (Footer.SignatureHeader, 0x020D);
  576. WriteTI1 (Footer.SignatureType, 0x40);
  577. // Export the footer.
  578. fwrite (&Footer, sizeof (Footer), 1, File->File);
  579. }
  580. }
  581. break;
  582. #endif /* TIOS_UPGRADE_FILE_SUPPORT */
  583. #ifdef DEBUGGING_INFO_SUPPORT
  584. case FF_GDB_COFF:
  585. // Do nothing.
  586. break;
  587. #endif /* DEBUGGING_INFO_SUPPORT */
  588. }
  589. // Close the file.
  590. fclose (File->File);
  591. }
  592. #endif /* !TARGET_EMBEDDED */