Changeset 2f90960 in freewrt


Ignore:
Timestamp:
Feb 27, 2007, 8:58:45 AM (19 years ago)
Author:
Waldemar Brodkorb <wbx@…>
Branches:
freewrt_1_0, freewrt_2_0
Children:
1334083d
Parents:
560055d
Message:

sync with trunk

git-svn-id: svn://www.freewrt.org/branches/freewrt_1_0@2104 afb5a338-a214-0410-bd46-81f09a774fd1

Location:
package/mtd
Files:
2 edited

Legend:

Unmodified
Added
Removed
  • package/mtd/Makefile

    r560055d r2f90960  
    99PKG_NAME:=              mtd
    1010PKG_VERSION:=           1.0
    11 PKG_RELEASE:=           5
     11PKG_RELEASE:=           6
    1212
    1313WRKSRC=                 ${WRKDIR}/$(PKG_NAME)
  • package/mtd/mtd.c

    r560055d r2f90960  
    22 * mtd - simple memory technology device manipulation tool
    33 *
    4  * Copyright (c) 2006 Thorsten Glaser <tg@freewrt.org>
     4 * Copyright (c) 2006, 2007 Thorsten Glaser <tg@freewrt.org>
    55 * Copyright (C) 2005 Waldemar Brodkorb <wbx@freewrt.org>,
    66 *                        Felix Fietkau <nbd@openwrt.org>
     
    3333#include <limits.h>
    3434#include <unistd.h>
     35#include <stdbool.h>
    3536#include <stdlib.h>
    3637#include <stdio.h>
     
    6364};
    6465
     66int image_check_bcom(int, const char *);
     67int image_check(int, const char *);
     68int mtd_check(char *);
     69int mtd_unlock(const char *);
     70int mtd_open(const char *, int);
     71int mtd_erase(const char *);
     72int mtd_write(int, const char *, int, bool);
     73void usage(void) __attribute__((noreturn));
     74
    6575char buf[BUFSIZE];
    6676int buflen;
     
    7585        buflen = read(imagefd, buf, 32);
    7686        if (buflen < 32) {
    77                 fprintf(stdout, "Could not get image header, file too small (%ld bytes)\n", buflen);
     87                fprintf(stdout, "Could not get image header, file too small (%d bytes)\n", buflen);
    7888                return 0;
    7989        }
     
    128138image_check(int imagefd, const char *mtd)
    129139{
    130         int fd, systype;
    131         size_t count;
     140        int systype;
    132141        char *c;
    133142        FILE *f;
     
    152161}
    153162
    154 int mtd_check(char *mtd)
     163int
     164mtd_check(char *mtd)
    155165{
    156166        struct mtd_info_user mtdInfo;
     
    263273
    264274int
    265 mtd_write(int imagefd, const char *mtd, int quiet)
    266 {
    267         int fd, i, result;
     275mtd_write(int imagefd, const char *mtd, int quiet, bool do_erase)
     276{
     277        int fd, result;
    268278        size_t r, w, e;
    269279        struct mtd_info_user mtdInfo;
    270280        struct erase_info_user mtdEraseInfo;
    271         int ret = 0;
    272281
    273282        fd = mtd_open(mtd, O_RDWR | O_SYNC);
     
    297306
    298307                /* need to erase the next block before writing data to it */
    299                 while (w > e) {
     308                while (do_erase && w > e) {
    300309                        mtdEraseInfo.start = e;
    301310                        mtdEraseInfo.length = mtdInfo.erasesize;
     
    314323                        fprintf(stderr, "\b\b\b[w]");
    315324
    316                 if ((result = write(fd, buf, r)) < r) {
     325                if ((result = write(fd, buf, r)) < (ssize_t)r) {
    317326                        if (result < 0) {
    318327                                fprintf(stderr, "Error writing image.\n");
     
    333342}
    334343
    335 void usage(void)
     344void
     345usage(void)
    336346{
    337347        fprintf(stderr, "Usage: mtd [<options> ...] <command> [<arguments> ...] <device>\n\n"
    338348        "The device is in the format of mtdX (eg: mtd4) or its label.\n"
    339         "mtd recognizes these commands:\n"
     349        "mtd recognises these commands:\n"
    340350        "        unlock                  unlock the device\n"
    341351        "        erase                   erase all data on device\n"
     
    352362}
    353363
    354 int main (int argc, char **argv)
    355 {
    356         int ch, i, boot, unlock, imagefd, force, quiet, unlocked;
    357         char *erase[MAX_ARGS], *device, *imagefile;
     364int
     365main(int argc, char **argv)
     366{
     367        int ch, i, boot, imagefd = -1, force, quiet, unlocked;
     368        char *erase[MAX_ARGS], *device, *imagefile = NULL;
    358369        enum {
    359370                CMD_ERASE,
     
    449460                mtd_erase(erase[i]);
    450461                if (strcmp(erase[i], device) == 0)
     462                        /* this means that <device> is unlocked and erased */
    451463                        unlocked = 1;
    452464                i++;
     
    463475                        break;
    464476                case CMD_ERASE:
     477                        if (unlocked) {
     478                                fprintf(stderr, "Already erased: %s\n", device);
     479                                break;
     480                        }
    465481                        if (quiet < 2)
    466482                                fprintf(stderr, "Erasing %s ...\n", device);
     
    470486                        if (quiet < 2)
    471487                                fprintf(stderr, "Writing from %s to %s ... ", imagefile, device);
    472                         mtd_write(imagefd, device, quiet);
     488                        mtd_write(imagefd, device, quiet, (unlocked == 0));
    473489                        if (quiet < 2)
    474490                                fprintf(stderr, "\n");
     
    479495
    480496        if (boot) {
     497                if (quiet < 2)
     498                        fprintf(stderr, "\nRebooting ... ");
    481499                fflush(stdout);
     500                fflush(stderr);
    482501                syscall(SYS_reboot,LINUX_REBOOT_MAGIC1,LINUX_REBOOT_MAGIC2,LINUX_REBOOT_CMD_RESTART,NULL);
    483502        }
Note: See TracChangeset for help on using the changeset viewer.