export.c 17 KB

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