Ejemplo n.º 1
0
int fiasco_write_to_file(struct fiasco * fiasco, const char * file) {

    int fd = -1;
    int i;
    int device_count;
    uint32_t size;
    uint32_t length;
    uint16_t hash;
    uint8_t length8;
    char ** device_hwrevs_bufs;
    const char * str;
    const char * type;
    struct image_list * image_list;
    struct image * image;
    unsigned char buf[4096];

    if ( ! fiasco )
        return -1;

    printf("Generating Fiasco image %s...\n", file);

    if ( ! fiasco->first )
        FIASCO_WRITE_ERROR(file, fd, "Nothing to write");

    if ( fiasco->name && strlen(fiasco->name)+1 > UINT8_MAX )
        FIASCO_WRITE_ERROR(file, fd, "Fiasco name string is too long");

    if ( fiasco->swver && strlen(fiasco->swver)+1 > UINT8_MAX )
        FIASCO_WRITE_ERROR(file, fd, "SW version string is too long");

    if ( ! simulate ) {
        fd = open(file, O_RDWR|O_CREAT|O_TRUNC, 0644);
        if ( fd < 0 ) {
            ERROR_INFO("Cannot create file");
            return -1;
        }
    }

    printf("Writing Fiasco header...\n");

    WRITE_OR_FAIL(file, fd, "\xb4", 1); /* signature */

    if ( fiasco->name[0] )
        str = fiasco->name;
    else
        str = "OSSO UART+USB";

    length = 4 + strlen(str) + 3;
    if ( fiasco->swver[0] )
        length += strlen(fiasco->swver) + 3;
    length = htonl(length);
    WRITE_OR_FAIL(file, fd, &length, 4); /* FW header length */

    if ( fiasco->swver[0] )
        length = htonl(2);
    else
        length = htonl(1);
    WRITE_OR_FAIL(file, fd, &length, 4); /* FW header blocks count */

    /* Fiasco name */
    length8 = strlen(str)+1;
    WRITE_OR_FAIL(file, fd, "\xe8", 1);
    WRITE_OR_FAIL(file, fd, &length8, 1);
    WRITE_OR_FAIL(file, fd, str, length8);

    /* SW version */
    if ( fiasco->swver[0] ) {
        printf("Writing SW version: %s\n", fiasco->swver);
        length8 = strlen(fiasco->swver)+1;
        WRITE_OR_FAIL(file, fd, "\x31", 1);
        WRITE_OR_FAIL(file, fd, &length8, 1);
        WRITE_OR_FAIL(file, fd, fiasco->swver, length8);
    };

    printf("\n");

    image_list = fiasco->first;

    while ( image_list ) {

        image = image_list->image;

        if ( ! image )
            FIASCO_WRITE_ERROR(file, fd, "Empty image");

        printf("Writing image...\n");
        image_print_info(image);

        type = image_type_to_string(image->type);

        device_hwrevs_bufs = device_list_alloc_to_bufs(image->devices);

        device_count = 0;
        if ( device_hwrevs_bufs && device_hwrevs_bufs[0] )
            for ( ; device_hwrevs_bufs[device_count]; ++device_count );

        if ( ! type )
            FIASCO_WRITE_ERROR(file, fd, "Unknown image type");

        if ( image->version && strlen(image->version) > UINT8_MAX )
            FIASCO_WRITE_ERROR(file, fd, "Image version string is too long");

        if ( image->layout && strlen(image->layout) > UINT8_MAX )
            FIASCO_WRITE_ERROR(file, fd, "Image layout is too long");

        printf("Writing image header...\n");

        /* signature */
        WRITE_OR_FAIL(file, fd, "T", 1);

        /* number of subsections */
        length8 = device_count+1;
        if ( image->version )
            ++length8;
        if ( image->layout )
            ++length8;
        WRITE_OR_FAIL(file, fd, &length8, 1);

        /* unknown */
        WRITE_OR_FAIL(file, fd, "\x2e\x19\x01\x01\x00", 5);

        /* checksum */
        hash = htons(image->hash);
        WRITE_OR_FAIL(file, fd, &hash, 2);

        /* image type name */
        memset(buf, 0, 12);
        strncpy((char *)buf, type, 12);
        WRITE_OR_FAIL(file, fd, buf, 12);

        /* image size */
        size = htonl(image->size);
        WRITE_OR_FAIL(file, fd, &size, 4);

        /* unknown */
        WRITE_OR_FAIL(file, fd, "\x00\x00\x00\x00", 4);

        /* append version subsection */
        if ( image->version ) {
            WRITE_OR_FAIL(file, fd, "1", 1); /* 1 - version */
            length8 = strlen(image->version)+1;
            WRITE_OR_FAIL(file, fd, &length8, 1);
            WRITE_OR_FAIL(file, fd, image->version, length8);
        }

        /* append device & hwrevs subsection */
        for ( i = 0; i < device_count; ++i ) {
            WRITE_OR_FAIL(file, fd, "2", 1); /* 2 - device & hwrevs */
            WRITE_OR_FAIL(file, fd, &device_hwrevs_bufs[i][0], 1);
            WRITE_OR_FAIL(file, fd, device_hwrevs_bufs[i]+1, ((uint8_t *)(device_hwrevs_bufs[i]))[0]);
        }
        free(device_hwrevs_bufs);

        /* append layout subsection */
        if ( image->layout ) {
            length8 = strlen(image->layout);
            WRITE_OR_FAIL(file, fd, "3", 1); /* 3 - layout */
            WRITE_OR_FAIL(file, fd, &length8, 1);
            WRITE_OR_FAIL(file, fd, image->layout, length8);
        }

        /* dummy byte - end of all subsections */
        WRITE_OR_FAIL(file, fd, "\x00", 1);

        printf("Writing image data...\n");

        image_seek(image, 0);
        while ( 1 ) {
            size = image_read(image, buf, sizeof(buf));
            if ( size == 0 )
                break;
            WRITE_OR_FAIL(file, fd, buf, size);
        }

        image_list = image_list->next;

        if ( image_list )
            printf("\n");

    }

    close(fd);
    printf("\nDone\n\n");
    return 0;

}
Ejemplo n.º 2
0
int fiasco_unpack(struct fiasco * fiasco, const char * dir) {

    int fd = -1;
    char * name = NULL;
    char * layout_name = NULL;
    struct image * image;
    struct image_list * image_list;
    uint32_t size;
    char cwd[256];
    unsigned char buf[4096];

    if ( dir ) {

        memset(cwd, 0, sizeof(cwd));

        if ( ! getcwd(cwd, sizeof(cwd)) ) {
            ERROR_INFO("Cannot store current directory");
            return -1;
        }

        if ( chdir(dir) < 0 ) {
            ERROR_INFO("Cannot change current directory to %s", dir);
            return -1;
        }

    }

    fiasco_print_info(fiasco);

    image_list = fiasco->first;

    while ( image_list ) {

        image = image_list->image;

        name = image_name_alloc_from_values(image);
        if ( ! name )
            return -1;

        printf("\n");
        printf("Unpacking image...\n");
        image_print_info(image);

        if ( image->layout ) {

            layout_name = calloc(1, strlen(name) + strlen(".layout") + 1);
            if ( ! layout_name )
                ALLOC_ERROR_RETURN(-1);

            sprintf(layout_name, "%s.layout", name);

            printf("    Layout file: %s\n", layout_name);

        }

        printf("    Output file: %s\n", name);

        if ( ! simulate ) {
            fd = open(name, O_RDWR|O_CREAT|O_TRUNC, 0644);
            if ( fd < 0 ) {
                ERROR_INFO("Cannot create output file %s", name);
                return -1;
            }
        }

        free(name);

        image_seek(image, 0);
        while ( 1 ) {
            size = image_read(image, buf, sizeof(buf));
            if ( size == 0 )
                break;
            WRITE_OR_FAIL(name, fd, buf, size);
        }

        close(fd);

        if ( image->layout ) {

            if ( ! simulate ) {
                fd = open(layout_name, O_RDWR|O_CREAT|O_TRUNC, 0644);
                if ( fd < 0 ) {
                    ERROR_INFO("Cannot create layout file %s", layout_name);
                    return -1;
                }
            }

            free(layout_name);

            WRITE_OR_FAIL(layout_name, fd, image->layout, (int)strlen(image->layout));

            close(fd);

        }

        image_list = image_list->next;

    }

    if ( dir ) {
        if ( chdir(cwd) < 0 ) {
            ERROR_INFO("Cannot change current directory back to %s", cwd);
            return -1;
        }
    }

    printf("\nDone\n\n");
    return 0;

}
Ejemplo n.º 3
0
void menu_func (int value)
{
	// variables used in the switch statement
	char filename[MAX_LINE];

	switch (value)
	{
	case M_QUIT:  // enum #0
		exit(0);
		break;



	case M_HELP:  // enum #1
		menu_help();
		break;



	case M_FILE_OPEN:   // enum #2
		if (!quietMode)
			cerr << "Open file (string - no spaces) : ";
		cin  >> filename;
		checkStream(cin);
		image_load(filename);
		break;


	case M_FILE_SAVE:   // enum #3
		if (!quietMode)
			cerr << "Save as (string - no spaces) : ";
		cin  >> filename;
		checkStream(cin);
		image_save(filename);
		break;


	case M_FILE_INFO:  // enum #4
		image_print_info();
		break;


	case M_FILE_REVERT:  // enum #5
		image_revert();
		break;

	case M_VIEW_PIXEL_VALUE: // enum #31
		{
			if (!currentImage) 
			{
				cerr << "Sorry, no image is loaded." << endl;
				break;
			}
			if (!quietMode)
			{
				cerr << "Current image width and height: " << currentImage->getWidth()-1 << " " 
					<< currentImage->getHeight()-1 << endl;
			}
			int x=getInt("x value of pixel to view");
			int y=getInt("y value of pixel to view");
			if (x<0 || x>=currentImage->getWidth() || y<0 || y>=currentImage->getHeight())
			{
				cerr << "Invalid pixel location." << endl;
				break;
			}
			cerr << "R: " << currentImage->getPixel(x,y,RED);
			cerr << ", G: " << currentImage->getPixel(x,y,GREEN);
			cerr << ", B: " << currentImage->getPixel(x,y,BLUE) << endl;
			break;
		}

	default:
		process_func(value);
	}
	return;
}