/* * buildtnco - helper for nLaunch * * Copyright (C) 2012-2013 nLaunch team * * This program is free software; you can redistribute it and/or modify * it under the terms of the GNU General Public License version 2, as * published by the Free Software Foundation. * * This program is distributed in the hope that it will be useful, * but WITHOUT ANY WARRANTY; without even the implied warranty of * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the * GNU General Public License for more details. * * You should have received a copy of the GNU General Public License * along with this program; if not, write to the Free Software Foundation, * Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA. */ #include static const unsigned char part1[] = { 0x54, 0x49, 0x2d, 0x4e, 0x73, 0x70, 0x69, 0x72, 0x65, 0x2e, 0x74, 0x6e, 0x63, 0x20, 0x31, 0x2e, 0x31, 0x2e, 0x39, 0x31, 0x37, 0x30, 0x20, 0x20, 0x20, 0x20, 0x20, 0x20, 0x20, 0x20, 0x30, 0x20, 0x30, 0x0a, 0x5f, 0x5f, 0x52, 0x45, 0x53, 0x5f, 0x5f, 0x20, 0x31, 0x2e, 0x31, 0x2e, 0x39, 0x31, 0x37, 0x30, 0x20, 0x30, 0x20, 0x20, 0x20, 0x20, 0x20, 0x20, 0x20, 0x20, 0x30, 0x0a, 0x0a, 0x1a, 0x50, 0x4b, 0x03, 0x04, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00 }; static const unsigned char part3[] = { 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x09, 0x00, 0x00, 0xea, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x70, 0x11, 0xb0, 0x02, 0x82, 0x11 }; int main(int argc, char * argv[]) { FILE * input = fopen("nlaunch.tns", "rb"); FILE * output1 = fopen("nlaunch.tno", "w+b"); FILE * output2 = fopen("nlaunch.tnc", "w+b"); long filesize; long padding; long i; if (input != NULL && output1 != NULL && output2 != NULL) { // Header fwrite(part1, sizeof(part1[0]), sizeof(part1)/sizeof(part1[0]), output1); fwrite(part1, sizeof(part1[0]), sizeof(part1)/sizeof(part1[0]), output2); // Retrieve the size of the payload. fseek(input, 0, SEEK_END); filesize = ftell(input); fseek(input, 0, SEEK_SET); // Second part: size bytes filesize += 0x1CA - 0x40 - 0x1E; padding = ((filesize + 0x100) & 0xFF00) - filesize; printf("Initial payload size is %ld (0x%lX), will add %ld (0x%lX) bytes of padding\n", filesize, filesize, padding, padding); fputc(0x00, output1); fputc(0x00, output2); fputc(((filesize + 0x100) & 0xFF00) >> 8, output1); fputc(((filesize + 0x100) & 0xFF00) >> 8, output2); filesize -= 0x1CA - 0x40 - 0x1E; // Smasher + padding fwrite(part3, sizeof(part3[0]), sizeof(part3)/sizeof(part3[0]), output1); fwrite(part3, sizeof(part3[0]), sizeof(part3)/sizeof(part3[0]), output2); for (i = 0; i < padding; i++) { fputc(0x00, output1); fputc(0x00, output2); } // Payload for (i = 0; i < filesize; i++) { int c = fgetc(input); fputc(c, output1); fputc(c, output2); } // Update leading metadata filesize = ftell(output1); fseek(output1, 23, SEEK_SET); fseek(output2, 23, SEEK_SET); fprintf(output1, "%8ld", filesize); fprintf(output2, "%8ld", filesize); fclose(input); fclose(output1); fclose(output2); } else { fprintf(stderr, "Couldn't open input file nlaunch.tns or output files nlaunch.tno/.tnc !\n"); } return 0; }