/**
 *      rtnet_mgr_init():       initialize the RTnet
 *
 */
int rtnet_init(void)
{
        int err = 0;

	printk("\n*** RTnet - built on %s, %s ***\n\n", __DATE__, __TIME__);
        printk("RTnet: init real-time networking\n");
        init_crc32();

        if ( (err=rtskb_pool_init()) )
                return err;

        rtsockets_init();
        rtnet_dev_init();
        rt_inet_proto_init();
        rtnet_chrdev_init();

#ifdef CONFIG_PROC_FS
        err = rtnet_mgr_proc_register ();
#endif

        /* initialise the Stack-Manager */
        if ( (err=rt_stack_mgr_init(&STACK_manager)) )
		return err;

        /* initialise the RTDEV-Manager */
        if ( (err=rt_rtdev_mgr_init(&RTDEV_manager)) )
		return err;

        return 0;
}
/**
 *  rtnet_init()
 */
int __init rtnet_init(void)
{
    int err = 0;


    printk("\n*** RTnet " RTNET_VERSION " - built on %s, %s ***\n\n",
           __DATE__, __TIME__);
    printk("RTnet: initialising real-time networking\n");

    if ((err = init_crc32()) != 0)
        goto err_out1;

    if ((err = rtskb_pools_init()) != 0)
        goto err_out2;

    /* initialize the Stack-Manager */
    if ((err=rt_stack_mgr_init(&STACK_manager)) != 0)
        goto err_out2;

    /* initialize the RTDEV-Manager */
    if ((err=rt_rtdev_mgr_init(&RTDEV_manager)) != 0)
        goto err_out3;

    rtsockets_init();
    rt_inet_proto_init();
    rtnet_chrdev_init();

    if ((err = rtpc_init()) != 0)
        goto err_out4;

#ifdef CONFIG_PROC_FS
    if ((err = rtnet_proc_register()) != 0)
        goto err_out5;
#endif

    return 0;


err_out5:
    rtpc_cleanup();

err_out4:
    rtnet_chrdev_release();
    rt_inet_proto_release();

    rt_rtdev_mgr_delete(&RTDEV_manager);

err_out3:
    rt_stack_mgr_delete(&STACK_manager);

err_out2:
    rtskb_pools_release();

err_out1:
    cleanup_crc32();

    return err;
}
Exemple #3
0
/**
 * load the image description from the image file
 *
 * note: img_head is meaning full only when img_opt.image_version is >= 2.
 */
void load_image_desc(int* ret, cmd_opt* opt, image_head_v2* img_head, file_system_info* fs_info, image_options* img_opt) {

	image_desc_v2 buf_v2;
	int r_size;
	int debug = opt->debug;
	int img_version;

	r_size = read_all(ret, (char*)&buf_v2, sizeof(buf_v2), opt);
	if (r_size != sizeof(buf_v2))
		log_mesg(0, 1, 1, debug, "read image_hdr error=%d\n", r_size);

	/// check the image magic
	if (memcmp(buf_v2.head.magic, IMAGE_MAGIC, IMAGE_MAGIC_SIZE))
		log_mesg(0, 1, 1, debug, "This is not partclone image.\n");

	img_version = atol(buf_v2.head.version);

	switch(img_version) {

	case 0x0001: {
		const image_desc_v1* buf_v1 = (image_desc_v1*)&buf_v2;
		image_options_v1 extra;

		// read the extra bytes
		if (read_all(ret, extra.buff, sizeof(image_desc_v1) - sizeof(image_desc_v2), opt) == -1)
			log_mesg(0, 1, 1, debug, "read image_hdr error=%d\n (%s)", r_size, strerror(errno));

		load_image_desc_v1(fs_info, img_opt, buf_v1->head, buf_v1->fs_info, opt);
		memset(img_head, 0, sizeof(image_head_v2));
		break;
	}

	case 0x0002: {
		uint32_t crc;

		// Verify checksum
		init_crc32(&crc);
		crc = crc32(crc, &buf_v2, sizeof(buf_v2) - CRC32_SIZE);
		if (crc != buf_v2.crc)
			log_mesg(0, 1, 1, debug, "Invalid header checksum [0x%08X != 0x%08X]\n", crc, buf_v2.crc);

		load_image_desc_v2(fs_info, img_opt, buf_v2.head, buf_v2.fs_info, buf_v2.options, opt);
		memcpy(img_head, &(buf_v2.head), sizeof(image_head_v2));
		break;
	}

	default: {

		char version[IMAGE_VERSION_SIZE+1] = { 0x00 };

		memcpy(version, buf_v2.head.version, IMAGE_VERSION_SIZE);
		log_mesg(0, 1, 1, debug, "The image version is not supported [%s]\n", version);
		break;
	}

	} //switch
}
/**
 *      rtnet_mgr_init():       initialize the RTnet
 *
 */
int rtnet_init(void)
{
        int err = 0;

	printk("\n*** RTnet - %s, %s ***\n\n", __DATE__, __TIME__);
        printk("RTnet: init real-time networking\n");
        init_crc32();

        if ( (err=rtskb_pool_init()) )
                return err;

        rtsockets_init();
        rtnet_dev_init();
        rt_inet_proto_init();
        rtnet_chrdev_init();

#ifdef CONFIG_PROC_FS
        err = rtnet_mgr_proc_register ();
#endif

        /* initialise the Stack-Manager */
        if ( (err=rt_stack_mgr_init(&STACK_manager)) )
		goto proc_mgr_err;

        /* initialise the RTDEV-Manager */
        if ( (err=rt_rtdev_mgr_init(&RTDEV_manager)) )
		goto  stack_mgr_err;

        /* initialise the RT-Wakeup-Manager */
        if ( (err=rt_wakeup_mgr_init(&rt_wakeup_manager)) )
		goto rtdev_mgr_err;

        /* initialise the Proxy-Wakeup-Manager */
        if ( (err=proxy_wakeup_mgr_init(&proxy_wakeup_manager)) )
		goto wakeup_mgr_err;

        return 0;

 wakeup_mgr_err:
	rt_wakeup_mgr_delete(&rt_wakeup_manager);
 rtdev_mgr_err:
	rt_rtdev_mgr_delete(&RTDEV_manager);
 stack_mgr_err:
	rt_stack_mgr_delete(&STACK_manager);
 proc_mgr_err:
#ifdef CONFIG_PROC_FS
        rtnet_mgr_proc_unregister ();
#endif
	return err;
}
Exemple #5
0
void write_image_desc(int* ret, file_system_info fs_info, image_options img_opt, cmd_opt* opt) {

	image_desc_v2 buf_v2;

	init_image_head_v2(&buf_v2.head);

	memcpy(&buf_v2.fs_info, &fs_info, sizeof(file_system_info));
	memcpy(&buf_v2.options, &img_opt, sizeof(image_options));

	init_crc32(&buf_v2.crc);
	buf_v2.crc = crc32(buf_v2.crc, &buf_v2, sizeof(image_desc_v2) - CRC32_SIZE);

	if (write_all(ret, (char*)&buf_v2, sizeof(image_desc_v2), opt) != sizeof(image_desc_v2))
		log_mesg(0, 1, 1, opt->debug, "error writing image header to image: %s\n", strerror(errno));
}
Exemple #6
0
void load_image_bitmap_bits(int* ret, cmd_opt opt, file_system_info fs_info, unsigned long* bitmap) {

	unsigned long long r_size, bitmap_size = BITS_TO_BYTES(fs_info.totalblock);
	uint32_t r_crc, crc;

	r_size = read_all(ret, (char*)bitmap, bitmap_size, &opt);
	if (r_size != bitmap_size)
		log_mesg(0, 1, 1, opt.debug, "unable to read bitmap.\n");

	r_size = read_all(ret, (char*)&r_crc, sizeof(r_crc), &opt);
	if (r_size != sizeof(r_crc))
		log_mesg(0, 1, 1, opt.debug, "read bitmap's crc error\n");

	init_crc32(&crc);

	crc = crc32(crc, bitmap, bitmap_size);
	if (crc != r_crc)
		log_mesg(0, 1, 1, opt.debug, "read bitmap's crc error\n");
}
int CommandDownload_Address(char *szCom,target_context_t **tc,
                            char *szFile,unsigned long addr,unsigned long flags) {
    int ret;
    mem_region_t *mr;

    init_crc32();
    if(flags & F_FORCE_LOCKED)
        opt_force_locked = 1;

    mr = slurp_file_or_die(szFile);

    ret = CommandMemmap(szCom,tc);
    if(ret == FALSE) return FALSE;

    set_serial_timeout(((target_context_t *)*tc)->portfd, TIMEOUT_FLASH);
    ret = download_to_addr(*tc, mr, addr);
    set_serial_timeout(((target_context_t *)*tc)->portfd, TIMEOUT_NORMAL);
    if(ret == -1) return FALSE;

    return TRUE;
}
int CommandErase_Region(char *szCom, target_context_t **tc,
                        char *szRegion, unsigned long flags)
{
    int ret;

    init_crc32();

    if (flags & F_FORCE_LOCKED)
        opt_force_locked = 1;
    else
        opt_force_locked = 0;

    ret = CommandMemmap(szCom, tc);
    if (ret == FALSE)
        return FALSE;

    set_serial_timeout(((target_context_t *)*tc)->portfd, TIMEOUT_FLASH);
    ret = erase_region(*tc, szRegion);
    set_serial_timeout(((target_context_t *)*tc)->portfd, TIMEOUT_NORMAL);
    if (ret == -1)
        return FALSE;

    return TRUE;
}
Exemple #9
0
int
main(int argc, char **argv) {
    int fd, fd2, c, i, j, k, n;
    unsigned long long size;
    struct hd_geometry g;
    struct slice all;
    struct blkpg_ioctl_arg a;
    struct blkpg_partition pt;
    struct pt *ptp;
    enum action what = LIST;
    char *p, *type, *diskdevice, *device;
    int lower, upper;
    int verbose = 0;
    int ret = 0;

    initpts();
    init_crc32();

    lower = upper = 0;
    type = device = diskdevice = NULL;

    while ((c = getopt_long (argc, argv, short_opts, long_opts, NULL))
            != -1) switch(c) {
        case 'l':
            what = LIST;
            break;
        case 'a':
            what = ADD;
            break;
        case 'd':
            what = DELETE;
            break;
        case 'g':
            force_gpt = 1;
            break;
        case 'n':
            p = optarg;
            lower = atoi(p);
            p = strchr(p, '-');
            if (p)
                upper = atoi(p+1);
            else
                upper = lower;
            break;
        case 't':
            type = optarg;
            break;
        case 'v':
            verbose = 1;
            break;
        case '?':
        default:
            fprintf(stderr, "unknown option\n");
            exit(1);
        }

    if (optind == argc-2) {
        device = argv[optind];
        diskdevice = argv[optind+1];
    } else if (optind == argc-1) {
        diskdevice = device = argv[optind];
    } else {
        fprintf(stderr, "call: partx -opts [device] wholedisk\n");
        exit(1);
    }

    fd = open(diskdevice, O_RDONLY);
    if (fd == -1) {
        perror(diskdevice);
        exit(1);
    }

    /* remove the indicated partitions from the kernel partition tables */
    if (what == DELETE) {
        if (device != diskdevice) {
            fprintf(stderr,
                    "call: partx -d [--nr M-N] wholedisk\n");
            exit(1);
        }

        if (!lower)
            lower = 1;

        while (upper == 0 || lower <= upper) {
            int err;

            if (lower > MAXSLICES)
                break;
            pt.pno = lower;
            pt.start = 0;
            pt.length = 0;
            pt.devname[0] = 0;
            pt.volname[0] = 0;
            a.op = BLKPG_DEL_PARTITION;
            a.flags = 0;
            a.datalen = sizeof(pt);
            a.data = &pt;
            if (ioctl(fd, BLKPG, &a) == -1)
                err = errno;
            else
                err = 0;
            errmerge(err, lower,
                     "error deleting partition %d: ",
                     "error deleting partitions %d-%d: ");
            /* expected errors:
               EBUSY: mounted or in use as swap
               ENXIO: no such nonempty partition
               EINVAL: not wholedisk, or bad pno
               EACCES/EPERM: permission denied
            */
            if (err && err != EBUSY && err != ENXIO) {
                ret = 1;
                break;
            }
            if (err == 0 && verbose)
                printf("deleted partition %d\n", lower);
            lower++;
        }
        errmerge(0, 0,
                 "error deleting partition %d: ",
                 "error deleting partitions %d-%d: ");
        return ret;
    }

    if (device != diskdevice) {
        fd2 = open(device, O_RDONLY);
        if (fd2 == -1) {
            perror(device);
            exit(1);
        }
    } else {
        fd2 = fd;
    }

    if (ioctl(fd, HDIO_GETGEO, &g)) {
        perror("HDIO_GETGEO");
        exit(1);
    }
    if (g.start != 0) {
        fprintf(stderr, "last arg is not the whole disk\n");
        fprintf(stderr, "call: partx -opts device wholedisk\n");
        exit(1);
    }

    if (ioctl(fd2, HDIO_GETGEO, &g)) {
        perror("HDIO_GETGEO");
        exit(1);
    }
    all.start = g.start;

    if (blkdev_get_sectors(fd2, &size) != 0) {
        perror("partx");
        exit(1);
    }
    all.size = (unsigned int) size;

    if (verbose)
        printf("device %s: start %d size %d\n",
               device, all.start, all.size);

    if (all.size == 0) {
        fprintf(stderr, "That disk slice has size 0\n");
        exit(0);
    }
    if (all.size == 2)
        all.size = 0;	/* probably extended partition */

    /* add the indicated partitions to the kernel partition tables */
    if (!lower)
        lower = 1;
    for (i = 0; i < ptct; i++) {
        ptp = &pts[i];
        if (!type || !strcmp(type, ptp->type)) {
            n = ptp->fn(fd, all, slices, ARRAY_SIZE(slices));
            if (n >= 0 && verbose)
                printf("%s: %d slices\n", ptp->type, n);
            if (n > 0 && (verbose || what == LIST)) {
                for (j=0; j<n; j++)
                    printf("#%2d: %9d-%9d (%9d sectors, %6d MB)\n",
                           lower+j,
                           slices[j].start,
                           slices[j].start+slices[j].size-1,
                           slices[j].size,
                           (int)((512 * (long long) slices[j].size)
                                 / 1000000));
            }
            if (n > 0 && what == ADD) {
                /* test for overlap, as in the case of an
                   extended partition, and reduce size */
                for (j=0; j<n; j++) {
                    for (k=j+1; k<n; k++) {
                        if (slices[k].start > slices[j].start &&
                                slices[k].start < slices[j].start +
                                slices[j].size) {
                            slices[j].size = slices[k].start -
                                             slices[j].start;
                            if (verbose)
                                printf("reduced size of "
                                       "partition #%d to %d\n",
                                       lower+j,
                                       slices[j].size);
                        }
                    }
                }
                for (j=0; j<n; j++) {
                    /* skip unused/empty partitions */
                    if (slices[j].size == 0)
                        continue;
                    pt.pno = lower+j;
                    pt.start = 512 * (long long) slices[j].start;
                    pt.length = 512 * (long long) slices[j].size;
                    pt.devname[0] = 0;
                    pt.volname[0] = 0;
                    a.op = BLKPG_ADD_PARTITION;
                    a.flags = 0;
                    a.datalen = sizeof(pt);
                    a.data = &pt;
                    if (ioctl(fd, BLKPG, &a) == -1) {
                        perror("BLKPG");
                        fprintf(stderr,
                                "error adding partition %d\n",
                                lower+j);
                    } else if (verbose)
                        printf("added partition %d\n", lower+j);
                }
            }
        }
    }

    return 0;
}
Exemple #10
0
int main(int argc, char **argv) {

    char c;

    int i,aa,aarx;
    double sigma2, sigma2_dB=0,SNR,snr0=10.0,snr1=10.2;
    int snr1set=0;
    uint32_t *txdata,*rxdata[2];
    double *s_re[2],*s_im[2],*r_re[2],*r_im[2];
    double iqim=0.0;
    int trial, ntrials=1;
    int n_rx=1;

    int awgn_flag=0;
    int n_frames=1;
    channel_desc_t *ch;
    uint32_t tx_lev,tx_lev_dB;
    int interf1=-19,interf2=-19;
    SCM_t channel_model=AWGN;
    uint32_t sdu_length_samples;
    TX_VECTOR_t tx_vector;
    int errors=0,misdetected_errors=0,signal_errors=0;
    int symbols=0;
    int tx_offset = 0,rx_offset;
    RX_VECTOR_t *rxv;
    uint8_t *data_ind,*data_ind_rx;
    int no_detection=1;
    int missed_packets=0;
    uint8_t rxp;
    int off,off2;
    double txg,txg_dB;
    int log2_maxh;
    double  snr_array[100];
    int  errors_array[100];
    int  trials_array[100];
    int  misdetected_errors_array[100];
    int  signal_errors_array[100];
    int  missed_packets_array[100];
    int  cnt=0;
    char fname[100],vname[100];
    int stop=0;

    data_ind    = (uint8_t*)malloc(4095+2+1);
    data_ind_rx = (uint8_t*)malloc(4095+2+1);

    tx_vector.rate=1;
    tx_vector.sdu_length=256;
    tx_vector.service=0;

    logInit();

    randominit(0);
    set_taus_seed(0);

    // Basic initializations
    init_fft(64,6,rev64);
    init_interleavers();
    ccodedot11_init();
    ccodedot11_init_inv();
    phy_generate_viterbi_tables();

    init_crc32();

    data_ind[0] = 0;
    data_ind[1] = 0;

    tx_offset = taus()%(FRAME_LENGTH_SAMPLES_MAX/2);

    while ((c = getopt (argc, argv, "hag:n:s:S:z:r:p:d:")) != -1) {
        switch (c) {
        case 'a':
            printf("Running AWGN simulation\n");
            awgn_flag = 1;
            ntrials=1;
            break;
        case 'g':
            switch((char)*optarg) {
            case 'A':
                channel_model=SCM_A;
                break;
            case 'B':
                channel_model=SCM_B;
                break;
            case 'C':
                channel_model=SCM_C;
                break;
            case 'D':
                channel_model=SCM_D;
                break;
            case 'E':
                channel_model=EPA;
                break;
            case 'F':
                channel_model=EVA;
                break;
            case 'G':
                channel_model=ETU;
                break;
            case 'H':
                channel_model=Rayleigh8;
            case 'I':
                channel_model=Rayleigh1;
            case 'J':
                channel_model=Rayleigh1_corr;
            case 'K':
                channel_model=Rayleigh1_anticorr;
            case 'L':
                channel_model=Rice8;
            case 'M':
                channel_model=Rice1;
                break;
            default:
                printf("Unsupported channel model!\n");
                exit(-1);
            }
            break;
        case 'd':
            tx_offset = atoi(optarg);
            break;
        case 'p':
            tx_vector.sdu_length = atoi(optarg);
            if (atoi(optarg)>4095) {
                printf("Illegal sdu_length %d\n",tx_vector.sdu_length);
                exit(-1);
            }
            break;
        case 'r':
            tx_vector.rate = atoi(optarg);
            if (atoi(optarg)>7) {
                printf("Illegal rate %d\n",tx_vector.rate);
                exit(-1);
            }
            break;
        case 'n':
            n_frames = atoi(optarg);
            break;
        case 's':
            snr0 = atof(optarg);
            printf("Setting SNR0 to %f\n",snr0);
            break;
        case 'S':
            snr1 = atof(optarg);
            snr1set=1;
            printf("Setting SNR1 to %f\n",snr1);
            break;
        case 'z':
            n_rx=atoi(optarg);
            if ((n_rx==0) || (n_rx>2)) {
                printf("Unsupported number of rx antennas %d\n",n_rx);
                exit(-1);
            }
            break;
        default:
        case 'h':
            printf("%s -h(elp) -a(wgn on) -p(extended_prefix) -N cell_id -f output_filename -F input_filename -g channel_model -n n_frames -t Delayspread -r Ricean_FactordB -s snr0 -S snr1 -x transmission_mode -y TXant -z RXant -i Intefrence0 -j Interference1 -A interpolation_file -C(alibration offset dB) -N CellId\n",argv[0]);
            printf("-h This message\n");
            printf("-a Use AWGN channel and not multipath\n");
            printf("-n Number of frames to simulate\n");
            printf("-s Starting SNR, runs from SNR0 to SNR0 + 5 dB.  If n_frames is 1 then just SNR is simulated\n");
            printf("-S Ending SNR, runs from SNR0 to SNR1\n");
            printf("-g [A,B,C,D,E,F,G] Use 3GPP SCM (A,B,C,D) or 36-101 (E-EPA,F-EVA,G-ETU) models (ignores delay spread and Ricean factor)\n");
            printf("-z Number of RX antennas used\n");
            printf("-F Input filename (.txt format) for RX conformance testing\n");
            exit (-1);
            break;
        }
    }

    if (n_frames==1)
        snr1 = snr0+.2;
    else
        snr1 = snr0+5;
    for (i=0; i<tx_vector.sdu_length; i++)
        data_ind[i+2] = i;//taus();  // randomize packet
    data_ind[tx_vector.sdu_length+2+4]=0;  // Tail byte

    // compute number of OFDM symbols in DATA period
    symbols = ((4+2+1+tx_vector.sdu_length)<<1) / nibbles_per_symbol[tx_vector.rate];
    if ((((4+2+1+tx_vector.sdu_length)<<1) % nibbles_per_symbol[tx_vector.rate]) > 0)
        symbols++;

    sdu_length_samples = (symbols + 5) * 80;

    printf("Number of symbols for sdu : %d, samples %d\n",symbols,sdu_length_samples);

    txdata = (uint32_t*)memalign(16,sdu_length_samples*sizeof(uint32_t));
    for (i=0; i<n_rx; i++) {
        rxdata[i] = (uint32_t*)memalign(16,(FRAME_LENGTH_SAMPLES_MAX+1280)*sizeof(uint32_t));
        bzero(rxdata[i],(FRAME_LENGTH_SAMPLES_MAX+1280)*sizeof(uint32_t));
    }
    s_re[0] = (double *)malloc(sdu_length_samples*sizeof(double));
    bzero(s_re[0],sdu_length_samples*sizeof(double));
    s_im[0] = (double *)malloc(sdu_length_samples*sizeof(double));
    bzero(s_im[0],sdu_length_samples*sizeof(double));
    for (i=0; i<n_rx; i++) {
        r_re[i] = (double *)malloc((sdu_length_samples+100)*sizeof(double));
        bzero(r_re[i],(sdu_length_samples+100)*sizeof(double));
        r_im[i] = (double *)malloc((sdu_length_samples+100)*sizeof(double));
        bzero(r_im[i],(sdu_length_samples+100)*sizeof(double));
    }

    ch = new_channel_desc_scm(1,
                              n_rx,
                              channel_model,
                              BW,
                              0.0,
                              0,
                              0);


    if (ch==NULL) {
        printf("Problem generating channel model. Exiting.\n");
        exit(-1);
    }


    phy_tx_start(&tx_vector,txdata,0,FRAME_LENGTH_SAMPLES_MAX,data_ind);

    tx_lev = signal_energy((int32_t*)txdata,320);
    tx_lev_dB = (unsigned int) dB_fixed(tx_lev);

    write_output("txsig0.m","txs", txdata,sdu_length_samples,1,1);

    // multipath channel

    for (i=0; i<sdu_length_samples; i++) {
        s_re[0][i] = (double)(((short *)txdata)[(i<<1)]);
        s_im[0][i] = (double)(((short *)txdata)[(i<<1)+1]);
    }

    for (SNR=snr0; SNR<snr1; SNR+=.2) {

        printf("n_frames %d SNR %f sdu_length %d rate %d\n",n_frames,SNR,tx_vector.sdu_length,tx_vector.rate);
        errors=0;
        misdetected_errors=0;
        signal_errors=0;
        missed_packets=0;
        stop=0;
        for (trial=0; trial<n_frames; trial++) {
            //      printf("Trial %d (errors %d), sdu_length_samples %d\n",trial,errors,sdu_length_samples);
            sigma2_dB = 25; //10*log10((double)tx_lev) - SNR;
            txg_dB = 10*log10((double)tx_lev) - (SNR + sigma2_dB);
            txg = pow(10.0,-.05*txg_dB);
            if (n_frames==1)
                printf("sigma2_dB %f (SNR %f dB) tx_lev_dB %f, txg %f\n",sigma2_dB,SNR,10*log10((double)tx_lev)-txg_dB,txg_dB);
            //AWGN
            sigma2 = pow(10,sigma2_dB/10);
            //      printf("Sigma2 %f (sigma2_dB %f)\n",sigma2,sigma2_dB);

            //          sigma2 = 0;

            multipath_channel(ch,s_re,s_im,r_re,r_im,
                              sdu_length_samples,0);

            if (n_frames==1) {
                printf("rx_level data symbol %f, tx_lev %f\n",
                       10*log10(signal_energy_fp(r_re,r_im,1,80,0)),
                       10*log10(tx_lev));
            }

            for (aa=0; aa<n_rx; aa++) {
                for (i=0; i<(sdu_length_samples+100); i++) {


                    ((short*)&rxdata[aa][tx_offset])[(i<<1)]   = (short) (((txg*r_re[aa][i]) + sqrt(sigma2/2)*gaussdouble(0.0,1.0)));
                    ((short*)&rxdata[aa][tx_offset])[1+(i<<1)] = (short) (((txg*r_im[aa][i]) + (iqim*r_re[aa][i]*txg) + sqrt(sigma2/2)*gaussdouble(0.0,1.0)));

                    //	  if (i<128)
                    //	    printf("i%d : rxdata %d, txdata %d\n",i,((short *)rxdata[aa])[rx_offset+(i<<1)],((short *)txdata)[i<<1]);
                }

                for (i=0; i<tx_offset; i++) {
                    ((short*) rxdata[aa])[(i<<1)]   = (short) (sqrt(sigma2/2)*gaussdouble(0.0,1.0));
                    ((short*) rxdata[aa])[1+(i<<1)] = (short) (sqrt(sigma2/2)*gaussdouble(0.0,1.0));
                }
                for (i=(tx_offset+sdu_length_samples+100); i<FRAME_LENGTH_SAMPLES_MAX; i++) {
                    ((short*) rxdata[aa])[(i<<1)]   = (short) (sqrt(sigma2/2)*gaussdouble(0.0,1.0));
                    ((short*) rxdata[aa])[1+(i<<1)] = (short) (sqrt(sigma2/2)*gaussdouble(0.0,1.0));
                }

            }
            if (n_frames==1) {
                write_output("rxsig0.m","rxs", &rxdata[0][0],FRAME_LENGTH_SAMPLES_MAX,1,1);
            }
            no_detection=1;
            off = 0;
            while(off<FRAME_LENGTH_SAMPLES_MAX) {

                rxp = dB_fixed(signal_energy(rxdata[0]+off,512));
                if (n_frames==1)
                    printf("off %d: rxp %d (%d)\n",off,rxp,signal_energy(rxdata[0]+off,104));

                if (rxp>RX_THRES_dB) {
                    if (off<105)
                        off2 = FRAME_LENGTH_SAMPLES_MAX-105;
                    else
                        off2=off;
                    if ((initial_sync(&rxv,&rx_offset,&log2_maxh,(uint32_t*)rxdata[0],FRAME_LENGTH_SAMPLES_MAX,off2,1) == BUSY)) {
                        if (n_frames==1)
                            printf("Channel is busy, rxv %p, offset %d\n",(void*)rxv,rx_offset);
                        no_detection=0;
                        if (rxv) {
                            if (n_frames==1)
                                printf("Rate %d, SDU_LENGTH %d\n",rxv->rate,rxv->sdu_length);
                            if ( (rxv->rate != tx_vector.rate)||(rxv->sdu_length != tx_vector.sdu_length)) {
                                signal_errors++;
                                if ((signal_errors > (n_frames/10)) && (trial>=100)) {
                                    stop=1;
                                }
                                if (n_frames == 1)
                                    printf("SIGNAL error: rx_offset %d, tx_offset %d (off2 %d)\n",rx_offset,tx_offset,off2);
                                break;
                            }
                            else {
                                memset(data_ind_rx,0,rxv->sdu_length+4+2+1);
                                if (data_detection(rxv,data_ind_rx,(uint32_t*)rxdata[0],FRAME_LENGTH_SAMPLES_MAX,rx_offset,log2_maxh,NULL)) {
                                    for (i=0; i<rxv->sdu_length+6; i++) {
                                        if (data_ind[i]!=data_ind_rx[i]) {
                                            //printf("error position %d : %x,%x\n",i,data_ind[i],data_ind_rx[i]);
                                            misdetected_errors++;
                                            errors++;
                                        }
                                    }
                                    if ((errors > (n_frames/10)) && (trial>100)) {
                                        stop=1;
                                        break;
                                    }
                                } // initial_synch returns IDLE
                                else {
                                    errors++;
                                    if (n_frames == 1) {
                                        printf("Running data_detection fails\n");

                                        for (i=0; i<rxv->sdu_length+6; i++) {
                                            if (data_ind[i]!=data_ind_rx[i]) {
                                                printf("error position %d : %x,%x\n",i,data_ind[i],data_ind_rx[i]);
                                            }
                                        }
                                    }
                                    if ((errors > (n_frames/10)) && (trial>=100)) {
                                        stop=1;
                                        break;
                                    }

                                }
                                break;
                            }
                        }
                    }
                }

                off+=105;
            }
            if (no_detection==1)
                missed_packets++;
            if (stop==1)
                break;
        }

        printf("\nSNR %f dB: errors %d/%d, misdetected errors %d/%d,signal_errors %d/%d, missed_packets %d/%d\n",SNR,errors,trial-signal_errors,misdetected_errors,trial-signal_errors,signal_errors,trial,missed_packets,trial);
        snr_array[cnt] = SNR;
        errors_array[cnt] = errors;
        trials_array[cnt] = trial;
        misdetected_errors_array[cnt] = misdetected_errors;
        signal_errors_array[cnt] = signal_errors;
        missed_packets_array[cnt] = missed_packets;
        cnt++;
        if (cnt>99) {
            printf("too many SNR points, exiting ...\n");
            break;
        }
        if (errors == 0)
            break;
#ifdef EXECTIME
        print_is_stats();
        print_dd_stats();
#endif
    }


    sprintf(fname,"SNR_%d_%d.m",tx_vector.rate,tx_vector.sdu_length);
    sprintf(vname,"SNR_%d_%d_v",tx_vector.rate,tx_vector.sdu_length);
    write_output(fname,vname,snr_array,cnt,1,7);
    sprintf(fname,"errors_%d_%d.m",tx_vector.rate,tx_vector.sdu_length);
    sprintf(vname,"errors_%d_%d_v",tx_vector.rate,tx_vector.sdu_length);
    write_output(fname,vname,errors_array,cnt,1,2);
    sprintf(fname,"trials_%d_%d.m",tx_vector.rate,tx_vector.sdu_length);
    sprintf(vname,"trials_%d_%d_v",tx_vector.rate,tx_vector.sdu_length);
    write_output(fname,vname,trials_array,cnt,1,2);
    sprintf(fname,"signal_errors_%d_%d.m",tx_vector.rate,tx_vector.sdu_length);
    sprintf(vname,"signal_errors_%d_%d_v",tx_vector.rate,tx_vector.sdu_length);
    write_output(fname,vname,signal_errors_array,cnt,1,2);
    free(data_ind);
    free(data_ind_rx);
    //  free_channel_desc_scm(ch);

    free(txdata);
    for (i=0; i<n_rx; i++) {
        free(rxdata[i]);
    }

    free(s_re[0]);
    free(s_im[0]);

    for (i=0; i<n_rx; i++) {
        free(r_re[i]);
        free(r_im[i]);
    }

    return(0);

}
Exemple #11
0
void write_image_bitmap(int* ret, file_system_info fs_info, image_options img_opt, unsigned long* bitmap, cmd_opt* opt) {

	int i, debug = opt->debug;

	switch(img_opt.bitmap_mode) {

	case BM_BIT:
	{
		if (write_all(ret, (char*)bitmap, BITS_TO_BYTES(fs_info.totalblock), opt) == -1)
			log_mesg(0, 1, 1, debug, "write bitmap to image error: %s\n", strerror(errno));

		break;
	}

	case BM_BYTE:
	{
		char bbuffer[16384];

		for (i = 0; i < fs_info.totalblock; ++i) {

			bbuffer[i % sizeof(bbuffer)] = pc_test_bit(i, bitmap);

			if (i % sizeof(bbuffer) == sizeof(bbuffer) - 1 || i == fs_info.totalblock - 1) {

				if (write_all(ret, bbuffer, 1 + (i % sizeof(bbuffer)), opt) == -1)
					log_mesg(0, 1, 1, debug, "write bitmap to image error: %s\n", strerror(errno));
			}
		}

		break;
	}

	case BM_NONE:
	{
		// All blocks MUST be present
		if (fs_info.usedblocks != fs_info.totalblock)
			log_mesg(0, 1, 1, debug, "ERROR: used blocks count does not match total blocks count\n");
		break;
	}

	default:
		log_mesg(0, 1, 1, debug, "ERROR: unknown bitmap mode [%d]\n", img_opt.bitmap_mode);
		break;
	}

	if (img_opt.bitmap_mode != BM_NONE) {

		switch (img_opt.image_version) {

		case 0x0001:
			if (write_all(ret, BIT_MAGIC, BIT_MAGIC_SIZE, opt) != BIT_MAGIC_SIZE)
				log_mesg(0, 1, 1, debug, "write bitmap to image error: %s\n", strerror(errno));
			break;

		case 0x0002: {

			uint32_t crc;

			init_crc32(&crc);

			crc = crc32(crc, bitmap, BITS_TO_BYTES(fs_info.totalblock));

			if (write_all(ret, (char*)&crc, sizeof(crc), opt) != sizeof(crc))
				log_mesg(0, 1, 1, debug, "write bitmap to image error: %s\n", strerror(errno));

			break;
		}

		default:
			log_mesg(0, 1, 1, debug, "image_version is not set or write_image_bitmap() must to be updated [%d]\n",
				img_opt.image_version);
			break;
		}
	}
}
int main(int argc, char *argv[])
{
	command_t *cmd;
	mem_region_t *mr;
	target_context_t *tc;

#if !defined(WIN32)
	cmd = get_commands(argc, argv);
	if(cmd == NULL) return -1;
#endif

	/* open raw ethernet socket if desired, then drop root */
#if !defined(WIN32)
	if (opt_ethernet)
		eth_raw_socket();
#endif
	drop_root();

	/* miscellaneous initialization */

	init_crc32();

	/* open a connection to the target */
	if (!(tc = target_open(opt_port, opt_ethernet ? opt_netif : NULL))){
		panic("couldn't open connection to target");
		return -1;
	}

	while (cmd) {
		command_t *tmp;

		if (opt_verbose)
			print_command(cmd);

		switch (cmd->type) {

#ifdef AJ_FIRMUPDATE_SUPPORT
                case CMD_FIRMUPDATE:
			mr = slurp_file_or_die(cmd->input_path);
			if(require_flags(tc, REQ_MEM_MAP) == -1){
				return -1;
			}
                        firmupdate(tc, mr);
			break;
#endif

		case CMD_DOWNLOAD:
			if (cmd->region && cmd->info.download.have_address){
				panic("can't specify both region and address");
				return -1;
			}

			mr = slurp_file_or_die(cmd->input_path);
			if(require_flags(tc, REQ_MEM_MAP) == -1){
				return -1;
			}
			if (cmd->region)
				download_to_region(tc, mr, cmd->region);
			else if (cmd->info.download.have_address)
				download_to_addr(tc, mr, cmd->addr);
			else{
				warn("download: must specify address or region\n");
				return -1;
			}
			break;

		case CMD_ERASE:
			if (cmd->region && cmd->info.download.have_address){
				panic("can't specify both region and address");
				return -1;
			}

			if(require_flags(tc, REQ_MEM_MAP) == -1){
				return -1;
			}
			if (cmd->region)
				erase_region(tc, cmd->region);
			else if (cmd->info.download.have_address)
				erase_addr(tc, cmd->addr);
			else{
				warn("erase: must specify address or region\n");
				return -1;
			}
			break;

		case CMD_MAP:
			if(require_flags(tc, REQ_MEM_MAP) == -1){
				return -1;
			}
			region_print(tc->memmap);
			return 0;

		case CMD_TERMINAL:
			if(restore_interactive(tc) == -1){
				return -1;
			}
#if !defined(WIN32)
			serial_terminal();
#endif
			return 0;

		case CMD_MD5SUM:
			if (cmd->region && cmd->info.download.have_address){
				panic("md5sum: can't specify both region and address");
				return -1;
			}

			if (cmd->size == 0) {
				warn("md5sum: must specify size\n");
				return -1;
			}

			if (cmd->region){
				if(require_flags(tc, REQ_MEM_MAP) == -1){
					return -1;
				}

				md5sum_region(tc, cmd->region, cmd->size);
			} else if (cmd->info.download.have_address) {
				md5sum_addr(tc, cmd->addr, cmd->size);
			} else{
				warn("md5sum: must specify address or region\n");
				return -1;
			}
			break;

		default:
			warn("unsupported command\n");
			return -1;
		}

		tmp = cmd;
		cmd = cmd->next;
		/*
		 * We don't free paths in the command because we don't
		 * know whether or not they're dynamically allocated.
		 * Thus we leak a little memory.  It's not like this
		 * is a long-running program...
		 */
		free(tmp);
	}

	restore_interactive(tc);
	return 0;
}
Exemple #13
0
int
main(int argc, char **argv){
	int fd, i, j, m, n, op, off, arg, c, d;
	struct slice all;
	struct pt *ptp;
	enum action what = LIST;
	char *type, *diskdevice, *device, *progname;
	int verbose = 0;
	char partname[PARTNAME_SIZE], params[PARTNAME_SIZE + 16];
	char * loopdev = NULL;
	char * delim = NULL;
	char *uuid = NULL;
	char *mapname = NULL;
	int loopro = 0;
	int hotplug = 0;
	int loopcreated = 0;
	int sync = 0;
	struct stat buf;
	uint32_t cookie = 0;

	initpts();
	init_crc32();

	type = device = diskdevice = NULL;
	memset(&all, 0, sizeof(all));
	memset(&partname, 0, sizeof(partname));

	/* Check whether hotplug mode. */
	progname = strrchr(argv[0], '/');

	if (!progname)
		progname = argv[0];
	else
		progname++;

	if (!strcmp(progname, "kpartx.dev")) { /* Hotplug mode */
		hotplug = 1;

		/* Setup for original kpartx variables */
		if (!(device = get_hotplug_device()))
			exit(1);

		diskdevice = device;
		what = ADD;
	} else if (argc < 2) {
		usage();
		exit(1);
	}

	while ((arg = getopt(argc, argv, short_opts)) != EOF) switch(arg) {
		case 'g':
			force_gpt=1;
			break;
		case 't':
			type = optarg;
			break;
		case 'v':
			verbose = 1;
			break;
		case 'p':
			delim = optarg;
			break;
		case 'l':
			what = LIST;
			break;
		case 'a':
			what = ADD;
			break;
		case 'd':
			what = DELETE;
			break;
		case 's':
			sync = 1;
			break;
		default:
			usage();
			exit(1);
	}

	if (!sync)
		dm_udev_set_sync_support(0);

	if (dm_prereq(DM_TARGET, 0, 0, 0) && (what == ADD || what == DELETE)) {
		fprintf(stderr, "device mapper prerequisites not met\n");
		exit(1);
	}

	if (hotplug) {
		/* already got [disk]device */
	} else if (optind == argc-2) {
		device = argv[optind];
		diskdevice = argv[optind+1];
	} else if (optind == argc-1) {
		diskdevice = device = argv[optind];
	} else {
		usage();
		exit(1);
	}

	if (stat(device, &buf)) {
		printf("failed to stat() %s\n", device);
		exit (1);
	}

	if (S_ISREG (buf.st_mode)) {
		/* already looped file ? */
		loopdev = find_loop_by_file(device);

		if (!loopdev && what == DELETE)
			exit (0);

		if (!loopdev) {
			loopdev = find_unused_loop_device();

			if (set_loop(loopdev, device, 0, &loopro)) {
				fprintf(stderr, "can't set up loop\n");
				exit (1);
			}
			loopcreated = 1;
		}
		device = loopdev;
	}

	if (delim == NULL) {
		delim = malloc(DELIM_SIZE);
		memset(delim, 0, DELIM_SIZE);
		set_delimiter(device, delim);
	}

	off = find_devname_offset(device);

	if (!loopdev) {
		uuid = dm_mapuuid((unsigned int)MAJOR(buf.st_rdev),
				  (unsigned int)MINOR(buf.st_rdev));
		mapname = dm_mapname((unsigned int)MAJOR(buf.st_rdev),
				     (unsigned int)MINOR(buf.st_rdev));
	}

	if (!uuid)
		uuid = device + off;

	if (!mapname)
		mapname = device + off;

	fd = open(device, O_RDONLY);

	if (fd == -1) {
		perror(device);
		exit(1);
	}

	/* add/remove partitions to the kernel devmapper tables */
	int r = 0;
	for (i = 0; i < ptct; i++) {
		ptp = &pts[i];

		if (type && strcmp(type, ptp->type))
			continue;

		/* here we get partitions */
		n = ptp->fn(fd, all, slices, SIZE(slices));

#ifdef DEBUG
		if (n >= 0)
			printf("%s: %d slices\n", ptp->type, n);
#endif

		if (n > 0)
			close(fd);
		else
			continue;

		switch(what) {
		case LIST:
			for (j = 0, c = 0, m = 0; j < n; j++) {
				if (slices[j].size == 0)
					continue;
				if (slices[j].container > 0) {
					c++;
					continue;
				}

				slices[j].minor = m++;

				printf("%s%s%d : 0 %" PRIu64 " %s %" PRIu64"\n",
				       mapname, delim, j+1,
				       slices[j].size, device,
				       slices[j].start);
			}
			/* Loop to resolve contained slices */
			d = c;
			while (c) {
				for (j = 0; j < n; j++) {
					uint64_t start;
					int k = slices[j].container - 1;

					if (slices[j].size == 0)
						continue;
					if (slices[j].minor > 0)
						continue;
					if (slices[j].container == 0)
						continue;
					slices[j].minor = m++;

					start = slices[j].start - slices[k].start;
					printf("%s%s%d : 0 %" PRIu64 " /dev/dm-%d %" PRIu64 "\n",
					       mapname, delim, j+1,
					       slices[j].size,
					       slices[k].minor, start);
					c--;
				}
				/* Terminate loop if nothing more to resolve */
				if (d == c)
					break;
			}

			if (loopcreated && S_ISREG (buf.st_mode)) {
				if (del_loop(device)) {
					if (verbose)
						printf("can't del loop : %s\n",
							device);
					exit(1);
				}
				printf("loop deleted : %s\n", device);
			}
			break;

		case DELETE:
			for (j = n-1; j >= 0; j--) {
				if (safe_sprintf(partname, "%s%s%d",
					     mapname, delim, j+1)) {
					fprintf(stderr, "partname too small\n");
					exit(1);
				}
				strip_slash(partname);

				if (!slices[j].size || !dm_map_present(partname))
					continue;

				if (!dm_simplecmd(DM_DEVICE_REMOVE, partname,
						  0, &cookie)) {
					r++;
					continue;
				}
				if (verbose)
					printf("del devmap : %s\n", partname);
			}

			if (S_ISREG (buf.st_mode)) {
				if (del_loop(device)) {
					if (verbose)
						printf("can't del loop : %s\n",
							device);
					exit(1);
				}
				printf("loop deleted : %s\n", device);
			}
			break;

		case ADD:
			for (j = 0, c = 0; j < n; j++) {
				if (slices[j].size == 0)
					continue;

				/* Skip all contained slices */
				if (slices[j].container > 0) {
					c++;
					continue;
				}

				if (safe_sprintf(partname, "%s%s%d",
					     mapname, delim, j+1)) {
					fprintf(stderr, "partname too small\n");
					exit(1);
				}
				strip_slash(partname);

				if (safe_sprintf(params, "%s %" PRIu64 ,
						 device, slices[j].start)) {
					fprintf(stderr, "params too small\n");
					exit(1);
				}

				op = (dm_map_present(partname) ?
					DM_DEVICE_RELOAD : DM_DEVICE_CREATE);

				if (!dm_addmap(op, partname, DM_TARGET, params,
					       slices[j].size, uuid, j+1,
					       buf.st_mode & 0777, buf.st_uid,
					       buf.st_gid, &cookie)) {
					fprintf(stderr, "create/reload failed on %s\n",
						partname);
					r++;
				}
				if (op == DM_DEVICE_RELOAD &&
				    !dm_simplecmd(DM_DEVICE_RESUME, partname,
						  1, &cookie)) {
					fprintf(stderr, "resume failed on %s\n",
						partname);
					r++;
				}
				dm_devn(partname, &slices[j].major,
					&slices[j].minor);

				if (verbose)
					printf("add map %s (%d:%d): 0 %" PRIu64 " %s %s\n",
					       partname, slices[j].major,
					       slices[j].minor, slices[j].size,
					       DM_TARGET, params);
			}
			/* Loop to resolve contained slices */
			d = c;
			while (c) {
				for (j = 0; j < n; j++) {
					uint64_t start;
					int k = slices[j].container - 1;

					if (slices[j].size == 0)
						continue;

					/* Skip all existing slices */
					if (slices[j].minor > 0)
						continue;

					/* Skip all simple slices */
					if (slices[j].container == 0)
						continue;

					/* Check container slice */
					if (slices[k].size == 0)
						fprintf(stderr, "Invalid slice %d\n",
							k);

					if (safe_sprintf(partname, "%s%s%d",
							 mapname, delim, j+1)) {
						fprintf(stderr, "partname too small\n");
						exit(1);
					}
					strip_slash(partname);

					start = slices[j].start - slices[k].start;
					if (safe_sprintf(params, "%d:%d %" PRIu64,
							 slices[k].major,
							 slices[k].minor,
							 start)) {
						fprintf(stderr, "params too small\n");
						exit(1);
					}

					op = (dm_map_present(partname) ?
					      DM_DEVICE_RELOAD : DM_DEVICE_CREATE);

					dm_addmap(op, partname, DM_TARGET, params,
						  slices[j].size, uuid, j+1,
						  buf.st_mode & 0777,
						  buf.st_uid, buf.st_gid,
						  &cookie);

					if (op == DM_DEVICE_RELOAD)
						dm_simplecmd(DM_DEVICE_RESUME,
							     partname, 1,
							     &cookie);

					dm_devn(partname, &slices[j].major,
						&slices[j].minor);

					if (verbose)
						printf("add map %s : 0 %" PRIu64 " %s %s\n",
						       partname, slices[j].size,
						       DM_TARGET, params);
					c--;
				}
				/* Terminate loop */
				if (d == c)
					break;
			}
			break;

		default:
			break;

		}
		if (n > 0)
			break;
	}
	dm_udev_wait(cookie);
	dm_lib_release();
	dm_lib_exit();

	return r;
}