Пример #1
0
// Assumes we are at the beginning of a directory and fat16_state.file_left
// is set to amount of file entries. Reads on until a given file is found,
// or no more file entries are available.
char fat16_open_file(char *filename, char *ext) {  
    char i, bytes;
    
#ifdef DEBUG
    printf("Trying to open file [%s.%s]\n", filename, ext);
#endif

    do {
        bytes = fat16_read_file(sizeof(Fat16Entry));
        
        if(bytes < sizeof(Fat16Entry))
            return FAT16_ERR_FILE_READ;
		
#ifdef DEBUG
        if(FAT16_entry->filename[0])
            printf("Found file [%.8s.%.3s]\n", FAT16_entry->filename, FAT16_entry->ext);
#endif

        for(i=0; i<8; i++) // we don't have memcmp on a MCU...
            if(FAT16_entry->filename[i] != filename[i])
                break;        
        if(i < 8) // not the filename we are looking for
            continue;
		
        for(i=0; i<3; i++) // we don't have memcmp on a MCU...
            if(FAT16_entry->ext[i] != ext[i])
                break;
        if(i < 3) // not the extension we are looking for
            continue;
            
#ifdef DEBUG
        printf("File found at cluster %d!\n", FAT16_entry->starting_cluster);
#endif

        // Initialize reading variables
        fat16_state.cluster = FAT16_entry->starting_cluster;
        fat16_state.cluster_left = fat16_state.sectors_per_cluster * 512;
        
        if(FAT16_entry->filename[0] == 0x2E || 
           FAT16_entry->attributes & 0x10) { // directory
            // set file size so large that the file entries
            // are not limited by it, but by the sectors used
            fat16_state.file_left = 0xFFFFFFFF;
        } else {
            fat16_state.file_left = FAT16_entry->file_size;
        }
        
        // Go to first cluster
        fat16_seek(fat16_state.data_start + (fat16_state.cluster-2) * 
            fat16_state.sectors_per_cluster * 512);
        return 0;
    } while(fat16_state.file_left > 0);
    
#ifdef DEBUG    
    printf("File not found: [%s.%s]!\n", filename, ext);
#endif
    
    return FAT16_ERR_FILE_NOT_FOUND;
}
Пример #2
0
//0 for success, negative for failure
int writeLogCon(void) {
  char line[64];
  char stringBuf[512];

  struct fat16_file_struct fd;
  int len;
  int s=0;
  int t=0;
  line[0]=0;
  int result;

  result = root_open(&fd,LOGCON_NAME);
    
  if(result<0) return result;
  len = fat16_read_file(&fd, (unsigned char *)stringBuf, 512);
  if(len<0) return -1;
  fat16_close_file(&fd);
  
  s=0;
  t=0;
  for(s = 0; s < len; s++) {
    if(stringBuf[s]=='\n') {
      line[t]=0;
	  if(line[t-1]=='\r') line[t-1]=0;
      t=0;
      fillPktStart(&sdBuf,PT_FILE);
      fillPktString(&sdBuf,line);
      fillPktFinish(&sdBuf);
      if(result<0) return result;
    } else {
      line[t]=stringBuf[s];
      t++;
    }
  }
  
  return 0;
}
Пример #3
0
int load_fw(char* filename)
{
    struct fat16_file_struct * fd;
    int read;
    int i;
    char* addy;
    addy = (char*)STARTADDR;

    /* Erase all sectors we could use */
    prep_command[1]=ERASE_SECT_START;
    prep_command[2]=ERASE_SECT_STOP;
    iap_fn(prep_command,result);
    iap_fn(erase_command,result);

    /* Open the file */
    fd = root_open(filename);

    /* Clear the buffer */
    for(i=0;i<READBUFSIZE;i++)
    {
        readbuf[i]=0;
    }

    /* Read the file contents, and print them out */
    while( (read=fat16_read_file(fd,(unsigned char*)readbuf,READBUFSIZE)) > 0 )
    {

        // Print Data to UART (DEBUG)
        /*for(i=0; i<read; i++)
        {
            rprintf("%c",readbuf[i]);
        }*/

        /* Write Data to Flash */

        /* Prepare Current Sector */
        /* This assumes that we are always only writing to one sector!
                     * This is only true if our write size necessarily aligns
                     * on proper boundaries. Be careful */
        prep_command[1] = SECTOR_NUMBER(((int)addy));
        prep_command[2] = prep_command[1];
        iap_fn(prep_command,result);


        /* *** Should check result here... but I'm not */
        /* If all went according to plan, the sector is primed for write
                     * (or erase)
                     */


        /* Now write data */
        write_command[1]=(unsigned int)addy;
        write_command[2]=(unsigned int)readbuf;
        write_command[3]=READBUFSIZE;
        iap_fn(write_command,result);

        /* *** Should check result here... but I'm not */
        /* If all went according to plan, data is in flash,
                     * and the sector is locked again
                     */


        /* Done with current data, so clear buffer
                     *  this is because we ALWAYS write out the
                     *  entire buffer, and we don't want to write
                     *  garbage data on reads smaller than READBUFSIZE
                     */
        for(i=0;i<READBUFSIZE;i++)
        {
            readbuf[i]=0;
        }


        /* Now update the address-- */
        addy = addy + READBUFSIZE;

        /* And we should probably bounds-check... *SIGH*/
        if((unsigned int)addy > (unsigned int) 0x0007CFFF)
        {
            break;
        }

    }

    /* All data copied to FLASH */
    /* Debug: Report the flash contents */
    /*  addy = (char*)STARTADDR;*/

    /*  PRINTF0("Dirty Screech\n\r");*/
    /*  delay_ms(10000);*/
    /*  while(addy<(char*)0x00050000)*/
    /*  {*/
    /*    __putchar(*addy++);*/
    /*  }*/
    /*  PRINTF0("Dirty Screech completed\n\r");*/


    /* Close the file! */
    sd_raw_sync();
    fat16_close_file(fd);
    root_delete(filename);

    return 0;
}
Пример #4
0
int main()
{
    /* setup uart */
    uart_init();
    /* setup stdio */
    uart_connect_stdio();

    /* setup sd card slot */
    if(!sd_raw_init())
    {
#if DEBUG
        printf_P(PSTR("MMC/SD initialization failed\n"));
#endif
        return 1;
    }

    /* open first partition */
    struct partition_struct* partition = partition_open(sd_raw_read,
                                                        sd_raw_read_interval,
                                                        sd_raw_write,
                                                        0);

    if(!partition)
    {
        /* If the partition did not open, assume the storage device
         * is a "superfloppy", i.e. has no MBR.
         */
        partition = partition_open(sd_raw_read,
                                   sd_raw_read_interval,
                                   sd_raw_write,
                                   -1
                                  );
        if(!partition)
        {
#if DEBUG
            printf_P(PSTR("opening partition failed\n"));
#endif
            return 1;
        }
    }

    /* open file system */
    struct fat16_fs_struct* fs = fat16_open(partition);
    if(!fs)
    {
#if DEBUG
        printf_P(PSTR("opening filesystem failed\n"));
#endif
        return 1;
    }

    /* open root directory */
    struct fat16_dir_entry_struct directory;
    fat16_get_dir_entry_of_path(fs, "/", &directory);

    struct fat16_dir_struct* dd = fat16_open_dir(fs, &directory);
    if(!dd)
    {
#if DEBUG
        printf_P(PSTR("opening root directory failed\n"));
#endif
        return 1;
    }
    
    /* provide a simple shell */
    char buffer[24];
    while(1)
    {
        /* print prompt */
        uart_putc('>');
        uart_putc(' ');

        /* read command */
        char* command = buffer;
        if(read_line(command, sizeof(buffer)) < 1)
            continue;

        /* execute command */
        if(strncmp_P(command, PSTR("cd "), 3) == 0)
        {
            command += 3;
            if(command[0] == '\0')
                continue;

            /* change directory */
            struct fat16_dir_entry_struct subdir_entry;
            if(find_file_in_dir(fs, dd, command, &subdir_entry))
            {
                struct fat16_dir_struct* dd_new = fat16_open_dir(fs, &subdir_entry);
                if(dd_new)
                {
                    fat16_close_dir(dd);
                    dd = dd_new;
                    continue;
                }
            }

            printf_P(PSTR("directory not found: %s\n"), command);
        }
        else if(strcmp_P(command, PSTR("ls")) == 0)
        {
            /* print directory listing */
            struct fat16_dir_entry_struct dir_entry;
            while(fat16_read_dir(dd, &dir_entry))
            {
                printf_P(PSTR("%10lu  %s%c\n"),
                         dir_entry.file_size,
                         dir_entry.long_name,
                         (dir_entry.attributes & FAT16_ATTRIB_DIR) ? '/' : ' '
                        );
            }
        }
        else if(strncmp_P(command, PSTR("cat "), 4) == 0)
        {
            command += 4;
            if(command[0] == '\0')
                continue;
            
            /* search file in current directory and open it */
            struct fat16_file_struct* fd = open_file_in_dir(fs, dd, command);
            if(!fd)
            {
                printf_P(PSTR("error opening %s\n"), command);
                continue;
            }

            /* print file contents */
            uint8_t buffer[8];
            uint32_t offset = 0;
            while(fat16_read_file(fd, buffer, sizeof(buffer)) > 0)
            {
                printf_P(PSTR("%08lx: %02x %02x %02x %02x %02x %02x %02x %02x\n"),
                         offset,
                         buffer[0],
                         buffer[1],
                         buffer[2],
                         buffer[3],
                         buffer[4],
                         buffer[5],
                         buffer[6],
                         buffer[7]
                        );
                offset += 8;
            }

            fat16_close_file(fd);
        }
#if FAT16_WRITE_SUPPORT
        else if(strncmp_P(command, PSTR("rm "), 3) == 0)
        {
            command += 3;
            if(command[0] == '\0')
                continue;
            
            struct fat16_dir_entry_struct file_entry;
            if(find_file_in_dir(fs, dd, command, &file_entry))
            {
                if(fat16_delete_file(fs, &file_entry))
                    continue;
            }

            printf_P(PSTR("error deleting file: %s\n"), command);
        }
        else if(strncmp_P(command, PSTR("touch "), 6) == 0)
        {
            command += 6;
            if(command[0] == '\0')
                continue;

            struct fat16_dir_entry_struct file_entry;
            if(!fat16_create_file(dd, command, &file_entry))
                printf_P(PSTR("error creating file: %s\n"), command);
        }
        else if(strncmp_P(command, PSTR("write "), 6) == 0)
        {
            command += 6;
            if(command[0] == '\0')
                continue;

            char* offset_value = command;
            while(*offset_value != ' ' && *offset_value != '\0')
                ++offset_value;

            if(*offset_value == ' ')
                *offset_value++ = '\0';
            else
                continue;

            /* search file in current directory and open it */
            struct fat16_file_struct* fd = open_file_in_dir(fs, dd, command);
            if(!fd)
            {
                printf_P(PSTR("error opening %s\n"), command);
                continue;
            }

            int32_t offset = strtol(offset_value, 0, 0);
            if(!fat16_seek_file(fd, &offset, FAT16_SEEK_SET))
            {
                printf_P(PSTR("error seeking on %s\n"), command);
                fat16_close_file(fd);
                continue;
            }

            /* read text from the shell and write it to the file */
            uint8_t data_len;
            while(1)
            {
                /* give a different prompt */
                uart_putc('<');
                uart_putc(' ');

                /* read one line of text */
                data_len = read_line(buffer, sizeof(buffer));
                if(!data_len)
                    break;

                /* write text to file */
                if(fat16_write_file(fd, (uint8_t*) buffer, data_len) != data_len)
                {
                    printf_P(PSTR("error writing to file\n"));
                    break;
                }
            }

            fat16_close_file(fd);
        }
#endif
#if SD_RAW_WRITE_BUFFERING
        else if(strcmp_P(command, PSTR("sync")) == 0)
        {
            if(!sd_raw_sync())
                printf_P(PSTR("error syncing disk\n"));
        }
#endif
        else
        {
            printf_P(PSTR("unknown command: %s\n"), command);
        }
    }

    /* close file system */
    fat16_close(fs);

    /* close partition */
    partition_close(partition);
    
    return 0;
}
Пример #5
0
//0 for success, negative for failure
int readLogCon(void) {
  char keyword[64];
  char value[64];                            
  char stringBuf[512];

  struct fat16_file_struct fd;
  int len;
  int s=0;
  int t=0;
  keyword[0]=0;
  value[0]=0;
  int result;
  if(root_file_exists(LOGCON_NAME)) {
    //There already is a logcon file, open it and suck int in
    result = root_open(&fd,LOGCON_NAME);
    
    if(result<0) return result;
    len = fat16_read_file(&fd, (unsigned char *)stringBuf, 512);
    if(len<0) return -1;
    fat16_close_file(&fd);
  } else {
    //No existing logcon file, write a fresh one
    result=root_open_new(&fd,LOGCON_NAME);
    if(result<0) return result;
    stringBuf[0]=0;

    //Write out each default line
    for(int i=0;strlen(fTable[i].tag)>0;i++) {
      strcat(stringBuf,fTable[i].tag);
      strcat(stringBuf,"=");
      strcat(stringBuf,fTable[i].def);
      strcat(stringBuf,"\r\n");
    }

    //write it out to the file
    len=strlen(stringBuf);
    fat16_write_file(&fd, (unsigned char*)stringBuf, len);
    fat16_close_file(&fd);
    sd_raw_sync();
  }

  //Either way, the logcon file is now in stringBuf and
  //its length is in len
  
  s=0;
  t=0;
  char inValue=0;
  for(s = 0; s < len; s++) {
    if(stringBuf[s] == '=') {
      keyword[t]=0;
      inValue=1;
      t=0;
    } else if(stringBuf[s]=='\n') {
      value[t]=0;
      inValue=0;
      t=0;
      result=processLine(keyword,value);
      if(result<0) return result;
    } else {
      if(inValue) {
        value[t]=stringBuf[s];
      } else {
        keyword[t]=stringBuf[s];
      }
      t++;
    }
  }
  return 0;
}