Ejemplo n.º 1
0
Archivo: upgrade.c Proyecto: jhbsz/LC4
int
import_config(char *url, webs_t stream, int *total)
{
    char upload_file[] = "/tmp/ezp_cfg_XXXXXX";
    char decoded_file[] = "/tmp/decoded_cfg.cfg";
    FILE *fifo = NULL;
    FILE *tmp_fifo = NULL;
    char *buf = NULL;
    int c;
    int count; 
    int ret = 0;
    long flags = -1;
    int size = BUFSIZ;

    mkstemp(upload_file) ;
    if (!(fifo = fopen(upload_file, "w"))) {
        ret = errno;
        goto err;
    }
#ifdef DEBUG
    printf("Random filename %s\n", upload_file);
#endif

    /* Set nonblock on the socket so we can timeout */
    if (!do_ssl) {
        if ((flags = fcntl(fileno(stream), F_GETFL)) < 0 ||
                fcntl(fileno(stream), F_SETFL, flags | O_NONBLOCK) < 0) {
            ret = errno;
            goto err;
        }
    }

    /*
     ** The buffer must be at least as big as what the stream file is
     ** using so that it can read all the data that has been buffered 
     ** in the stream file. Otherwise it would be out of sync with fn
     ** select specially at the end of the data stream in which case
     ** the select tells there is no more data available but there in 
     ** fact is data buffered in the stream file's buffer. Since no
     ** one has changed the default stream file's buffer size, let's
     ** use the constant BUFSIZ until someone changes it.
     **/

    if ((buf = malloc(size)) == NULL) {
        ret = ENOMEM;
        goto err;
    }

#ifdef DEBUG
    printf("Uploading the configuration file %s\n", upload_file);
#endif
    /* Upload the configuration file first. */
    while (total && *total) {
        if (do_ssl) {
            if (size > *total)
                size = *total;
            count = wfread(buf, 1, size, stream);
        } else {
            if (waitfor(fileno(stream), 30) <= 0) {
                cprintf("waitfor timeout 30 secs\n");
                break;
            }
            count = safe_fread(buf, 1, size, stream);
            if (!count && (ferror(stream) || feof(stream)))
                break;
        }

        safe_fwrite(buf, 1, count, fifo);
        *total -= count;
        printf("total:%d\n", *total);
#ifdef DEBUG
        printf(".");
#endif
    }
    fclose(fifo);
    fifo = NULL;

    /* cfg file needs to be decoded. */
    if (!(fifo = fopen(upload_file, "r"))) {
        ret = errno;
        goto err;
    }

    if (!(tmp_fifo = fopen(decoded_file, "w"))) {
        ret = errno;
        goto err;
    }

    /* Decoding procedure */
    /* Note: c must be an integer to compare with EOF. */
    while ((c = fgetc(fifo)) != EOF) {
        decode((char *)&c, 1);
        fputc(c, tmp_fifo);
    }

    fclose(fifo);
    fifo = NULL;
    fclose(tmp_fifo);
    tmp_fifo = NULL;

    if (!do_ssl) {
        /* Reset nonblock on the socket */
        if (fcntl(fileno(stream), F_SETFL, flags) < 0) {
            ret = errno;
            goto err;
        }
    }

   ret=nvram_import(decoded_file); 

#ifdef DEBUG
    printf("done\n");
#endif

err:
    if (buf)        { free(buf); }
    if (fifo)       { fclose(fifo); }
    if (tmp_fifo)   { fclose(tmp_fifo); }

#ifdef DEBUG
    if (ret) { perror(NULL); }
#endif
    return ret;
}
Ejemplo n.º 2
0
Archivo: upgrade.c Proyecto: jhbsz/LC4
int
do_upgrade(char *url, webs_t stream, int *total)
{
    char upload_file[] = "/tmp/ezp_firmware_XXXXXX";
    FILE *fifo = NULL;
    FILE *tmp_fifo = NULL;
    char *write_argv[4];
    pid_t pid;
    char *buf = NULL;
    int count; 
    int ret = 0;
    long flags = -1;
    int size = BUFSIZ;
    struct stat st;
    int upgrade_cmd_complete = 0;
#if defined(PLATFORM_LIS) || defined(PLATFORM_AXA)
    FILE *pFile;
    char result[24]={0};
    int percentage=0;
    if ((pFile = popen("ezp-i2c gauge percentage", "r"))) {
        fgets(result, sizeof(result), pFile);
        sscanf(result,"Battery: %d %%", &percentage);
    }
    // terminate
    pclose (pFile);
    if (percentage < 60) {
        ret=2;
        goto err;
    }
#endif
    system("/sbin/stop_services.sh");
    start_upgrade_indicator();
    /* FIRMWARE upgrade utility */
    write_argv[0] = "/usr/sbin/upgraded";
    write_argv[1] = upload_file;
    write_argv[2] = NULL;

    // system("/tmp/ezp-i2c gauge upgrade start");
    // system("/sbin/reserve_link.sh");

    mkstemp(upload_file) ;
    if (!(fifo = fopen(upload_file, "w"))) {
        ret = errno;
        goto err;
    }
#ifdef DEBUG
    printf("Random filename %s\n", upload_file);
#endif

    /* Set nonblock on the socket so we can timeout */
    if (!do_ssl) {
        if ((flags = fcntl(fileno(stream), F_GETFL)) < 0 ||
                fcntl(fileno(stream), F_SETFL, flags | O_NONBLOCK) < 0) {
            ret = errno;
            goto err;
        }
    }

    /*
     ** The buffer must be at least as big as what the stream file is
     ** using so that it can read all the data that has been buffered 
     ** in the stream file. Otherwise it would be out of sync with fn
     ** select specially at the end of the data stream in which case
     ** the select tells there is no more data available but there in 
     ** fact is data buffered in the stream file's buffer. Since no
     ** one has changed the default stream file's buffer size, let's
     ** use the constant BUFSIZ until someone changes it.
     **/

    if ((buf = malloc(size)) == NULL) {
        ret = ENOMEM;
        goto err;
    }

#ifdef DEBUG
    printf("Uploading the firmware %s\n", upload_file);
#endif
    /* Upload the file first. */
    while (total && *total) {
        if (do_ssl) {
            if (size > *total)
                size = *total;
            count = wfread(buf, 1, size, stream);
        } else {
            if (waitfor(fileno(stream), 30) <= 0) {
                cprintf("waitfor timeout 30 secs\n");
                ret = errno;
                goto err;
            }
            count = safe_fread(buf, 1, size, stream);
            if (!count && (ferror(stream) || feof(stream))) {
                ret = errno;
                goto err;
            }
        }

        safe_fwrite(buf, 1, count, fifo);
        *total -= count;
#ifdef DEBUG
        printf(".");
#endif
    }
    fclose(fifo);
    fifo = NULL;

    if (!do_ssl) {
        /* Reset nonblock on the socket */
        if (fcntl(fileno(stream), F_SETFL, flags) < 0) {
            ret = errno;
            goto err;
        }
    }
    /* Data transfer finished, bring down devices. */
    //system("/sbin/ifdown wan0");
    //system("/sbin/ifdown wan1");
    //system("/sbin/ifdown lan0");
    _eval(write_argv, NULL, 0, &pid) ;
    upgrade_cmd_complete = 1;
    while((stat("/tmp/fw_incorrect",&st) != 0) && (stat("/tmp/fw_correct",&st) != 0)){
        usleep(200000);
    }
    if(stat("/tmp/fw_incorrect", &st) == 0) ret = 1;
    else if(stat("/tmp/fw_correct", &st) == 0) ret = 0;
#ifdef DEBUG
    printf("done with ret:%d\n", ret);
#endif

err:
    if (buf) {
        free(buf);
    }
    if (fifo) {
        fclose(fifo);
    }
    if (tmp_fifo) {
        fclose(tmp_fifo);
    }
#ifdef DEBUG
    if (ret) {
        perror(NULL);
    }
#endif
    if(!upgrade_cmd_complete) {
        system("/tmp/ezp-i2c gauge upgrade finish"); 
    }
    /* ret value
     * 1 : upgrade fail
     * 2 : low power
     * other : upgrade success
     * */
    return ret;
}
Ejemplo n.º 3
0
int
// sys_upgrade(char *url, FILE *stream, int *total)
sys_upgrade(char *url, webs_t stream, int *total, int type)	// jimmy,
								// https,
								// 8/6/2003
{

#ifndef ANTI_FLASH
	char upload_fifo[] = "/tmp/uploadXXXXXX";
	FILE *fifo = NULL;
	char *write_argv[4];
	pid_t pid;
	char *buf = NULL;
	int count, ret = 0;
	long flags = -1;
	int size = BUFSIZ;
	int i = 0;

	{
		write_argv[0] = "write";
		write_argv[1] = upload_fifo;
		write_argv[2] = "linux";
		write_argv[3] = NULL;
	}

	// diag_led(DIAG, START_LED); // blink the diag led
	C_led(1);
#ifdef HAVE_HTTPS
	if (do_ssl)
		ACTION("ACT_WEBS_UPGRADE");
	else
#endif
		ACTION("ACT_WEB_UPGRADE");

	/*
	 * Feed write from a temporary FIFO 
	 */
	if (!mktemp(upload_fifo) || !(fifo = fopen(upload_fifo, "w"))) {
		if (!ret)
			ret = errno;
		goto err;
	}

	/*
	 * Set nonblock on the socket so we can timeout 
	 */

	/*
	 ** The buffer must be at least as big as what the stream file is
	 ** using so that it can read all the data that has been buffered
	 ** in the stream file. Otherwise it would be out of sync with fn
	 ** select specially at the end of the data stream in which case
	 ** the select tells there is no more data available but there in
	 ** fact is data buffered in the stream file's buffer. Since no
	 ** one has changed the default stream file's buffer size, let's
	 ** use the constant BUFSIZ until someone changes it.
	 **/

	if (size < MIN_BUF_SIZE)
		size = MIN_BUF_SIZE;
	if ((buf = safe_malloc(size)) == NULL) {
		ret = ENOMEM;
		goto err;
	}

	/*
	 * Pipe the rest to the FIFO 
	 */
	cprintf("Upgrading\n");
	// while (total && *total)
	{
		wfread(&buf[0], 1, 5, stream);
		*total -= 5;
		if (buf[0] != 'R' || buf[1] != 'B' || buf[2] != '5' || buf[3] != '0' || buf[4] != '0') {
			ret = -1;
			goto err;
		}
		int linuxsize;
		int fssize;

		wfread(&linuxsize, 1, 4, stream);
		wfread(&fssize, 1, 4, stream);
		*total -= 8;
		safe_fwrite(&linuxsize, 1, 4, fifo);
		safe_fwrite(&fssize, 1, 4, fifo);
		linuxsize += fssize;
		for (i = 0; i < linuxsize / MIN_BUF_SIZE; i++) {
			wfread(&buf[0], 1, MIN_BUF_SIZE, stream);
			fwrite(&buf[0], 1, MIN_BUF_SIZE, fifo);
		}

		wfread(&buf[0], 1, linuxsize % MIN_BUF_SIZE, stream);
		fwrite(&buf[0], 1, linuxsize % MIN_BUF_SIZE, fifo);
		*total -= linuxsize;

	}
	fclose(fifo);
	fifo = NULL;
	fifo = fopen(upload_fifo, "rb");
	unsigned long linuxsize;
	unsigned long fssize;

	linuxsize = 0;
	linuxsize += getc(fifo);
	linuxsize += getc(fifo) * 256;
	linuxsize += getc(fifo) * 256 * 256;
	linuxsize += getc(fifo) * 256 * 256 * 256;
	fssize = 0;
	fssize += getc(fifo);
	fssize += getc(fifo) * 256;
	fssize += getc(fifo) * 256 * 256;
	fssize += getc(fifo) * 256 * 256 * 256;
	fprintf(stderr, "Write Linux %d\n", linuxsize);
	FILE *out = fopen("/dev/cf/card0/part1", "wb");

	for (i = 0; i < linuxsize; i++)
		putc(getc(fifo), out);
	fclose(out);
	fprintf(stderr, "Write FileSys %d\n", fssize);
	out = fopen("/dev/cf/card0/part2", "wb");
	for (i = 0; i < fssize; i++)
		putc(getc(fifo), out);
	fclose(out);

	/*
	 * Wait for write to terminate 
	 */
	// waitpid (pid, &ret, 0);
	cprintf("done\n");
	ret = 0;
err:
	if (buf)
		free(buf);
	if (fifo)
		fclose(fifo);
	unlink(upload_fifo);

	// diag_led(DIAG, STOP_LED);
	// C_led (0);
	fprintf(stderr, "Idle\n");
	ACTION("ACT_IDLE");

	return ret;
#else
	return 0;
#endif
}