]> pilppa.org Git - familiar-h63xx-build.git/blob - org.handhelds.familiar/packages/simpad-utilities/serload/main.cpp
OE tree imported from monotone branch org.openembedded.oz354fam083 at revision 8b12e3...
[familiar-h63xx-build.git] / org.handhelds.familiar / packages / simpad-utilities / serload / main.cpp
1 //=============================================================================
2 // Project:      SIMpad
3 //=============================================================================
4 // FILE-NAME:    main.cpp
5 // FUNCTION:     Serial download of a new image from PC to SIMpad
6 //
7 // AUTHOR:       Juergen Messerer, Peter Voser
8 // CREAT.-DATE:  01.04.2001 (dd.mm.yy)
9 //
10 // NOTES:        -
11 //
12 //=============================================================================
13
14 #include <unistd.h>
15 #include <iostream>
16 #include <string.h>
17 #include "serialdownload.h"
18 using namespace std;
19
20 const int STRING_LENGTH = 128;
21
22 //=============================================================================
23 //=============================================================================
24 void printHelp(void)
25 {
26     cout << "serload V1.0 for Linux " << endl;
27     cout << "Downloading a new image to the SIMpad " 
28          << "using the serial interface." << endl << endl;
29     cout << "Invocation: serload [IMAGEFILE] [ttyS-PORT]" << endl;
30     cout << "IMAGEFILE: the file with the new image prepared for the"
31          << " SIMpad Bootloader." 
32          << endl;
33     cout << "ttyS-PORT: number of the ttyS-Port for the download." 
34          << endl;
35     cout << "Note: ttyS0 = COM1, ttyS1 = COM2, ..." << endl << endl;
36 }
37
38 //=============================================================================
39 //=============================================================================
40 int main(int argc, char *argv[])
41 {
42     int i;
43     for(i = 0; i < argc; ++i)
44     {
45         if(strcmp(argv[i], "--help") == 0)
46         {
47             // The user asks for help.
48             printHelp(); 
49             exit(0);
50         }
51     }
52
53     if(argc != 3 && argc != 2)
54     {
55         cerr << endl << "Usage: serload [IMAGEFILE] [ttyS-PORT]" << endl; 
56         cerr << "See also \"serload --help\"!" << endl << endl;
57         exit(1);
58     }
59
60     char device[STRING_LENGTH];
61     if(argc == 3)
62     {
63         strcpy(device, "/dev/ttyS");
64         strcat(device, argv[2]);
65     }
66     else
67     {
68         // If no serial port is given, use ttyS0 as default.
69         strcpy(device, "/dev/ttyS0");
70     }
71
72     SerialDownload serload;
73     int myError, imagesize;
74     static char *image;
75
76     int success = serload.openSerialPort(device, myError);
77     if(success != 0)
78     {
79         cerr << "Error: cannot open " << device << ". Aborting." 
80              << endl << endl;
81         exit(2);
82     }
83
84     myError = serload.loadFile(argv[1], image, imagesize);
85     if(myError != 0)
86     {
87         cerr << "Error: cannot load file " 
88              << argv[1] << "! Aborting." << endl << endl;
89         exit(3);
90     }
91
92     cout << "Please press RESET at the back of the SIMpad!" << endl;
93     int reply = serload.connectToSimpad(115200, myError);
94
95     if(reply != 0)
96     {
97         cerr << "Error: cannot connect to SIMpad! Aborting." 
98              << endl << endl;
99         exit(4);
100     }
101   
102     // Determine number of blocks to send without remaining bytes!
103     int progress = 0;
104     int size = imagesize;
105     int totalBlocks = size / 512;
106     int numberOfBlocksToSend = totalBlocks;
107     int numberOfBlocksSent = 0;
108
109     // Send blocks.
110     while(numberOfBlocksToSend)
111     {
112         serload.sendBlock(image, 512, myError);
113         image += 512;
114         --numberOfBlocksToSend;
115         // Update progress info every 100th block.
116         if(!(numberOfBlocksSent % 100))
117         {
118             progress = 100 * numberOfBlocksSent / totalBlocks;
119         }
120         ++numberOfBlocksSent;
121     }
122
123     // Determine, if there are remaining bytes.
124     int numberOfRemainingBytes = size % 512;
125     if(numberOfRemainingBytes)
126     {
127         serload.sendBlock(image, numberOfRemainingBytes, myError); 
128     }
129
130     // The bootloader burns the new image.
131     serload.waitForEndOfBurning();
132
133     cout << "Update successfully finished! Swich the SIMpad on." << endl;
134
135     return 0;
136 }