/* * scripts/spidhdr/spidhdr.c * * (C) Copyright 2009 SPiDCOM Technologies * * This program is free software; you can redistribute it and/or * modify it under the terms of the GNU General Public License as * published by the Free Software Foundation; either version 2 of * the License, or (at your option) any later version. * * 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., 59 Temple Place, Suite 330, Boston, * MA 02111-1307 USA * * Author(s): * 30 Apr 2009 Drasko DRASKOVIC */ #include #include #include #include #include #include #include #include #include "spid_img_desc.h" spidcom_image_desc_300_t img_300 = { .magic = SPIDCOM_IMG_DESC_MAGIC, .index = 0xffffffff, /* mandatory to be changed into the flash */ .is_valid = 1, .is_1st_boot = 1, .is_not_success = 1, .is_not_update = 1, .type = 0, .arch = SPIDCOM_IMG_DESC_SPC300, .header = { .magic = SPIDCOM_IMG_DESC_MAGIC, .header_version = SPIDCOM_IMG_DESC_COMMON_HEADER_VERSION, }, }; spidcom_image_desc_200_t img_200 = { .magic = SPIDCOM_IMG_DESC_MAGIC, .index = 0xffffffff, /* mandatory to be changed into the flash */ .is_valid = 1, .is_1st_boot = 1, .is_not_success = 1, .type = 0, .arch = SPIDCOM_IMG_DESC_SPC200C, .header = { .magic = SPIDCOM_IMG_DESC_MAGIC, .header_version = SPIDCOM_IMG_DESC_COMMON_HEADER_VERSION, }, }; static void print_usage(const char *cmd) { printf("Usage: %s -i (200|300) [options]\n", cmd); printf("Generate a Spidcom header for a Linux image\n\n"); printf(" -i, --image=200|300\t set the image version (mandatory)\n" "\t 200 is used for a SPC200 or a MSE500-200\n" "\t 300 is used for a SPC300 or a MSE500-300\n"); printf(" -s, --size=S\t\t set size of the image to S bytes\n"); printf(" -v, --ver=V\t\t set version of the image to V\n"); printf(" -d, --desc=D\t\t set the description of the image to D\n"); printf(" -m, --md5=M\t\t set the MD5 sum of the image to M\n"); printf(" -r, --plc-ram=R\t set the amount of RAM given to PLC to R " "bytes\n"); printf(" -h, --help\t\t print this message and exit\n"); } static spidcom_image_desc_image_type_t string_to_image_type ( const char *img_str) { const char *imgs_str[SPIDCOM_IMG_DESC_IMAGE_TYPE_NB] = { "200", "300" }; int i; for(i = 0; i < SPIDCOM_IMG_DESC_IMAGE_TYPE_NB; i++) { if(strcmp(img_str, imgs_str[i]) == 0) { return (spidcom_image_desc_image_type_t) i; } } return SPIDCOM_IMG_DESC_IMAGE_TYPE_UNKNOWN; } int main(int argc, char **argv) { int c, i = 0; int j = 0; char tmpstr[3]; spidcom_image_desc_image_type_t image_type = SPIDCOM_IMG_DESC_IMAGE_TYPE_UNKNOWN; void *img_generic = NULL; int size_generic = -1; uint32_t size = 0; char version[64] = "v0.0"; char description[64] = "SPiDCOM image"; uint8_t md5_sum[16] = { '\0' }; uint32_t plc_ram = SPIDCOM_IMG_DESC_PLC_RAM; struct option opt[] = { { "size", required_argument, NULL, 's' }, { "ver", required_argument, NULL, 'v' }, { "desc", required_argument, NULL, 'd' }, { "md5", required_argument, NULL, 'm' }, { "plc-ram", required_argument, NULL, 'r' }, { "help", no_argument, NULL, 'h' }, { "image", required_argument, NULL, 'i' }, { 0, 0, 0, 0 } }; while((c = getopt_long(argc, argv, "s:v:d:m:r:i:h", opt, &i)) != -1) { switch(c) { case 's' : size = atoi(optarg); break; case 'v' : strncpy(version, optarg, sizeof(version)); break; case 'd' : strncpy(description, optarg, sizeof(description)); break; case 'm' : for (j=0; j<16; j++) { strncpy(tmpstr, optarg + j*2, 2); tmpstr[2] = '\0'; md5_sum[j] = (unsigned char)strtoul(tmpstr, NULL, 16); } break; case 'r' : plc_ram = atoi(optarg) * 1024 * 1024; break; case 'h' : print_usage(argv[0]); return 0; case 'i' : image_type = string_to_image_type(optarg); break; case '?' : /* getopt_long has already printed out an error, just exit. */ return -1; default: abort(); } } /* Fill image desc. */ switch (image_type) { case SPIDCOM_IMG_DESC_IMAGE_TYPE_200: img_200.size = size; strncpy(img_200.version, version, sizeof(img_200.version)); strncpy(img_200.description, description, sizeof(img_200.description)); strncpy((char *)img_200.md5_sum, (const char *)md5_sum, sizeof(img_200.md5_sum)); img_200.header.image_type = image_type; img_generic = &img_200; size_generic = sizeof(spidcom_image_desc_200_t); break; case SPIDCOM_IMG_DESC_IMAGE_TYPE_300: img_300.size = size; strncpy(img_300.version, version, sizeof(img_300.version)); strncpy(img_300.description, description, sizeof(img_300.description)); strncpy((char *)img_300.md5_sum, (const char *)md5_sum, sizeof(img_300.md5_sum)); img_300.plc_ram = plc_ram; img_300.header.image_type = image_type; img_generic = &img_300; size_generic = sizeof(spidcom_image_desc_300_t); break; default: fprintf(stderr, "You must specify an image type. See help (--help) " "for more information.\n"); return 2; } write(fileno(stdout), img_generic, size_generic); return 0; }