[Android Software Development] AndroidSword:
LEARNING ABOUT UPDATER-SCRIPT SYNTAX AND ADDING EXTERNAL FEATURES TO THE ROM​

Now comes the most interesting part of making ROMS (at least, according to me), and that is updater-script. Now, I would like you to navigate to "WORK/META-INF/com/google/android" and there you should see two files, namely, "updater-script" and "update-binary". Now, I will explain you first, What is updater-script, what is its essence and how is it so beneficial and interesting in ROM Development.

LEARNING ALL ABOUT UPDATER-SCRIPT (PART 1)

So, first of all, what the heck is this updater-script. It is basically the file, which instructs TWRP (or any other recovery) what to do with the zip (your ROM or any other flashable zip). Without this, you cant flash anything on your device (well, this is a lie). But, in flashing ROM, this is very important. So, now lets learn about the updater-script syntax

Well, before this, I need to tell you something and that is, how a recovery actually flashes a zip file (This is what I assumed). So basically, when we flash a file, the recovery looks for update-binary inside "META-INF/com/google/android" in the zip file. This update-binary, then tells the recovery, whether the zip has to be installed with a STANDALONE INSTALLER or AROMA INSTALLER. If the recovery founds out that the zip has to be installed by a standalone installer, it then goes to the updater-script, which HAS to be in the same directory as update-binary and says, Hey dude, can you tell me, what I have to do with this zip file.

Hence, the updater-script basically instructs the recovery about what to do with the zip. So, having know about updater-script and its essence, lets now discuss about the syntax or the language in which we can write the updater-script.

NOTE:- THIS IS NOT AN UPDATER-SCRIPT GUIDE, SO I WILL ONLY LET YOU KNOW OF THE IMPORTANT KEYWORDS OR OTHER SUCH STUFFS REGARDING ROM DEVELOPMENT. HOWEVER, THIS IS WHAT I HAVE LEARNT AND SO FAR, IT IS MORE THAN ENOUGH. MOREOVER, IT IS ESSENTIAL, THAT YOU PAY FULL ATTENTION TO THE UPCOMING SECTION. LASTLY, IT WILL BE GOOD, IF YOU HAVE SOME PROGRAMMING BACKGROUND, BUT IF YOU ARE NOT, I WILL TRY TO BE AS SIMPLE AND STRAIGHTFORWARD AS POSSIBLE

LEARNING ABOUT UPDATER-SCRIPT SYNTAX

So, for this, first open the updater-script with Notepad++, and you will be astonished to see a huge loads of lines. You would be like, what the heck does this mean. I am gonna explain you most (if not all) of the stuffs here.

1) ui_print("");

So, we first begin with ui_print. So, what this does is that, anything you put inside the double quotes (" "), it will print it out directly into the installer screen. (For my programming dudes if you learned about C or C++, it is like cout<<" ")

Now, lets me explain this better with an example, here is a piece of code and its output

ui_print("Hello, I am learning about ROM Building/Modification");

And its output in the recovery:

Hello, I am learning about ROM Building/Modification

Hope you got the idea, now let's go to next one.

2) run_program("/sbin/busybox","mount","/system");

This is basically a modified command. The original command (run_program()) is used to run many in-built programs available in Recovery. You will explore many versions of this command as you head forward in ROM Development. Now, what this specific command does is that, it mounts the "system" partition of the device.

Now, a few of you may ask, why do we require mounting. Basically, without mounting a particular partition, we cannot modify (Add files or remove them) the partition altogether. So, I hope that now you understand, why mounting is so important.

In ROM building, we usually need to mount system, data, cache, and dalvik. So, you will always see the following set of code in the beginning of any updater-script of a ROM.

run_program("/sbin/busybox","mount","/system");
run_program("/sbin/busybox","mount","/data");
run_program("/sbin/busybox","mount","/cache");
run_program("/sbin/busybox","mount","/preload");

3) delete("/file_path");

As the name suggests, it deletes a file located on your device. You need to specify the exact file directory in double quotes inside the parenthesis.

For example:-

delete("/system/priv-app/SystemUI/SystemUI.apk");

The above line (when running in a recovery) will delete the SystemUI.apk

4) delete_recursive("/directory);

As the name suggests, this command deletes a directory or even format a partition. It differs from the delete(); command in its working. delete_recursive() deletes a directory while delete() command deletes a file. (Too many deletes in a line:p)

For example:-

delete_recursive("/system/priv-app/SystemUI");

Note that in the above command, the Recovery will delete the SystemUI folder as well its contents.

Another use of this command is in formatting the partitions. As you know, flashing a ROM basically means, we need to extract the contents of the system folder in your zip file to the system partition of your device. So, whenever we flash a new ROM, it is always advised to wipe cache, dalvik and system.

delete_recursive("/system");
delete_recursive("/cache");
delete_recursive("/data/dalvik-cache");

The above code will firmat the necessary partitions

4) package_extract_dir("zip_directory","/directory_on_phone");

This is a very simple and an important command. What it, essentially does is that, it extracts a folder in your zip file to another folder on your device. Let me explain you, with an example:-

package_extract_dir("system","/system");
{Notice in the [B]/[/B] sign in the second parameter, and this is absent in the first parameter}

What the above command essentially does is that it extracts the [B]system[/B] folder in your ROM zip to the [B]system[/B] directory of your device.

Let's understand this with a few more examples:-

package_extract_dir("system/app","/system/app");

The above command will extract the app folder in the system inside your ROM zip to the app folder in the system directory of your device.

package_extract_dir("MODS/SystemUI","/system/app/SystemUI");

Do it urself

package_extract_dir("MODS/SecSettings2","/system/priv-app/SecSettings2");

Do it urself

5) symlink("file_name1", "file_name2");

This is a piece of code, which you will never be needing! But I am explaining this here, because, a big amount of updater-script. So, we won't go, into much detail of this command, just an overview. Now, this command basically creates a symbolic link between two files. A symbolic link is where a file has one main name, but there is one extra entry in the filename table that refers any accesses back to the main name.

symlink("/system/lib/libQjpeg.so", "/system/priv-app/SelectiveFocusViewer/lib/arm/libQjpeg.so");

This command created a symbolic link between [B]"/system/lib/libQjpeg.so"[/B] and [B]"/system/priv-app/SelectiveFocusViewer/lib/arm/libQjpeg.so"[/B]

6) set_perm(uid, gid, permissions, "file_name_with_adress");

This is quite a useful command, as it helps giving necessary permissions to various files in android. Now, basically, permissions are required for a particular file, which might require some special privilege, in order for it to complete an action. For example, Android cannot allow any file on the system to format the device, formatting can only take place if a file has necessary permissions from Android itself. So, I hope, you got the point why are permissions so important in Android.

Now let's understand the syntax of the above command, which will help give permissions to various Android Files ;)

set_perm(uid, gid, permissions, "file_name_with_adress");

Now, this statement itself has many parameters as you can see. Now, I will limit myself to only explain you the things, which is necessary for ROM development. Now the "UID" and "GID" are UserID and GroupID. (They are a little tough to explain and beyond the domain of this guide, so I will just give a brief description). They are essentially an important part of Android Permissions, and they are usually integers. Don't worry, we will look in the following example about how this works.

Secondly, permissions are like rw-r-r or rwx-rw-rw. Now, if you have been using Linux for a while, you know what these means. Lets understand this, by taking rwx-rw-rw as an example:-

Now the first section, i.e., "rwx" is the permission set given to the "Owner" of the file, the second section i.e., "rw" is the permission set given to the "Group", and the third section, "rw" is the permission given to "Global". They might seem a bit overwhelming, but bear with me, these have very little role, as far as, ROM development goes.

Now coming back to the command, the permission, there is used in an Integer form. Now, I am putting out a link here, if you want to know, how these permissions are converted to integers... (https://unix.stackexchange.com/a/39712).

Essentially, you will only need two permissions, 0644 and 0755. Out of these, 0644 will always be given to an apk, while 0755 will always be given to a folder. (0644 -> rw-r-r and 0755 -> rwx-rx-rx)

set_perm(0, 0, 0777, "/tmp/wipe.sh");

Now, the above command essentially gives the permission [B]"rwx-rwx-rwx"[/B] to the file "/tmp/wipe.sh".

7) set_perm_recursive(UID, GID, perm_directory, perm_files, "directory_name");

Now, this is very similar to set_perm(), but the only difference is that this gives permission to an entire directory and its contents. Let's look at an example...

set_perm_recursive(1000, 1000, 0775, 0644, "/system/framework");

This command gives "/system/framework" directory 0775 permissions and all its contents, 0644 permissions. (0775 -> rwx-rwx-rx).

So guys, in this post, I have tried to cover most of the essential Edify Scripting command, you will ever need in your ROM Development journey. I will be talking about a few more commands after we talk about aroma-config in the next post. I am posting out a link, which will help you find more such commands:- https://xdaforums.com/wiki/Edify_script_language.

Now, we are done with Updater Script commands (Part 1). In the upcoming post, we will talk about Aroma-Config and after that, we will look at Updater Script Commands (Part 2). So stay tuned and see you guys in the next post.