├── .gitignore ├── Makefile ├── unyaffs.h ├── README ├── gpl-2.0.txt └── unyaffs.c /.gitignore: -------------------------------------------------------------------------------- 1 | unyaffs 2 | -------------------------------------------------------------------------------- /Makefile: -------------------------------------------------------------------------------- 1 | CFLAGS = -O2 -Wall 2 | 3 | unyaffs: unyaffs.c unyaffs.h 4 | $(CC) $(CFLAGS) $(LDFLAGS) unyaffs.c -o unyaffs 5 | -------------------------------------------------------------------------------- /unyaffs.h: -------------------------------------------------------------------------------- 1 | /* 2 | * definition copied from yaffs project 3 | */ 4 | 5 | #ifndef __UNYAFFS_H__ 6 | #define __UNYAFFS_H__ 7 | 8 | 9 | #define YAFFS_MAX_NAME_LENGTH 255 10 | #define YAFFS_MAX_ALIAS_LENGTH 159 11 | 12 | /* Definition of types */ 13 | typedef unsigned char __u8; 14 | typedef unsigned short __u16; 15 | typedef unsigned __u32; 16 | 17 | typedef struct { 18 | unsigned sequenceNumber; 19 | unsigned objectId; 20 | unsigned chunkId; 21 | unsigned byteCount; 22 | } yaffs_PackedTags2TagsPart; 23 | 24 | typedef struct { 25 | unsigned char colParity; 26 | unsigned lineParity; 27 | unsigned lineParityPrime; 28 | } yaffs_ECCOther; 29 | 30 | typedef struct { 31 | yaffs_PackedTags2TagsPart t; 32 | yaffs_ECCOther ecc; 33 | } yaffs_PackedTags2; 34 | 35 | typedef enum { 36 | YAFFS_OBJECT_TYPE_UNKNOWN, 37 | YAFFS_OBJECT_TYPE_FILE, 38 | YAFFS_OBJECT_TYPE_SYMLINK, 39 | YAFFS_OBJECT_TYPE_DIRECTORY, 40 | YAFFS_OBJECT_TYPE_HARDLINK, 41 | YAFFS_OBJECT_TYPE_SPECIAL 42 | } yaffs_ObjectType; 43 | 44 | 45 | /* -------------------------- Object structure -------------------------------*/ 46 | /* This is the object structure as stored on NAND */ 47 | 48 | typedef struct { 49 | yaffs_ObjectType type; 50 | 51 | /* Apply to everything */ 52 | int parentObjectId; 53 | __u16 sum__NoLongerUsed; /* checksum of name. No longer used */ 54 | char name[YAFFS_MAX_NAME_LENGTH + 1]; 55 | 56 | /* The following apply to directories, files, symlinks - not hard links */ 57 | __u32 yst_mode; /* protection */ 58 | 59 | #ifdef CONFIG_YAFFS_WINCE 60 | __u32 notForWinCE[5]; 61 | #else 62 | __u32 yst_uid; 63 | __u32 yst_gid; 64 | __u32 yst_atime; 65 | __u32 yst_mtime; 66 | __u32 yst_ctime; 67 | #endif 68 | 69 | /* File size applies to files only */ 70 | int fileSize; 71 | 72 | /* Equivalent object id applies to hard links only. */ 73 | int equivalentObjectId; 74 | 75 | /* Alias is for symlinks only. */ 76 | char alias[YAFFS_MAX_ALIAS_LENGTH + 1]; 77 | 78 | __u32 yst_rdev; /* device stuff for block and char devices (major/min) */ 79 | 80 | #ifdef CONFIG_YAFFS_WINCE 81 | __u32 win_ctime[2]; 82 | __u32 win_atime[2]; 83 | __u32 win_mtime[2]; 84 | #else 85 | __u32 roomToGrow[6]; 86 | 87 | #endif 88 | __u32 inbandShadowsObject; 89 | __u32 inbandIsShrink; 90 | 91 | __u32 reservedSpace[2]; 92 | int shadowsObject; /* This object header shadows the specified object if > 0 */ 93 | 94 | /* isShrink applies to object headers written when we shrink the file (ie resize) */ 95 | __u32 isShrink; 96 | 97 | } yaffs_ObjectHeader; 98 | 99 | #endif 100 | -------------------------------------------------------------------------------- /README: -------------------------------------------------------------------------------- 1 | unyaffs 2 | ======= 3 | 4 | Overview 5 | -------- 6 | 7 | Unyaffs is a program to extract files from a YAFFS2 file system image. 8 | Currently it can only extract images created by mkyaffs2image. 9 | 10 | Unyaffs is based on work of the YAFFS project, see http://www.yaffs.net/ 11 | 12 | The original implementation was done by Kai Wei , 13 | it is available at http://code.google.com/p/unyaffs/ 14 | 15 | As the project was inactive, Bernhard Ehlers 16 | started a fork at https://github.com/ehlers/unyaffs 17 | 18 | 19 | License 20 | ------- 21 | 22 | This program is free software; you can redistribute it and/or modify 23 | it under the terms of the GNU General Public License version 2 as 24 | published by the Free Software Foundation. 25 | 26 | The full license text is available in the file gpl-2.0.txt. 27 | 28 | 29 | Requirements 30 | ------------ 31 | 32 | Unyaffs should run on most unix-like operating systems. 33 | A C compiler environment and make is neccessary to compile it. 34 | 35 | 36 | Compiling 37 | --------- 38 | 39 | type "make" 40 | 41 | 42 | Usage 43 | ----- 44 | 45 | unyaffs - extract files from a YAFFS2 file system image. 46 | 47 | Usage: unyaffs [options] [] 48 | 49 | Options: 50 | -d detection of flash layout, no extraction 51 | -b spare contains bad block information 52 | -c set chunk size in KByte (default: autodetect, max: 16) 53 | -s set spare size in Byte (default: autodetect, max: 512) 54 | -t list image contents 55 | -v verbose output 56 | -V print version 57 | 58 | In most cases the flash memory layout is detected automatically. 59 | If the detection doesn't work properly, the layout can be set with 60 | options -c and -s. If spare contains bad block info, set option -b. 61 | 62 | Option -t lists all the file names in the image without extracting them. 63 | Option -v generates a 'ls -l' like listing of the extracted files. 64 | When combined with -t it generates an extended file listing, nothing 65 | is extracted. 66 | 67 | The image file can be - for standard input. 68 | 69 | If the extraction directory is not given, the files are stored into the 70 | current directory. If the extraction dir doesn't exist, it will be created. 71 | 72 | unyaffs extracts all file types (regular files, directories, soft links, 73 | hard links and special files). The permissions and modification dates 74 | are restored. When run as root, the ownership is restored as well. 75 | 76 | Please be careful extracting images as root. It might be possible, 77 | that important system files get overwritten. The use of chroot 78 | or fakeroot can guard against these problems. 79 | 80 | Limitations 81 | ----------- 82 | 83 | Block and character devices can only be restored as root. In some 84 | operating systems (e.g. Mac OS X) sockets can't be restored. 85 | 86 | The use of special files (block devices, character devices, pipes, 87 | sockets) is not portable across different unix flavors. E.g. device 88 | files created on linux will get a strange major and minor device id, 89 | when restored under BSD. 90 | 91 | When it's necessary to deal with special files, please use the same 92 | OS as used in the system the image is build for. In most cases that's 93 | linux. 94 | 95 | Windows 96 | ------- 97 | 98 | The differences between the unix and windows file system are big enough 99 | to make a windows port of unyaffs a complicated task. But with cygwin 100 | (http://www.cygwin.com/) a unix compatibility layer for windows is 101 | available. The unmodified source compiles and runs nicely within cygwin. 102 | 103 | The main differences to a pure unix environment: 104 | - Unix functionality only within cygwin environment, native windows 105 | programs won't see the more advanced features. 106 | - Permissions and file dates/time are correctly restored 107 | - Owner/group are not fully restored, only user/groups available in windows 108 | - Hard and soft links are correctly restored 109 | - Device files have different ids as in linux 110 | - Sockets are not restored 111 | 112 | For analysis of a firmware image, unyaffs within cygwin is a nice tool, 113 | but because of the restricted user/group functionality rebuilding a 114 | modified image can't be recommended. 115 | -------------------------------------------------------------------------------- /gpl-2.0.txt: -------------------------------------------------------------------------------- 1 | GNU GENERAL PUBLIC LICENSE 2 | Version 2, June 1991 3 | 4 | Copyright (C) 1989, 1991 Free Software Foundation, Inc., 5 | 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA 6 | Everyone is permitted to copy and distribute verbatim copies 7 | of this license document, but changing it is not allowed. 8 | 9 | Preamble 10 | 11 | The licenses for most software are designed to take away your 12 | freedom to share and change it. By contrast, the GNU General Public 13 | License is intended to guarantee your freedom to share and change free 14 | software--to make sure the software is free for all its users. This 15 | General Public License applies to most of the Free Software 16 | Foundation's software and to any other program whose authors commit to 17 | using it. (Some other Free Software Foundation software is covered by 18 | the GNU Lesser General Public License instead.) You can apply it to 19 | your programs, too. 20 | 21 | When we speak of free software, we are referring to freedom, not 22 | price. Our General Public Licenses are designed to make sure that you 23 | have the freedom to distribute copies of free software (and charge for 24 | this service if you wish), that you receive source code or can get it 25 | if you want it, that you can change the software or use pieces of it 26 | in new free programs; and that you know you can do these things. 27 | 28 | To protect your rights, we need to make restrictions that forbid 29 | anyone to deny you these rights or to ask you to surrender the rights. 30 | These restrictions translate to certain responsibilities for you if you 31 | distribute copies of the software, or if you modify it. 32 | 33 | For example, if you distribute copies of such a program, whether 34 | gratis or for a fee, you must give the recipients all the rights that 35 | you have. You must make sure that they, too, receive or can get the 36 | source code. And you must show them these terms so they know their 37 | rights. 38 | 39 | We protect your rights with two steps: (1) copyright the software, and 40 | (2) offer you this license which gives you legal permission to copy, 41 | distribute and/or modify the software. 42 | 43 | Also, for each author's protection and ours, we want to make certain 44 | that everyone understands that there is no warranty for this free 45 | software. If the software is modified by someone else and passed on, we 46 | want its recipients to know that what they have is not the original, so 47 | that any problems introduced by others will not reflect on the original 48 | authors' reputations. 49 | 50 | Finally, any free program is threatened constantly by software 51 | patents. We wish to avoid the danger that redistributors of a free 52 | program will individually obtain patent licenses, in effect making the 53 | program proprietary. To prevent this, we have made it clear that any 54 | patent must be licensed for everyone's free use or not licensed at all. 55 | 56 | The precise terms and conditions for copying, distribution and 57 | modification follow. 58 | 59 | GNU GENERAL PUBLIC LICENSE 60 | TERMS AND CONDITIONS FOR COPYING, DISTRIBUTION AND MODIFICATION 61 | 62 | 0. This License applies to any program or other work which contains 63 | a notice placed by the copyright holder saying it may be distributed 64 | under the terms of this General Public License. The "Program", below, 65 | refers to any such program or work, and a "work based on the Program" 66 | means either the Program or any derivative work under copyright law: 67 | that is to say, a work containing the Program or a portion of it, 68 | either verbatim or with modifications and/or translated into another 69 | language. (Hereinafter, translation is included without limitation in 70 | the term "modification".) Each licensee is addressed as "you". 71 | 72 | Activities other than copying, distribution and modification are not 73 | covered by this License; they are outside its scope. The act of 74 | running the Program is not restricted, and the output from the Program 75 | is covered only if its contents constitute a work based on the 76 | Program (independent of having been made by running the Program). 77 | Whether that is true depends on what the Program does. 78 | 79 | 1. You may copy and distribute verbatim copies of the Program's 80 | source code as you receive it, in any medium, provided that you 81 | conspicuously and appropriately publish on each copy an appropriate 82 | copyright notice and disclaimer of warranty; keep intact all the 83 | notices that refer to this License and to the absence of any warranty; 84 | and give any other recipients of the Program a copy of this License 85 | along with the Program. 86 | 87 | You may charge a fee for the physical act of transferring a copy, and 88 | you may at your option offer warranty protection in exchange for a fee. 89 | 90 | 2. You may modify your copy or copies of the Program or any portion 91 | of it, thus forming a work based on the Program, and copy and 92 | distribute such modifications or work under the terms of Section 1 93 | above, provided that you also meet all of these conditions: 94 | 95 | a) You must cause the modified files to carry prominent notices 96 | stating that you changed the files and the date of any change. 97 | 98 | b) You must cause any work that you distribute or publish, that in 99 | whole or in part contains or is derived from the Program or any 100 | part thereof, to be licensed as a whole at no charge to all third 101 | parties under the terms of this License. 102 | 103 | c) If the modified program normally reads commands interactively 104 | when run, you must cause it, when started running for such 105 | interactive use in the most ordinary way, to print or display an 106 | announcement including an appropriate copyright notice and a 107 | notice that there is no warranty (or else, saying that you provide 108 | a warranty) and that users may redistribute the program under 109 | these conditions, and telling the user how to view a copy of this 110 | License. (Exception: if the Program itself is interactive but 111 | does not normally print such an announcement, your work based on 112 | the Program is not required to print an announcement.) 113 | 114 | These requirements apply to the modified work as a whole. If 115 | identifiable sections of that work are not derived from the Program, 116 | and can be reasonably considered independent and separate works in 117 | themselves, then this License, and its terms, do not apply to those 118 | sections when you distribute them as separate works. But when you 119 | distribute the same sections as part of a whole which is a work based 120 | on the Program, the distribution of the whole must be on the terms of 121 | this License, whose permissions for other licensees extend to the 122 | entire whole, and thus to each and every part regardless of who wrote it. 123 | 124 | Thus, it is not the intent of this section to claim rights or contest 125 | your rights to work written entirely by you; rather, the intent is to 126 | exercise the right to control the distribution of derivative or 127 | collective works based on the Program. 128 | 129 | In addition, mere aggregation of another work not based on the Program 130 | with the Program (or with a work based on the Program) on a volume of 131 | a storage or distribution medium does not bring the other work under 132 | the scope of this License. 133 | 134 | 3. You may copy and distribute the Program (or a work based on it, 135 | under Section 2) in object code or executable form under the terms of 136 | Sections 1 and 2 above provided that you also do one of the following: 137 | 138 | a) Accompany it with the complete corresponding machine-readable 139 | source code, which must be distributed under the terms of Sections 140 | 1 and 2 above on a medium customarily used for software interchange; or, 141 | 142 | b) Accompany it with a written offer, valid for at least three 143 | years, to give any third party, for a charge no more than your 144 | cost of physically performing source distribution, a complete 145 | machine-readable copy of the corresponding source code, to be 146 | distributed under the terms of Sections 1 and 2 above on a medium 147 | customarily used for software interchange; or, 148 | 149 | c) Accompany it with the information you received as to the offer 150 | to distribute corresponding source code. (This alternative is 151 | allowed only for noncommercial distribution and only if you 152 | received the program in object code or executable form with such 153 | an offer, in accord with Subsection b above.) 154 | 155 | The source code for a work means the preferred form of the work for 156 | making modifications to it. For an executable work, complete source 157 | code means all the source code for all modules it contains, plus any 158 | associated interface definition files, plus the scripts used to 159 | control compilation and installation of the executable. However, as a 160 | special exception, the source code distributed need not include 161 | anything that is normally distributed (in either source or binary 162 | form) with the major components (compiler, kernel, and so on) of the 163 | operating system on which the executable runs, unless that component 164 | itself accompanies the executable. 165 | 166 | If distribution of executable or object code is made by offering 167 | access to copy from a designated place, then offering equivalent 168 | access to copy the source code from the same place counts as 169 | distribution of the source code, even though third parties are not 170 | compelled to copy the source along with the object code. 171 | 172 | 4. You may not copy, modify, sublicense, or distribute the Program 173 | except as expressly provided under this License. Any attempt 174 | otherwise to copy, modify, sublicense or distribute the Program is 175 | void, and will automatically terminate your rights under this License. 176 | However, parties who have received copies, or rights, from you under 177 | this License will not have their licenses terminated so long as such 178 | parties remain in full compliance. 179 | 180 | 5. You are not required to accept this License, since you have not 181 | signed it. However, nothing else grants you permission to modify or 182 | distribute the Program or its derivative works. These actions are 183 | prohibited by law if you do not accept this License. Therefore, by 184 | modifying or distributing the Program (or any work based on the 185 | Program), you indicate your acceptance of this License to do so, and 186 | all its terms and conditions for copying, distributing or modifying 187 | the Program or works based on it. 188 | 189 | 6. Each time you redistribute the Program (or any work based on the 190 | Program), the recipient automatically receives a license from the 191 | original licensor to copy, distribute or modify the Program subject to 192 | these terms and conditions. You may not impose any further 193 | restrictions on the recipients' exercise of the rights granted herein. 194 | You are not responsible for enforcing compliance by third parties to 195 | this License. 196 | 197 | 7. If, as a consequence of a court judgment or allegation of patent 198 | infringement or for any other reason (not limited to patent issues), 199 | conditions are imposed on you (whether by court order, agreement or 200 | otherwise) that contradict the conditions of this License, they do not 201 | excuse you from the conditions of this License. If you cannot 202 | distribute so as to satisfy simultaneously your obligations under this 203 | License and any other pertinent obligations, then as a consequence you 204 | may not distribute the Program at all. For example, if a patent 205 | license would not permit royalty-free redistribution of the Program by 206 | all those who receive copies directly or indirectly through you, then 207 | the only way you could satisfy both it and this License would be to 208 | refrain entirely from distribution of the Program. 209 | 210 | If any portion of this section is held invalid or unenforceable under 211 | any particular circumstance, the balance of the section is intended to 212 | apply and the section as a whole is intended to apply in other 213 | circumstances. 214 | 215 | It is not the purpose of this section to induce you to infringe any 216 | patents or other property right claims or to contest validity of any 217 | such claims; this section has the sole purpose of protecting the 218 | integrity of the free software distribution system, which is 219 | implemented by public license practices. Many people have made 220 | generous contributions to the wide range of software distributed 221 | through that system in reliance on consistent application of that 222 | system; it is up to the author/donor to decide if he or she is willing 223 | to distribute software through any other system and a licensee cannot 224 | impose that choice. 225 | 226 | This section is intended to make thoroughly clear what is believed to 227 | be a consequence of the rest of this License. 228 | 229 | 8. If the distribution and/or use of the Program is restricted in 230 | certain countries either by patents or by copyrighted interfaces, the 231 | original copyright holder who places the Program under this License 232 | may add an explicit geographical distribution limitation excluding 233 | those countries, so that distribution is permitted only in or among 234 | countries not thus excluded. In such case, this License incorporates 235 | the limitation as if written in the body of this License. 236 | 237 | 9. The Free Software Foundation may publish revised and/or new versions 238 | of the General Public License from time to time. Such new versions will 239 | be similar in spirit to the present version, but may differ in detail to 240 | address new problems or concerns. 241 | 242 | Each version is given a distinguishing version number. If the Program 243 | specifies a version number of this License which applies to it and "any 244 | later version", you have the option of following the terms and conditions 245 | either of that version or of any later version published by the Free 246 | Software Foundation. If the Program does not specify a version number of 247 | this License, you may choose any version ever published by the Free Software 248 | Foundation. 249 | 250 | 10. If you wish to incorporate parts of the Program into other free 251 | programs whose distribution conditions are different, write to the author 252 | to ask for permission. For software which is copyrighted by the Free 253 | Software Foundation, write to the Free Software Foundation; we sometimes 254 | make exceptions for this. Our decision will be guided by the two goals 255 | of preserving the free status of all derivatives of our free software and 256 | of promoting the sharing and reuse of software generally. 257 | 258 | NO WARRANTY 259 | 260 | 11. BECAUSE THE PROGRAM IS LICENSED FREE OF CHARGE, THERE IS NO WARRANTY 261 | FOR THE PROGRAM, TO THE EXTENT PERMITTED BY APPLICABLE LAW. EXCEPT WHEN 262 | OTHERWISE STATED IN WRITING THE COPYRIGHT HOLDERS AND/OR OTHER PARTIES 263 | PROVIDE THE PROGRAM "AS IS" WITHOUT WARRANTY OF ANY KIND, EITHER EXPRESSED 264 | OR IMPLIED, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF 265 | MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE. THE ENTIRE RISK AS 266 | TO THE QUALITY AND PERFORMANCE OF THE PROGRAM IS WITH YOU. SHOULD THE 267 | PROGRAM PROVE DEFECTIVE, YOU ASSUME THE COST OF ALL NECESSARY SERVICING, 268 | REPAIR OR CORRECTION. 269 | 270 | 12. IN NO EVENT UNLESS REQUIRED BY APPLICABLE LAW OR AGREED TO IN WRITING 271 | WILL ANY COPYRIGHT HOLDER, OR ANY OTHER PARTY WHO MAY MODIFY AND/OR 272 | REDISTRIBUTE THE PROGRAM AS PERMITTED ABOVE, BE LIABLE TO YOU FOR DAMAGES, 273 | INCLUDING ANY GENERAL, SPECIAL, INCIDENTAL OR CONSEQUENTIAL DAMAGES ARISING 274 | OUT OF THE USE OR INABILITY TO USE THE PROGRAM (INCLUDING BUT NOT LIMITED 275 | TO LOSS OF DATA OR DATA BEING RENDERED INACCURATE OR LOSSES SUSTAINED BY 276 | YOU OR THIRD PARTIES OR A FAILURE OF THE PROGRAM TO OPERATE WITH ANY OTHER 277 | PROGRAMS), EVEN IF SUCH HOLDER OR OTHER PARTY HAS BEEN ADVISED OF THE 278 | POSSIBILITY OF SUCH DAMAGES. 279 | 280 | END OF TERMS AND CONDITIONS 281 | 282 | How to Apply These Terms to Your New Programs 283 | 284 | If you develop a new program, and you want it to be of the greatest 285 | possible use to the public, the best way to achieve this is to make it 286 | free software which everyone can redistribute and change under these terms. 287 | 288 | To do so, attach the following notices to the program. It is safest 289 | to attach them to the start of each source file to most effectively 290 | convey the exclusion of warranty; and each file should have at least 291 | the "copyright" line and a pointer to where the full notice is found. 292 | 293 | 294 | Copyright (C) 295 | 296 | This program is free software; you can redistribute it and/or modify 297 | it under the terms of the GNU General Public License as published by 298 | the Free Software Foundation; either version 2 of the License, or 299 | (at your option) any later version. 300 | 301 | This program is distributed in the hope that it will be useful, 302 | but WITHOUT ANY WARRANTY; without even the implied warranty of 303 | MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 304 | GNU General Public License for more details. 305 | 306 | You should have received a copy of the GNU General Public License along 307 | with this program; if not, write to the Free Software Foundation, Inc., 308 | 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA. 309 | 310 | Also add information on how to contact you by electronic and paper mail. 311 | 312 | If the program is interactive, make it output a short notice like this 313 | when it starts in an interactive mode: 314 | 315 | Gnomovision version 69, Copyright (C) year name of author 316 | Gnomovision comes with ABSOLUTELY NO WARRANTY; for details type `show w'. 317 | This is free software, and you are welcome to redistribute it 318 | under certain conditions; type `show c' for details. 319 | 320 | The hypothetical commands `show w' and `show c' should show the appropriate 321 | parts of the General Public License. Of course, the commands you use may 322 | be called something other than `show w' and `show c'; they could even be 323 | mouse-clicks or menu items--whatever suits your program. 324 | 325 | You should also get your employer (if you work as a programmer) or your 326 | school, if any, to sign a "copyright disclaimer" for the program, if 327 | necessary. Here is a sample; alter the names: 328 | 329 | Yoyodyne, Inc., hereby disclaims all copyright interest in the program 330 | `Gnomovision' (which makes passes at compilers) written by James Hacker. 331 | 332 | , 1 April 1989 333 | Ty Coon, President of Vice 334 | 335 | This General Public License does not permit incorporating your program into 336 | proprietary programs. If your program is a subroutine library, you may 337 | consider it more useful to permit linking proprietary applications with the 338 | library. If this is what you want to do, use the GNU Lesser General 339 | Public License instead of this License. 340 | -------------------------------------------------------------------------------- /unyaffs.c: -------------------------------------------------------------------------------- 1 | /* 2 | * unyaffs: extract files from yaffs2 file system image to current directory 3 | * 4 | * Created by Kai Wei 5 | * Modified by Bernhard Ehlers 6 | * 7 | * This program is free software; you can redistribute it and/or modify 8 | * it under the terms of the GNU General Public License version 2 as 9 | * published by the Free Software Foundation. 10 | */ 11 | 12 | /* 13 | * History: 14 | * V0.1 2008-12-29 15 | * Initial version uploaded to http://code.google.com/p/unyaffs/ 16 | * V0.8 2011-08-25 17 | * Fork created at https://github.com/ehlers/unyaffs 18 | * Support of chunksizes from 2k to 16k 19 | * Restore special files (device nodes) 20 | * Restore file date and time 21 | * Restore file ownership, when run as root 22 | * File listing 23 | * Much more error checking 24 | * V0.9 2011-09-03 25 | * Allow - as filename for stdin 26 | * Optional base dir for file extraction 27 | * V0.9.1 2012-04-12 28 | * Additional flash layout: 8k chunk size, 448 byte spare 29 | * V0.9.2 2012-04-18 30 | * Additional flash layout: 8k chunk size, 368 byte spare 31 | * New options -c and -s to set chunk and spare size, obsoletes option -l 32 | * Add support for bad block information in spare header. 33 | * Allow first data chunk to be stored in image before file header. 34 | * V0.9.3 2012-04-30 35 | * check result of lchown system call 36 | * Code cleanup 37 | * V0.9.4 2012-05-01 38 | * No predefined flash layouts, detect all possible layouts 39 | * Option -d shows detected flash layout, no extraction 40 | */ 41 | 42 | #define VERSION "0.9.4" 43 | 44 | /* check if lutimes is available */ 45 | #if defined(__linux__) || defined(__FreeBSD__) || defined(__NetBSD__) || (defined(__APPLE__) && defined(__MACH__)) 46 | #define HAS_LUTIMES 1 47 | #endif 48 | 49 | #include 50 | #include 51 | #include 52 | #include 53 | #include 54 | #include 55 | #include 56 | #include 57 | #include 58 | #include 59 | #include 60 | #ifdef HAS_LUTIMES 61 | #include 62 | #else 63 | #include 64 | #endif 65 | 66 | #include "unyaffs.h" 67 | 68 | #define MIN_CHUNK_SIZE 2048 69 | #define MAX_CHUNK_SIZE 16384 70 | #define MIN_SPARE_SIZE 64 71 | #define MAX_SPARE_SIZE 512 72 | #define HASH_SIZE 7001 73 | #define MAX_WARN 20 74 | #define YAFFS_OBJECTID_ROOT 1 75 | 76 | #define STD_PERMS (S_IRWXU|S_IRWXG|S_IRWXO) 77 | #define EXTRA_PERMS (S_ISUID|S_ISGID|S_ISVTX) 78 | 79 | unsigned char data[MAX_CHUNK_SIZE + MAX_SPARE_SIZE]; 80 | unsigned char buffer[4*(MAX_CHUNK_SIZE + MAX_SPARE_SIZE)]; 81 | unsigned char *chunk_data = data; 82 | unsigned char *spare_data = NULL; 83 | int chunk_size = MIN_CHUNK_SIZE; 84 | int spare_size = MIN_SPARE_SIZE; 85 | int spare_off = 0; 86 | int buf_len = 0; 87 | int buf_idx = 0; 88 | int chunk_no = 0; 89 | int warn_count = 0; 90 | int warn_chown = 0; 91 | int img_file; 92 | int opt_list; 93 | int opt_verbose; 94 | 95 | typedef struct _object { 96 | unsigned id; 97 | struct _object *next; 98 | yaffs_ObjectType type; 99 | unsigned prev_dir_id; 100 | __u32 atime; 101 | __u32 mtime; 102 | char path_name[1]; /* variable length, must be last */ 103 | } object; 104 | 105 | object *obj_list[HASH_SIZE]; 106 | 107 | unsigned last_dir_id; 108 | 109 | int set_utime(const char *filename, __u32 yst_atime, __u32 yst_mtime) { 110 | #ifdef HAS_LUTIMES 111 | struct timeval ftime[2]; 112 | 113 | ftime[0].tv_sec = yst_atime; 114 | ftime[0].tv_usec = 0; 115 | ftime[1].tv_sec = yst_mtime; 116 | ftime[1].tv_usec = 0; 117 | 118 | return lutimes(filename, ftime); 119 | #else 120 | struct utimbuf ftime; 121 | 122 | ftime.actime = yst_atime; 123 | ftime.modtime = yst_mtime; 124 | 125 | return utime(filename, &ftime); 126 | #endif 127 | } 128 | 129 | /* error reporting function, similar to GNU error() */ 130 | static void prt_err(int status, int errnum, const char *format, ...) { 131 | va_list varg; 132 | 133 | va_start(varg, format); 134 | fflush(stdout); 135 | vfprintf(stderr, format, varg); 136 | if (errnum != 0) 137 | fprintf(stderr, ": %s", strerror(errnum)); 138 | fprintf(stderr, "\n"); 139 | va_end(varg); 140 | 141 | if (status != 0) 142 | exit(status); 143 | } 144 | 145 | /* read function, which handles partial and interrupted reads */ 146 | ssize_t safe_read(int fd, void *buf, size_t len) { 147 | char *ptr = buf; 148 | ssize_t offset, ret; 149 | 150 | offset = 0; 151 | while (offset < len) { 152 | ret = read(fd, ptr+offset, len-offset); 153 | if (ret < 0) { 154 | if (errno != EAGAIN && errno != EINTR) 155 | return -1; 156 | } else if (ret == 0) 157 | break; 158 | else 159 | offset += ret; 160 | } 161 | return offset; 162 | } 163 | 164 | /* write function, which handles partial and interrupted writes */ 165 | ssize_t safe_write(int fd, void *buf, size_t len) { 166 | char *ptr = buf; 167 | ssize_t offset, ret; 168 | 169 | offset = 0; 170 | while (offset < len) { 171 | ret = write(fd, ptr+offset, len-offset); 172 | if (ret < 0) { 173 | if (errno != EAGAIN && errno != EINTR) 174 | return -1; 175 | } else if (ret == 0) 176 | break; 177 | else 178 | offset += ret; 179 | } 180 | return offset; 181 | } 182 | 183 | /* 184 | * mkdirpath - creates directories including intermediate dirs 185 | */ 186 | static int mkdirpath(const char *name, mode_t mode) { 187 | struct stat st; 188 | char *cp; 189 | char *buf; 190 | 191 | if ((buf = malloc(strlen(name)+1)) == NULL) 192 | { errno = ENOMEM; return -1; } 193 | strcpy(buf, name); 194 | cp = buf; 195 | while ((cp = strchr(cp, '/')) != NULL) { 196 | *cp = '\0'; 197 | mkdir(buf, 0755); 198 | *cp++ = '/'; 199 | } 200 | free(buf); 201 | 202 | if (mkdir(name, mode) < 0 && 203 | (stat(name, &st) < 0 || !S_ISDIR(st.st_mode))) 204 | return -1; 205 | 206 | return 0; 207 | } 208 | 209 | /* 210 | * save_lchown - call lchown and check result 211 | */ 212 | static void safe_lchown(const char *path, uid_t owner, gid_t group) { 213 | if (lchown(path, owner, group) < 0) { 214 | if (errno == EPERM || errno == EINVAL) 215 | warn_chown = 1; 216 | else 217 | prt_err(1, errno, "Can't chown %s", path); 218 | } 219 | } 220 | 221 | static void init_obj_list(void) { 222 | object *obj; 223 | unsigned idx; 224 | 225 | for (idx = 0; idx < HASH_SIZE; idx++) 226 | obj_list[idx] = NULL; 227 | last_dir_id = 0; 228 | 229 | obj = malloc(offsetof(object, path_name) + 2); 230 | if (obj == NULL) 231 | prt_err(1, 0, "Malloc struct object failed."); 232 | 233 | obj->id = YAFFS_OBJECTID_ROOT; 234 | obj->type = YAFFS_OBJECT_TYPE_DIRECTORY; 235 | obj->prev_dir_id = 0; 236 | obj->atime = obj->mtime = 0; 237 | strcpy(obj->path_name, "."); 238 | idx = obj->id % HASH_SIZE; 239 | obj->next = obj_list[idx]; 240 | obj_list[idx] = obj; 241 | } 242 | 243 | static object *get_object(unsigned id) { 244 | object *obj; 245 | 246 | obj = obj_list[id % HASH_SIZE]; 247 | while (obj != NULL && obj->id != id) 248 | obj = obj->next; 249 | return obj; 250 | } 251 | 252 | static object *add_object(yaffs_ObjectHeader *oh, yaffs_PackedTags2 *pt) { 253 | object *obj, *parent; 254 | unsigned idx; 255 | 256 | obj = get_object(pt->t.objectId); 257 | if (pt->t.objectId == YAFFS_OBJECTID_ROOT) { 258 | if (obj == NULL) 259 | prt_err(1, 0, "Missing root object"); 260 | if (oh->type != YAFFS_OBJECT_TYPE_DIRECTORY) 261 | prt_err(1, 0, "Root object must be directory"); 262 | if (last_dir_id == 0) 263 | last_dir_id = YAFFS_OBJECTID_ROOT; 264 | } else { 265 | if (oh->type != YAFFS_OBJECT_TYPE_FILE && 266 | oh->type != YAFFS_OBJECT_TYPE_DIRECTORY && 267 | oh->type != YAFFS_OBJECT_TYPE_SYMLINK && 268 | oh->type != YAFFS_OBJECT_TYPE_HARDLINK && 269 | oh->type != YAFFS_OBJECT_TYPE_SPECIAL && 270 | oh->type != YAFFS_OBJECT_TYPE_UNKNOWN) 271 | prt_err(1, 0, "Illegal type %d in object %u (%s)", 272 | oh->type, pt->t.objectId, oh->name); 273 | if (oh->name[0] == '\0' || strchr(oh->name, '/') != NULL || 274 | strcmp(oh->name, ".") == 0 || strcmp(oh->name, "..") == 0) 275 | prt_err(1, 0, "Illegal file name %s in object %u", 276 | oh->name, pt->t.objectId); 277 | if (obj != NULL) 278 | prt_err(1, 0, "Duplicate objectId %u", pt->t.objectId); 279 | parent = get_object(oh->parentObjectId); 280 | if (parent == NULL) 281 | prt_err(1, 0, "Invalid parentObjectId %u in object %u (%s)", 282 | oh->parentObjectId, pt->t.objectId, oh->name); 283 | if (parent->type != YAFFS_OBJECT_TYPE_DIRECTORY) 284 | prt_err(1, ENOTDIR, "File %s can't be created in %s", 285 | oh->name, parent->path_name); 286 | obj = malloc(offsetof(object, path_name) + 287 | strlen(parent->path_name) + strlen(oh->name) + 2); 288 | if (obj == NULL) 289 | prt_err(1, 0, "Malloc struct object failed."); 290 | 291 | obj->id = pt->t.objectId; 292 | obj->type = oh->type; 293 | if (obj->type == YAFFS_OBJECT_TYPE_DIRECTORY) { 294 | obj->prev_dir_id = last_dir_id; 295 | last_dir_id = obj->id; 296 | } else 297 | obj->prev_dir_id = 0; 298 | if (strcmp(parent->path_name, ".") == 0) { 299 | strcpy(obj->path_name, oh->name); 300 | } else { 301 | strcpy(obj->path_name, parent->path_name); 302 | strcat(obj->path_name, "/"); 303 | strcat(obj->path_name, oh->name); 304 | } 305 | idx = obj->id % HASH_SIZE; 306 | obj->next = obj_list[idx]; 307 | obj_list[idx] = obj; 308 | } 309 | 310 | obj->atime = oh->yst_atime; 311 | obj->mtime = oh->yst_mtime; 312 | 313 | return obj; 314 | } 315 | 316 | void set_dirs_utime(void) { 317 | unsigned id; 318 | object *obj; 319 | 320 | id = last_dir_id; 321 | while (id != 0 && (obj = get_object(id)) != NULL) { 322 | set_utime(obj->path_name, obj->atime, obj->mtime); 323 | id = obj->prev_dir_id; 324 | } 325 | } 326 | 327 | static void prt_node(char *name, yaffs_ObjectHeader *oh) { 328 | object *eq_obj; 329 | struct tm tm; 330 | time_t mtime; 331 | mode_t mode; 332 | char type; 333 | char fsize[16]; 334 | char perm[10]; 335 | 336 | /* get file type, size, mtine and mode */ 337 | eq_obj = NULL; 338 | strcpy(fsize, "0"); 339 | mtime = oh->yst_mtime; 340 | mode = oh->yst_mode; 341 | switch(oh->type) { 342 | case YAFFS_OBJECT_TYPE_FILE: type = '-'; 343 | snprintf(fsize, sizeof(fsize), "%d", oh->fileSize); 344 | break; 345 | case YAFFS_OBJECT_TYPE_DIRECTORY: type = 'd'; break; 346 | case YAFFS_OBJECT_TYPE_SYMLINK: type = 'l'; break; 347 | case YAFFS_OBJECT_TYPE_HARDLINK: type = 'h'; 348 | eq_obj = get_object(oh->equivalentObjectId); 349 | mtime = eq_obj != NULL ? eq_obj->mtime : 0; 350 | mode = STD_PERMS; 351 | break; 352 | case YAFFS_OBJECT_TYPE_SPECIAL: 353 | switch (oh->yst_mode & S_IFMT) { 354 | case S_IFBLK: type = 'b'; 355 | snprintf(fsize, sizeof(fsize), "%d,%4d", 356 | major(oh->yst_rdev), 357 | minor(oh->yst_rdev)); 358 | break; 359 | case S_IFCHR: type = 'c'; 360 | snprintf(fsize, sizeof(fsize), "%d,%4d", 361 | major(oh->yst_rdev), 362 | minor(oh->yst_rdev)); 363 | break; 364 | case S_IFIFO: type = 'p'; break; 365 | case S_IFSOCK: type = 's'; break; 366 | default: type = '?'; break; 367 | } 368 | break; 369 | default: type = '?'; break; 370 | } 371 | 372 | /* get file permissions */ 373 | perm[0] = mode & S_IRUSR ? 'r' : '-'; 374 | perm[1] = mode & S_IWUSR ? 'w' : '-'; 375 | perm[2] = mode & S_IXUSR ? 'x' : '-'; 376 | perm[3] = mode & S_IRGRP ? 'r' : '-'; 377 | perm[4] = mode & S_IWGRP ? 'w' : '-'; 378 | perm[5] = mode & S_IXGRP ? 'x' : '-'; 379 | perm[6] = mode & S_IROTH ? 'r' : '-'; 380 | perm[7] = mode & S_IWOTH ? 'w' : '-'; 381 | perm[8] = mode & S_IXOTH ? 'x' : '-'; 382 | if (mode & S_ISUID) perm[2] = perm[2] == '-' ? 'S' : 's'; 383 | if (mode & S_ISGID) perm[5] = perm[5] == '-' ? 'S' : 's'; 384 | if (mode & S_ISVTX) perm[8] = perm[8] == '-' ? 'T' : 't'; 385 | perm[9] = '\0'; 386 | 387 | /* print file infos */ 388 | localtime_r(&mtime, &tm); 389 | printf("%c%s %8s %4d-%02d-%02d %02d:%02d %s", 390 | type, perm, fsize, 391 | tm.tm_year + 1900, tm.tm_mon + 1, tm.tm_mday, 392 | tm.tm_hour, tm.tm_min, name); 393 | 394 | /* link destination */ 395 | if (oh->type == YAFFS_OBJECT_TYPE_HARDLINK) { 396 | if (eq_obj == NULL) 397 | printf(" -> !!! Invalid !!!"); 398 | else 399 | printf(" -> /%s", eq_obj->path_name); 400 | } else if (oh->type == YAFFS_OBJECT_TYPE_SYMLINK) { 401 | printf(" -> %s", oh->alias); 402 | } 403 | printf("\n"); 404 | } 405 | 406 | int read_chunk(void); 407 | 408 | static struct { 409 | unsigned objectId; 410 | int chunk_no; 411 | unsigned char data[MAX_CHUNK_SIZE + MAX_SPARE_SIZE]; 412 | } saved_chunk; 413 | 414 | int next_data_chunk(void) { 415 | yaffs_PackedTags2 *pt; 416 | unsigned next_objectId; 417 | unsigned next_chunkId; 418 | int ret = 0; 419 | 420 | pt = (yaffs_PackedTags2 *)spare_data; 421 | next_objectId = pt->t.objectId; 422 | next_chunkId = pt->t.chunkId + 1; 423 | 424 | if (saved_chunk.objectId == next_objectId && /* use saved chunk ? */ 425 | next_chunkId == 1) { 426 | memcpy(chunk_data, saved_chunk.data, chunk_size); 427 | memcpy(spare_data, saved_chunk.data+chunk_size, spare_size); 428 | saved_chunk.objectId = 0; 429 | ret = 1; 430 | } else { 431 | if (read_chunk() && /* valid next chunk ? */ 432 | pt->t.objectId == next_objectId && 433 | pt->t.chunkId == next_chunkId) { 434 | ret = 1; 435 | } 436 | } 437 | return ret; 438 | } 439 | 440 | void process_chunk(void) { 441 | yaffs_ObjectHeader oh; 442 | yaffs_PackedTags2 *pt; 443 | object *obj, *eq_obj; 444 | int out_file, remain, s; 445 | 446 | oh = *(yaffs_ObjectHeader *)chunk_data; 447 | pt = (yaffs_PackedTags2 *)spare_data; 448 | 449 | if (pt->t.sequenceNumber == 0xffffffff) /* empty object */ 450 | return; 451 | 452 | if (saved_chunk.objectId != 0 && /* saved chunk is not part of object */ 453 | saved_chunk.objectId != pt->t.objectId) { 454 | prt_err(0, 0, "Warning: Invalid header at chunk #%d, skipping...", 455 | saved_chunk.chunk_no); 456 | if (++warn_count >= MAX_WARN) 457 | prt_err(1, 0, "Giving up"); 458 | saved_chunk.objectId = 0; 459 | } 460 | 461 | if (pt->t.chunkId == 1) { /* save chunk #1 */ 462 | saved_chunk.objectId = pt->t.objectId; 463 | saved_chunk.chunk_no = chunk_no; 464 | memcpy(saved_chunk.data, chunk_data, chunk_size); 465 | memcpy(saved_chunk.data+chunk_size, spare_data, spare_size); 466 | return; 467 | } else if (pt->t.chunkId != 0) { /* not a new object */ 468 | prt_err(0, 0, "Warning: Invalid header at chunk #%d, skipping...", 469 | chunk_no); 470 | if (++warn_count >= MAX_WARN) 471 | prt_err(1, 0, "Giving up"); 472 | return; 473 | } 474 | 475 | obj = add_object(&oh, pt); 476 | 477 | /* listing */ 478 | if (opt_verbose) 479 | prt_node(obj->path_name, &oh); 480 | else if (opt_list) 481 | printf("%s\n", obj->path_name); 482 | if (opt_list) { 483 | if (oh.type == YAFFS_OBJECT_TYPE_FILE) { 484 | remain = oh.fileSize; /* skip over data chunks */ 485 | while(remain > 0) { 486 | if (!next_data_chunk()) 487 | prt_err(1, 0, "Broken image file"); 488 | remain -= pt->t.byteCount; 489 | } 490 | } 491 | return; 492 | } 493 | 494 | switch(oh.type) { 495 | case YAFFS_OBJECT_TYPE_FILE: 496 | remain = oh.fileSize; 497 | out_file = creat(obj->path_name, oh.yst_mode & STD_PERMS); 498 | if (out_file < 0) 499 | prt_err(1, errno, "Can't create file %s", obj->path_name); 500 | while(remain > 0) { 501 | if (!next_data_chunk()) 502 | prt_err(1, 0, "Broken image file"); 503 | s = (remain < pt->t.byteCount) ? remain : pt->t.byteCount; 504 | if (safe_write(out_file, chunk_data, s) < 0) 505 | prt_err(1, errno, "Can't write to %s", obj->path_name); 506 | remain -= s; 507 | } 508 | close(out_file); 509 | safe_lchown(obj->path_name, oh.yst_uid, oh.yst_gid); 510 | if ((oh.yst_mode & EXTRA_PERMS) != 0 && 511 | chmod(obj->path_name, oh.yst_mode) < 0) 512 | prt_err(0, errno, "Warning: Can't chmod %s", obj->path_name); 513 | break; 514 | case YAFFS_OBJECT_TYPE_SYMLINK: 515 | if (symlink(oh.alias, obj->path_name) < 0) 516 | prt_err(1, errno, "Can't create symlink %s", obj->path_name); 517 | safe_lchown(obj->path_name, oh.yst_uid, oh.yst_gid); 518 | break; 519 | case YAFFS_OBJECT_TYPE_DIRECTORY: 520 | if (pt->t.objectId != YAFFS_OBJECTID_ROOT && 521 | mkdir(obj->path_name, oh.yst_mode & STD_PERMS) < 0) 522 | prt_err(1, errno, "Can't create directory %s", obj->path_name); 523 | safe_lchown(obj->path_name, oh.yst_uid, oh.yst_gid); 524 | if ((pt->t.objectId == YAFFS_OBJECTID_ROOT || 525 | (oh.yst_mode & EXTRA_PERMS) != 0) && 526 | chmod(obj->path_name, oh.yst_mode) < 0) 527 | prt_err(0, errno, "Warning: Can't chmod %s", obj->path_name); 528 | break; 529 | case YAFFS_OBJECT_TYPE_HARDLINK: 530 | eq_obj = get_object(oh.equivalentObjectId); 531 | if (eq_obj == NULL) 532 | prt_err(1, 0, "Invalid equivalentObjectId %u in object %u (%s)", 533 | oh.equivalentObjectId, pt->t.objectId, oh.name); 534 | if (link(eq_obj->path_name, obj->path_name) < 0) 535 | prt_err(1, errno, "Can't create hardlink %s", obj->path_name); 536 | break; 537 | case YAFFS_OBJECT_TYPE_SPECIAL: 538 | if (mknod(obj->path_name, oh.yst_mode, oh.yst_rdev) < 0) { 539 | if (errno == EPERM || errno == EINVAL) 540 | prt_err(0, errno, "Warning: Can't create device %s", obj->path_name); 541 | else 542 | prt_err(1, errno, "Can't create device %s", obj->path_name); 543 | } else 544 | safe_lchown(obj->path_name, oh.yst_uid, oh.yst_gid); 545 | break; 546 | case YAFFS_OBJECT_TYPE_UNKNOWN: 547 | break; 548 | } 549 | 550 | /* set file date and time */ 551 | switch(oh.type) { 552 | case YAFFS_OBJECT_TYPE_FILE: 553 | case YAFFS_OBJECT_TYPE_SPECIAL: 554 | #ifdef HAS_LUTIMES 555 | case YAFFS_OBJECT_TYPE_SYMLINK: 556 | #endif 557 | set_utime(obj->path_name, 558 | oh.yst_atime, oh.yst_mtime); 559 | break; 560 | case YAFFS_OBJECT_TYPE_DIRECTORY: 561 | default: 562 | break; 563 | } 564 | } 565 | 566 | 567 | int read_chunk(void) { 568 | ssize_t s, len, offset; 569 | 570 | chunk_no++; 571 | len = chunk_size + spare_size; 572 | offset = 0; 573 | memset(data, 0xff, len); 574 | 575 | if (buf_len > buf_idx) { /* copy from buffer */ 576 | s = buf_len - buf_idx; 577 | if (s > len) s = len; 578 | memcpy(data, buffer+buf_idx, s); 579 | buf_idx += s; offset += s; 580 | } 581 | 582 | if (offset < len) { /* read from file */ 583 | s = safe_read(img_file, data+offset, len-offset); 584 | if (s < 0) 585 | prt_err(1, errno, "Read image file"); 586 | offset += s; 587 | } 588 | 589 | if (offset != 0 && offset != len) /* partial chunk */ 590 | prt_err(1, 0, "Broken image file"); 591 | 592 | if (offset == len && spare_off != 0) { /* bad block info */ 593 | memmove(data+chunk_size, data+chunk_size+spare_off, 594 | spare_size-spare_off); 595 | memset(data+len-spare_off, 0xff, spare_off); 596 | } 597 | 598 | return offset != 0; 599 | } 600 | 601 | int check_layout(int chunk, int spare, int off) { 602 | yaffs_ObjectHeader oh; 603 | yaffs_PackedTags2 pt; 604 | int i, detect; 605 | 606 | for (i = 0, detect = 1; i < 4 && detect; i++) { 607 | memcpy(&oh, buffer + i * (chunk+spare), sizeof(oh)); 608 | memcpy(&pt, buffer + i * (chunk+spare) + chunk+off, sizeof(pt)); 609 | 610 | detect = 611 | (pt.t.chunkId > 0 && pt.t.chunkId <= 10 && 612 | pt.t.objectId >= 0x100 && pt.t.objectId <= (0x100+10) && 613 | pt.t.byteCount <= chunk) || 614 | (pt.t.chunkId == 0 && 615 | (pt.t.objectId == YAFFS_OBJECTID_ROOT || 616 | (pt.t.objectId >= 0x100 && pt.t.objectId <= (0x100+10))) && 617 | pt.t.byteCount == 0xffff && 618 | (oh.type == YAFFS_OBJECT_TYPE_FILE || 619 | oh.type == YAFFS_OBJECT_TYPE_DIRECTORY || 620 | oh.type == YAFFS_OBJECT_TYPE_SYMLINK || 621 | oh.type == YAFFS_OBJECT_TYPE_HARDLINK || 622 | oh.type == YAFFS_OBJECT_TYPE_SPECIAL)); 623 | } 624 | 625 | return detect; 626 | } 627 | 628 | void detect_flash_layout(int show, int first) { 629 | int cnt; 630 | int chunk, spare, off; 631 | 632 | memset(buffer, 0xff, sizeof(buffer)); 633 | buf_len = safe_read(img_file, buffer, sizeof(buffer)); 634 | if (buf_len < 0) 635 | prt_err(1, errno, "Read image file"); 636 | 637 | if (show) 638 | printf("Detected flash layout(s):\n"); 639 | 640 | cnt = 0; 641 | for (chunk = MIN_CHUNK_SIZE; chunk <= MAX_CHUNK_SIZE; chunk *= 2) { 642 | for (spare = MIN_SPARE_SIZE; spare <= MAX_SPARE_SIZE; spare += 16) { 643 | for (off = 0; off <= 2; off += 2) { 644 | if (check_layout(chunk, spare, off)) { 645 | cnt++; 646 | if (show) { 647 | printf("%2s -c %-2d -s %-3d : chunk size = %2dK, spare size = %3d, %sbad block info\n", 648 | off ? "-b" : "", chunk / 1024, spare, 649 | chunk / 1024, spare, off ? "" : "no "); 650 | } 651 | if (first) { 652 | chunk_size = chunk; 653 | spare_size = spare; 654 | spare_off = off; 655 | return; 656 | } 657 | } 658 | } 659 | } 660 | } 661 | 662 | if (cnt == 0) { 663 | if (show) { 664 | printf("-- none --\n"); 665 | exit(1); 666 | } else { 667 | prt_err(1, 0, "Can't determine flash layout, perhaps not a yaffs2 image"); 668 | } 669 | } 670 | } 671 | 672 | void usage(void) { 673 | fprintf(stderr, "\ 674 | unyaffs V%s - extract files from a YAFFS2 file system image.\n\ 675 | \n\ 676 | Usage: unyaffs [options] []\n\ 677 | \n\ 678 | Options:\n\ 679 | -d detection of flash layout, no extraction\n\ 680 | -b spare contains bad block information\n\ 681 | -c set chunk size in KByte (default: autodetect, max: %d)\n\ 682 | -s set spare size in Byte (default: autodetect, max: %d)\n\ 683 | -t list image contents\n\ 684 | -v verbose output\n\ 685 | -V print version\n\ 686 | ", VERSION, MAX_CHUNK_SIZE / 1024, MAX_SPARE_SIZE); 687 | exit(1); 688 | } 689 | 690 | int main(int argc, char **argv) { 691 | int ch; 692 | char *ep; 693 | 694 | int opt_detect; 695 | int opt_bad; 696 | int opt_chunk; 697 | int opt_spare; 698 | 699 | /* handle command line options */ 700 | opt_detect = 0; 701 | opt_bad = 0; 702 | opt_chunk = 0; 703 | opt_spare = 0; 704 | opt_list = 0; 705 | opt_verbose = 0; 706 | while ((ch = getopt(argc, argv, "dbc:s:tvVh?")) > 0) { 707 | switch (ch) { 708 | case 'd': 709 | opt_detect = 1; 710 | break; 711 | case 'b': 712 | opt_bad = 1; 713 | break; 714 | case 'c': 715 | opt_chunk = strtol(optarg, &ep, 0); 716 | if (*ep != '\0' || 717 | opt_chunk < 0 || 718 | opt_chunk > (MAX_CHUNK_SIZE / 1024) ) 719 | usage(); 720 | break; 721 | case 's': 722 | opt_spare = strtol(optarg, &ep, 0); 723 | if (*ep != '\0' || 724 | opt_spare < 0 || 725 | opt_chunk > MAX_SPARE_SIZE) 726 | usage(); 727 | break; 728 | case 't': 729 | opt_list = 1; 730 | break; 731 | case 'v': 732 | opt_verbose = 1; 733 | break; 734 | case 'V': 735 | printf("V%s\n", VERSION); 736 | exit(0); 737 | break; 738 | case 'h': 739 | case '?': 740 | default: 741 | usage(); 742 | break; 743 | } 744 | } 745 | 746 | /* extract rest of command line parameters */ 747 | if ((argc - optind) < 1 || (argc - optind) > 2) 748 | usage(); 749 | 750 | if (strcmp(argv[optind], "-") == 0) { /* image file from stdin ? */ 751 | img_file = 0; 752 | } else { 753 | img_file = open(argv[optind], O_RDONLY); 754 | if (img_file < 0) 755 | prt_err(1, errno, "Open image file failed"); 756 | } 757 | 758 | if (opt_detect) { 759 | detect_flash_layout(1, 0); 760 | return 0; 761 | } 762 | 763 | if (opt_chunk == 0 || opt_spare == 0) { 764 | detect_flash_layout(0, 1); 765 | if (opt_verbose) 766 | prt_err(0, 0, 767 | "Header check OK, chunk size = %dK, spare size = %d, %sbad block info.", 768 | chunk_size/1024, spare_size, spare_off ? "" : "no "); 769 | } else { 770 | chunk_size = opt_chunk * 1024; 771 | spare_size = opt_spare; 772 | spare_off = opt_bad ? 2 : 0; 773 | } 774 | spare_data = data + chunk_size; 775 | 776 | if ((argc - optind) == 2 && !opt_list) { 777 | if (mkdirpath(argv[optind+1], 0755) < 0) 778 | prt_err(1, errno, "Can't mkdir %s", argv[optind+1]); 779 | if (chdir(argv[optind+1]) < 0) 780 | prt_err(1, errno, "Can't chdir to %s", argv[optind+1]); 781 | } 782 | 783 | umask(0); 784 | 785 | init_obj_list(); 786 | saved_chunk.objectId = 0; 787 | while (read_chunk()) { 788 | process_chunk(); 789 | } 790 | set_dirs_utime(); 791 | close(img_file); 792 | 793 | if (warn_chown) 794 | #ifdef __CYGWIN__ 795 | prt_err(0, 0, "Warning: Can't restore owner/group attribute (limitation of Cygwin/Windows)"); 796 | #else 797 | prt_err(0, 0, "Warning: Can't restore owner/group attribute, run unyaffs as root"); 798 | #endif 799 | 800 | return 0; 801 | } 802 | --------------------------------------------------------------------------------