What I learned flashing an EEPROM

Context:

I used to travel with two laptops, a gaming laptop and a macbook for work on the go. I couldn’t carry a single laptop for both tasks in the past because I needed 4k for design work and a fast refresh rate display for smooth gaming. I recently upgraded to a Razer Blade Pro 17 2020 to fulfill this goal, it boasts a 4k 120hz OLED Touch Display. I needed some BIOS configurations to make it work for my usecase. The BIOS came locked on this machine by the manufacturer so I had to reflash the BIOS with a hardware programmer.

What I used:

I used a CH341A USB programmer on Windows paired with ASProgrammer. Instead of desoldering the BIOS from the Laptop Mobo and resoldering it on the programmer and back, I used the SOIC8 SOP8 Test Clip. You may want to order more than one as the first one I tried turned out to be faulty.

What I learned from this experience:

  • If your machine boots, use that as an opportunity to extract as much information as you can about the BIOS you’re about to flash. The programmer needs to know the exact model, otherwise the flashing won’t work. Most programmers have a chip-detection feature but it can be spotty.
  • Download a BIOS update from the manufacturer, and dump the BIOS from the chip. Make sure that they both have the same size. Most BIOS update executables from manufacturers can be unzipped to extract the BIOS fw.
  • Make sure to erase the BIOS before writing. If you just dump and write, the programmer will skip over the zero-bytes leaving you with a corrupt BIOS fw. Some programmers have an erase-flash-verify feature, use that if you can.
  • Practice on a dual-BIOS Computer Motherboard if you can, that’ll greatly lower your chances of failure in your first attempt.

What I don't like about Node.js

Node.js is a JavaScript runtime built on Chrome’s V8 JavaScript engine. I have been working with Node.js for many years and for the most part, it has been a pleasant and productive experience. The prototyping speed and ability to scale that same quickly written and not particularly optimized code is amazing. It often falls short when you do try to optimize tho, you hit the architectural limitations faster than you expect to.

Please keep in mind that this rant is a product representing its time, so if you are reading this in the future, It may no longer be true, or I would hope so.

Short version: No proper multi threading with atomic object access.

Long version:

My personal experience with the short comings of Node.js comes as a result of trying to write a fast module bundler in Node.js. In case you don’t know, a module bundler like Webpack, Rollup, Parcel or my own (now deprecated) Pundle is a piece of software that takes your program and its dependencies and bundles them up into a single file or sometimes chunks often to be consumed by browsers. Module bundlers are what let you require(...) or import(...) dependencies in the browser without the hassle of writing all those <script ...> tags by hand (and having to maintain the order of dependencies). You can configure them to do much more than that but that’s the gist of it.

For a module bundler to become widely successful, it has to be configurable, work well and be reasonably fast. Now the configurability and being reasonably fast are difficult to achive together as I have come to learn through experience. For example, if you control all the steps in the pipeline, you only have to parse the file once and continue to work on AST from then onwards. Making it configurable means that each step much pass on the resultant string or buffer to the next step. Most module bundlers of current time work this around by manipulating strings directly using magic-string or something similar to it. Using it allows you to infer trivial things and replace imports with internal references. Making the pipeline configurable however means that someone could put Babel transpilation or Terser minification in there, so your direct-string-manipulation isn’t going to stop parsing and re-parsing.

Another thing that makes this very difficult, if not impossible to scale with the number of cores is the fact that you cannot share objects between threads in Node.js. IPC in Node.js is quite expensive, expensive enough that you have to architect your module bundler so that one process does nothing but task the others and even then, it ends up being bottle-necked because of IPC overhead of serializing and unserializing these thousands of files and their state at different steps in the pipeline, not to mention that if you want to avoid re-parsing the file, AST is much much bigger than the source and you require most metadata of all the files in a bundle to generate a source map. Node.js only recently got proper multi threading support via worker_threads, but even that does not allow sharing actual objects, only transferring ownership of Buffers.

The source of lack of ability to transfer objects instead of Buffers, per my unverified source is that v8 locks the entire virtual machine to all threads when you try to call something in parallel. So your module bundler is limited by the fundamental limitations of the v8 engine. It’s like trying to run a horse race with only one horse. You cannot work around it.

JavascriptCore (the JS engine used inside Safari) does not however lock the entire virtual machine but only the specific execution context. There was an attempt by @voodooattack to create a runtime on top of it to rival (if not compliment) Node.js. It is called Nexus.js and available on Github. It supports Concurrent Variable Access among other cool things but sadly hasn’t gotten the love it deserved. Most Module Bundlers, if they were just ported to Nexus.js with no changes other than to remove their current worker pooling implementations would run many many times faster.

Alas.

Further reading:

How imports are handled in Pundle

This post is a part of the series “An Intro to Pundle”.


Imports are a way of sharing bindings between modules. Imports help keep the code modular and maintainable as the size of the codebase grows as well as sharing it with other people trying to solve similar problems (think npmjs).

In Javascript runtimes like V8 (heart of Chrome browser and Node.js) you can import JS files into JS files but you cannot for example import CSS files into JS files. Being able to import CSS into JS would help modularize the code further and help avoid any conflicts on the global css namespace.

Interoperability between asset types as different as JS and CSS (compared to say JS and WASM) requires a lot of decisions that are better done by the bundler instead of runtime. For example, whether to use the contents of the asset or export a path pointing at it because the size of the asset is too big (think 150kb+ of blob, wouldn’t make sense in a JS bundle).

While other module bundlers try to hard-code how the bridges work between types in the core, Pundle takes a different approach. Pundle’s file resolver allows users to specify completely arbitrary “format”s to use for each extension. Pundle enforces “chunk”s to have the same format throughout so when you import a css file or a golang file or any other non-js format file, It’ll be force loaded as js as to allow the transformers to provide “bridge” code.

While providing the bridge code the transformers can add the import as a chunk dependency and return path to it or return it as base64 blob etc. For css this lets us do interesting things like return the css modules object to the requiring module or insert css into a JSON blob that accepts HMR during development.

For example, here’s what happens when your import ./index.js imports an ./index.css

  • Resolve ./index.js relative to project root
  • Resolved to project/index.js with format js
  • Process project/index.js as js and scan for imports
  • Resolve ./index.css relative to ./index.js
  • Resolved to project/index.css with format css
  • Process project/index.css as js and scan for imports (notice how we’re processing as js)
    • If the file contains with .module before its extension like (.module.css), return its module mapping back to JS
    • If HMR and Development are turned on, put the raw contents as blob to create a style tag to be replaced on HMR later
      • Otherwise add project/index.css with format css as an external dependency of the file

This process depending on your configuration would either embed the css in JS file or add it as an external dependency (while also resolving its imports and adding them as dependencies recursively). This allows Pundle to maintain multiple versions of the same file with different formats. Handling imports like this allows for an unprecedented level of customizable interoperability between different formats.

How HMR in Pundle works

This post is a part of the series “An Intro to Pundle”.


HMR (Hot Module Replacement) is a way to apply changes to parts of the app while it’s still running allowing developers to quickly test new changes and be more productive. Webpack has a great introduction to HMR. I recommend you read it before reading through the rest of the post.

Here are some of the interesting things about Pundle’s implementation of HMR

Determining HMR server path:

Both Webpack and Parcel use WebSockets to deliver HMR to the clients. These can be a bit hard to develop / maintain because to handle WebSocket connections on the server side you have to either listen on a separate port for HMR (if source is on https, then you have to have https on ws too) or you have to provide the server object to the WebSocket libs (which means it cannot be an express middleware).

That leads to extra moving parts in the config, hmrHost or hmrPath. Pundle solves this by getting rid of WebSockets and using fetch() streaming instead. Fetch streaming works on normal HTTP requests so it can be done in an express middleware and does not need any third party library. For paths, Pundle uses document.currentScript + .hmr.pundle and then handles it as a route on the server.

As HMR is part of the development workflow, you don’t have to worry about supporting fetch streaming in older browsers as long as your own is up to date.

Compiling and applying changes

While Webpack uses a poll mechanism to determine if files have changed, Pundle makes the server do the heavy lifting. It writes HMR payloads to an internal list of streaming http requests. Pundle’s architecture allows it to generate individual files as temporary chunks, meaning that even if your bundle is 100mbs in size, only changed files would be transformed and sent to the client. This gives Pundle O(nFilesChanged) instead of O(nFilesTotal) time complexity for HMR.

Pundle also only regenerates a full bundle (ie joining all the transformed files together) only when it receives the next non-HMR request meaning that if you keep saving your files without refreshing, your CPU cycles won’t be wasted.


If you would like to read the source code of the HMR client (at the time of writing this post), You can hmr-client on Github

An Intro to Pundle

Pundle (Peaceful Bundle) is a next generation module bundler. It allows you to use Node.js style require statements in the browser and supports splitting out of the box with import() syntax. It uses worker processes to parallelize the work as much as possible, that combined with a file system transform cache delivers amazing performance on recurrent builds.

You can read how it works in this series of posts:


This series of posts is a Work-in-Progress and would be updated as Pundle furthers it’s path towards release.

Feminism vs Equality

Today is the 8th of March and is also the International Women’s day. While the media outlets use this day to promote a few women who did great things, I would like to pay respect and remember today the women who don’t get as much recognition but work very hard everyday to take care of their families and societies. These are our mothers, daughters, sisters and partners around the world.

It’s uplifting how the awareness against unjust treatment for women is raising around the world. We have come a long way from when women were burnt alive when their husbands died to when women are bosses in major institutions. But our struggle for justice is not over. True equality for all is possible only when we all try to do our part in ensuring equal opportunities and treatment for women around the world.

Countless movements around the world are pursuing this goal and among them is the Feminist movement. Like all the non-moderated public movements, it has a lot of flavors. In Feminism, there are people who want equality for all, and there are people who think women should have unfair advantage, both these groups living under the same umbrella.

Inequality against men is just as bad as inequality against women. Please stop using the word feminism for equality. If you believe in equality for all people regardless of gender, you are an equalist. Please do not associate yourself with people who are like the ones you are advocating against.

Thank You.

Why Reason?

Reason is a way to think properly while avoiding obstacles like bias, faith, emotions and beliefs. Reason is ultimately a tool for finding the truth. Instead of giving one person the authority to declare what is right and what is not, reason divides that authority among individuals.

Neither knowing the truth makes one reasonable, nor not knowing the truth makes one unreasonable. It’s the thought that “The person I am engaged in a discourse with might know something I do not. I might be wrong. I might learn something new.” As proven by Machine Learning in digital systems where neurons execute over datasets of millions, “On a large enough scale, quantity compensates for quality.” Thus, reason helps make societies where laws are fair, equal and enforced on all.

In a reasonable society, we take risks every time we make a statement. If the statement is verified, we gain trust of others or establish ourselves as liars otherwise. But the statements are not always verifiable, in which cases the liars would have a hard time convincing people of their occasional truth than truth tellers of their occasional lies.

The modern age of technology makes connection to the entire world incredibly easy. While it exposes us to people of vastly different cultures and beliefs, it also allows us to connect with millions of other people that think the way we do. People of the latter kind end up in an echo chamber that amplifies their thoughts ten folds. This pushes them further into the cave of ignorance and farther from the truth reason promises.

I have lost track of how many times I’ve seen a celebrity say things like “[Name] is a horrible person” without stating why they think that, which results in a backlash from supporters of the person referenced, which are again offensive statements without reason. This continues until the original poster claims the backlash is because of their race or gender and does still not explain why they said what they did.

Actions such as these are not only condescending but also very counter-productive. They do not encourage improvement while discouraging current behavior. The next time someone claims on Twitter, that someone else is a terrible person, don’t reply to them saying they are a terrible person for posting this. Instead, ask for their reasoning and proof so you can dismiss it or believe it based on your criteria of truth.